栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > C/C++/C#

c++函数重载和运算符重载

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++函数重载和运算符重载

函数重载

C++中第一种多态的实现形式:静态多态也叫编译时多态机制
C中定义函数功能类似时的方式:

函数重载概述

函数重载机制:
根据同名函数的参数不同,调用与之对应的函数。
函数重载是发生在同一作用域的。
函数重载与他的函数中的参数列中的形参的类型,个数,顺序 是相关的,与他的返回值是没有关系的。

函数重载示例
#include 
int add(int a, int b)
{
    return a + b;
}
float add1(float a, float b)
{
    return  a + b;
}
double add2(double a, double b)
{
    return  a + b;
}
int main()
{
    add1();
    return 0;
}
返回类型不同不发生重载
#include 
using namespace std;
int add(int a,int b)
{
    return  a + b;
}
float add(float a, float b)
{
    return a + b;
}
string add(string a, string b)
{
    return a + b;
}
int main()
{
    int a = 10;
    int b = 20;
    cout << add(a,b) << endl;
    float a1 = 3.14;
    float b1 = 5.21;
    cout << add(a1,b1) << endl;
    string a2 = "zhang";
    string b2 = "san";
    cout << add(a2,b2) << endl;
    return 0;
}
重载的注意事项:

1、引用作为重载条件

#include
using namespace std;
void fun(int &a){
 cout<<"hello world"<
 cout<<"hello world"<
	fun(10);//走的是第二个函数,因为int&a=10;不合法 
	int b=10;
	fun(b);//走的是第一个函数 int &a=b合法
}

2、重载遇见默认参数

void fun(int a,int b=10){
 cout<<"hello world"<
 cout<<"hello world"< 
运算符重载 
概念 

对已有运算符重新定义,赋予另一种功能适应不同数据类型。

运算符重载函数的语法形式:

实现运算符重载可以通过成员函数和全局函数实现

返回值 + operator + 要重载的运算符(形参列表)
{
    //函数体功能一定要与运算符的功能相匹配
}
C++的=号运算符重载及实现一个+运算符重载:
#include 
#include
using namespace std;
class Stu
{
private:
    string name;
    int age;
public:
    //C++11新语法。
    //Stu() = default;
    Stu(string name, int age)
    {
        this->name = name;
        this->age = age;
        cout << "Stu的构造" << endl;
    }
    ~Stu()
    {
        cout << "Stu的析构" << endl;
    }
    
    Stu(const Stu& other)
    {
        this->name = other.name;
        this->age = other.age;
    }

    //编译器自动提供的=号运算符重载函数。
    //如果当类中有属性指针指向堆区时,改造深拷贝的过程
    //我往期文章有介绍
//    Stu& operator=(const Stu& other)
//    {
//        this->name = other.name;
//        this->age = other.age;
//        return *this;
//    }

	//成员函数实现重载
    //实现一个两个对象相加的运算符重载。
    int operator+(const Stu& other)
    {
        return this->age + other.age;
    }
	
    void showInfo()
    {
        cout << "姓名:" << this->name << ",年龄:" << this->age << endl;
    }
};

//全局函数实现重载
Stu operator+(const Stu& o1,const Stu& o2){
	Stu stu;
	stu.age=o1.age+o2.age;
	return stu;
	//本质是Stu stu=operator+(o1,o2);
	//简化为Stu stu=o1+o2;
}
int main()
{
    Stu stu("zhangsan",20);
    Stu stu1("lisi",35);
//    stu1 = stu;
//    stu1.showInfo();
    //以下两者的用法是一样的。
    cout << stu + stu1 << endl;
    cout << stu.operator+(stu1) << endl;
	//本质Stu stu=stu.operator+(stu1);
	//简化Stu stu+=stu1;
    return 0;
}          
自增运算符的重载:
#include 
#include 
using namespace std;
class My_Clock
{
private:
    int min;
    int sec;
public:
    My_Clock(int m, int s)
    {
        this->min = m;
        this->sec = s;
    }
    //如果形参列表是没有任何类型 默认为前++
    My_Clock& operator++()
    {
        sec++;
        if(sec == 60)
        {
            sec = 0;
            min++;
            min %= 60;
        }
        return *this;
    }
    //使用一个int来做为一个标识,编译器会默认为后++
    //这种用类型仅做为标识使用的方式也叫哑元。
    My_Clock& operator++(int)
    {
        return ++(*this);
    }
    void showTime()
    {
        cout << min << ":" << sec << endl;
    }
};
int main()
{
    My_Clock c(0,0);
    while (true) {
        c++;
        sleep(1);
        c.showTime();
    }
    return 0;
}

重载左移运算符

成员函数实现
void operator <<(cout)//p.operator<<(cout)简化为p< 全局函数实现
,operator << (cout,p)简化为cout<< p

ostream& operator << (ostream& cout, const MyString& other)
{
    cout << other.getM_data();
    return cout ;
}

<<左移运算符和=赋值运算符都要出现追加操作,举个例子。int a =10; int b=10;
int c=20;a=b=c=30;cout < 所以我们在输出自定义数据类型时要以引用方式返回,不然值的方式返回调用的是拷贝构造。输出的是原来的复制品。而且为了提高代码运行效率我们一般用引用返回。

#include 
#include
using namespace std;
class person                                                                                      {
public:
	int age;
	string name;
	person(int age,string name){
		this->age=age;
		this->name=name;
	}
	person personAdd(person &p){
		this->age+=p.age;
		return *this;
	}

}
int main()
{
   person p1(18,"张三");
   person p2(20,"李四");
   p1.personAdd(p2).personAdd(p2).personAdd(p2);
   cout<
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1038886.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号