C++自学笔记(17 类和对象_运算符重载)
17 类和对象_运算符重载
本次记录运算符重载,还请各位大佬批评指正!
运算符概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。
加号运算符重载
作用: 实现两个自定义数据类型相加的运算。
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
//加号运算符重载
class person
{
public:
//person operator+(person& p)//通过成员函数重载了+运算符
person personAddperson(person& p)//通过自己写成员函数,实现两个对象属性相加
{
person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
int m_A;
int m_B;
};
void test01()
{
person p1;
p1.m_A = 10;
p1.m_B = 20;
person p2;
p2.m_A = 30;
p2.m_B = 40;
p1.personAddperson(p2);
cout << p1.personAddperson(p2).m_A << endl;
cout << p1.personAddperson(p2).m_B << endl;
//重载+运算符之后调用以下程序
//person p3 = p1.operator+(p2);//成员函数重载本质调用。可以简化为下边一行
/*person p3 = p1 + p2;
cout << p3.m_A << endl;
cout << p3.m_B << endl;*/
}
//通过全局函数重载+运算符
person operator+(person &p1, person &p2)
{
person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
//函数重载的版本
person operator+(person& p1, int num)
{
person temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
void test02()
{
person p1;
p1.m_A = 10;
p1.m_B = 20;
person p2;
p2.m_A = 30;
p2.m_B = 40;
//person p4 = operator+(p1, p2);//全局函数本质调用。可以简化为下边一行
person p4 = p1 + p2;
//运算符重载,也可以发生函数重载
person p5 = p1 + 100;//person + int
cout << p4.m_A << endl;
cout << p4.m_B << endl;
cout << p5.m_A << endl;
cout << p5.m_B << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
总结:
1、对于内置的数据类型的表达式的运算符是不可能改变的。
2、不要滥用运算符重载(如递增运算符实现递减运算符的功能)。
左移运算符重载
作用: 可以输出自定义数据类型。
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
//左移运算符重载
class person
{
friend ostream& operator<<(ostream& cout, person& p);
public:
person(int a, int b)
{
m_A = a;
m_B = b;
}
private:
利用成员函数重载<<运算符
不能利用成员函数重载<<运算符,因为无法实现 cout 在左侧
//void operator<<( cout )
//{
//}
int m_A;
int m_B;
};
//只能利用全局函数重载左移运算符
ostream & operator<<(ostream &cout, person &p)//本质 operator <<(cout, p)。简化 cout << p
{
cout << "m_A = " << p.m_A << " m_B = " << p.m_B;
return cout;
}
void test01()
{
person p1(10,20);
/*cout << p1.m_A << endl;
cout << p1.m_B << endl;*/
cout << p1 << " Hello World !" <<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结: 重载左移运算符配合友元可以实现输出自定义数据类型。
递增运算符重载
作用: 通过重载递增运算符,实现自己的整型数据。
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
//递增运算符重载
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
MyInteger()
{
m_Num = 0;
}
//重载前置++运算符
MyInteger& operator++()//返回引用是为了一直对一个数据进行递增操作。前置递增返回引用。
{
//先进行++运算
m_Num = m_Num++;
//再将自身做返回
return *this;
}
//重载后置++运算符
//int代表占位参数,可以用于区分前置和后置递增
MyInteger operator++(int)//后置递增返回值。
{
//先记录当时的结果
MyInteger temp = *this;
//后递增
m_Num++;
//最后将记录结果做返回
return temp;
}
private:
int m_Num;
};
//重载左移运算符
ostream& operator<<(ostream& cout, MyInteger myint)
{
cout << myint.m_Num;
return cout;
}
void test01()
{
MyInteger myint1;
cout << myint1 << endl;//0
cout << ++(++myint1) << endl;//2
cout << myint1 << endl;//2
}
void test02()
{
MyInteger myint2;
cout << myint2 << endl;
cout << myint2++ << endl;
cout << myint2 << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
总结: 前置递增(减)返回引用,后置递增(减)返回值。
赋值运算符重载
C++编译器至少给一个类添加4个函数:
1、默认构造函数(无参,函数体为空)。
2、默认析构函数(无参,函数体为空)。
3、默认拷贝构造函数,对属性进行值拷贝。
4、赋值运算符 operator=,对属性进行值拷贝。
若类中有属性指向堆区,做赋值操作时会出现深浅拷贝问题。
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
//赋值运算符重载
class person
{
public:
person(int age)//有参构造
{
m_Age = new int(age);//数据创建在堆区 new int 返回的是 int *
}
~person()//堆区数据需要程序员手动释放
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
//重载=运算符
person& operator=(person& p)
{
//编译器提供浅拷贝
//m_Age = p.m_Age;
//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
m_Age = new int(*p.m_Age);
//返回对象本身
return *this;
}
//private:
int* m_Age;
};
void test01()
{
person p1(18);
person p2(20);
person p3(30);
p3 = p2 = p1;//赋值操作
cout << "p1 的年龄:" << *(p1.m_Age)<< endl;
cout << "p2 的年龄:" << *(p2.m_Age) << endl;
cout << "p3 的年龄:" << *(p3.m_Age) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
关系运算符重载
作用: 重载关系运算符,可以让两个自定义类型对象进行对比操作。
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
//关系运算符重载
class person
{
public:
person(string name, int age)
{
m_Name = name;
m_Age = age;
}
//重载 ==
bool operator ==(person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
else
return false;
}
//重载 !=
bool operator !=(person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return false;
}
else
return true;
}
/*bool operator !=(person& p)
{
if (this->m_Name != p.m_Name || this->m_Age != p.m_Age)
{
return true;
}
else
return false;
}*/
string m_Name;
int m_Age;
};
void test01()
{
person p1("Tom", 18);
person p2("Tom", 18);
if (p1 == p2)
{
cout << "p1 和 p2 相等!" << endl;
}
else
{
cout << "p1 和 p2 不相等!" << endl;
}
if (p1 != p2)
{
cout << "p1 和 p2 不相等!" << endl;
}
else
{
cout << "p1 和 p2 相等!" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
函数调用的运算符重载
函数调用运算符()也可以重载。
由于重载后使用的方式非常像函数的调用,因此成为仿函数。
仿函数没有固定写法,非常灵活。
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
//函数调用运算符重载
class Myprint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout << test << endl;
}
};
void Myprint02(string test)
{
cout << test << endl;
}
void test01()
{
Myprint myprint;
myprint("Hello World!");//由于使用起来非常类似于函数调用,因此成为仿函数
Myprint02("Hello World");
}
//仿函数非常灵活,没有固定的写法
//加法类
class Myadd
{
public:
int operator()(int num1, int num2)
{
return num1 + num2;
}
};
void test02()
{
Myadd myadd;
int ret = myadd(100, 200);
cout << "ret= " << ret << endl;
//匿名函数对象 Myadd()
cout << Myadd()(100, 100) << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}