运算符重载
//概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
1、加号运算符重载
//作用:实现两个自定义数据类型的相加运算
class Person12
{
public:
int p_A;
int p_B;
//成员函数重载 +号
Person12 operator+ (Person12& p)
{
Person12 temp;
temp.p_A = this->p_A + p.p_A;
temp.p_B = this->p_B + p.p_B;
return temp;
}
};
//全局函数重载 +号
//Person12 operator+(Person12& p1, Person12& p2)
//{
// Person12 temp;
// temp.p_A = p1.p_A + p2.p_A;
// temp.p_B = p1.p_B + p2.p_B;
// return temp;
//}
//函数重载版本
Person12 operator+(Person12& p1,int a)
{
Person12 temp;
temp.p_A = p1.p_A + a;
temp.p_B = p1.p_B + a;
return temp;
}
void test27()
{
Person12 p1;
p1.p_A = 10;
p1.p_B = 20;
Person12 p2;
p2.p_A = 11;
p2.p_B = 22;
Person12 p3;
p3 = p1 + p2;
//成员函数重载 +号本质:p3 = p1.operator+(p2);
//全局函数重载 +号本质:p3 = operator+(p1, p2);
Person12 p4;
p4 = p1 + 10;//p4 = operator+(p1,10);
cout << "p3.p_A = " << p3.p_A << endl;
cout << "p3.P_B = " << p3.p_B << endl;
cout << "p4.p_A = " << p4.p_A << endl;
cout << "p4.P_B = " << p4.p_B << endl;
}
int main33()
{
test27();
system("pause");
return 0;
}
注意:内置数据类型的表达式的运算符是不可修改的(即不能修改 1+1=0 实现 + == - )
2、左移运算符重载
//作用:可以输出自定义数据类型
class Person13
{
friend ostream& operator<<(ostream& cout, Person13& p);
private:
int p_A;
int p_B;
public:
Person13(int a, int b)
{
p_A = a;
p_B = b;
}
//成员函数重载
//void operator<<(cout)
//{
//}
//p.operator<<(cout); == p << cout; 与想法cout << p;不一致
//故一般不会使用成员函数重载左移运算符,因为无法实现cout在左侧
};
//全局函数重载
//void operator<<(ostream &out, Person13& p)//本质:operator<<(out,p) 简化 cout << p
//{ //out 只是一个别名
// cout << "p_A = " << p.p_A << "\t" << "p_B = " << p.p_B << endl;
//}
//缺点:不能链式追加,加endl会报错。 改进如下:
ostream& operator<<(ostream& cout, Person13& p)//ostream 标准输出流
{
cout << "p_A = " << p.p_A << "\t" << "p_B = " << p.p_B;
return cout;
}
void test28()
{
Person13 p1(10, 10);
//cout << p1.p_A << endl;
cout << p1 << endl;
}
int main34()
{
test28();
system("pause");
return 0;
}
3、递增运算符重载
//作用:通过重载递增运算符,实现自己的整形数据
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myint);
private:
int m_Num;
public:
MyInteger()
{
m_Num = 0;
}
//重载++运算符(前置)
MyInteger& operator++()//返回类型必须是引用&,保证如果多次进行++是在对同一个数据进行操作
{
m_Num++; //先 进行++运算
return *this; //再 将自身做返回
}
//重载++运算符(后置)
MyInteger operator++(int)//int代表占位参数,可以用来区分前置和后置递增 不能返回 &,因为temp是临时变量
{
MyInteger temp = *this;//先 记录当时结果
m_Num++;//再 递增
return temp;//再 返回记录结果
}
};
ostream& operator<<(ostream &cout, MyInteger myint)
{
cout << myint.m_Num;
return cout;
}
void test29()
{
MyInteger m1;
cout << m1 << endl;
cout << ++m1 << endl; //1
cout << m1 << endl; //1
cout << ++(++m1) << endl; //3
cout << m1 << endl; //3
}
void test29_2()
{
MyInteger m2;
cout << m2 << endl;
cout << m2++ << endl;
cout << m2 << endl;
}
int main35()
{
test29();
test29_2();
system("pause");
return 0;
}
4、赋值运算符重载
C++编译器至少给一个类添加4个函数
1、默认构造函数
2、默认析构函数
3、拷贝构造函数
4、赋值运算符 operator= 对属性进行值拷贝
若类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
class Person14
{
public:
int* p_age;
Person14(int age)
{
p_age = new int(age);
}
~Person14()
{
if (p_age != NULL)
{
delete p_age;
p_age = NULL;
}
}
//重载 赋值运算符
Person14& operator=(Person14 &p)
{
//编译器提供的是浅拷贝,类似于:
//p_age = p.p_age;
//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
if (p_age != NULL)
{
delete p_age;
p_age = NULL;
}
//深拷贝
p_age = new int(*p.p_age);
return *this;
}
};
void test30()
{
Person14 p1(18);
cout << "p1 = " << *p1.p_age << endl;
Person14 p2(28);
cout << "p2 = " << *p2.p_age << endl;
p2 = p1;
cout << "p2 = " << *p2.p_age << endl;
Person14 p3(38);
cout << "p3 = " << *p3.p_age << endl;
p3 = p2 = p1;
cout << "p3 = " << *p3.p_age << endl;
}
int main36()
{
test30();
system("pause");
return 0;
}
5、关系运算符重载
//作用:可以比较两个自定义类型的对象
class Person15
{
public:
string p_name;
int p_age;
Person15(string name, int age)
{
p_name = name;
p_age = age;
}
bool operator==(Person15 &p)
{
if (this->p_name == p.p_name && this->p_age == p.p_age)
{
return true;
}
return false;
}
bool operator!=(Person15& p)
{
if (this->p_name == p.p_name && this->p_age == p.p_age)
{
return false;
}
return true;
}
};
void test31()
{
Person15 p1("张三", 18);
Person15 p2("张三", 18);
if (p1 == p2)
{
cout << "p1和p2相等" << endl;
}
if (p1 != p2)
{
cout << "p1和p2不相等" << endl;
}
}
int main37()
{
test31();
system("pause");
return 0;
}
6、函数调用运算符重载
函数调用运算符()也可以重载
由于重载后使用的方式非常像函数的使用,所以称为仿函数
仿函数没有固定写法,非常灵活
class MyPrint
{
public:
//重载函数调用运算符
void operator()(string str)
{
cout << str << endl;
}
};
void MyPrint2(string str)
{
cout << str << endl;
}
void test32()
{
MyPrint m1;
m1("Hello China!");//使用类似于函数调用
//m1.operator()("Hello China!");
MyPrint2("Hello China!!");//函数调用
}
class MyAdd
{
public:
int operator()(int num1, int num2)
{
return num1 + num2;
}
};
void test33()
{
MyAdd m1;
//m1(10, 20);
cout << m1(10, 20) << endl;
//匿名函数对象
cout << MyAdd()(10, 20) << endl;//会创建临时的 匿名的 对象 匿名对象->当前行执行完马上释放
}
int main38()
{
test32();
test33();
system("pause");
return 0;
}
(哔哩哔 哩黑马程序员 C++教程 学习笔记,如有侵权请联系删除)