1. 左移运算符重载
①通常不会利用成员函数重载<<运算符,因为无法实现cout在左侧
②只能利用全局函数重载<<运算符
#include<iostream>
using namespace std;
class Person
{
public:
Person(int a,int b)
{
m_A = a;
m_B = b;
}
friend ostream& operator<<(ostream& cout, Person& p);
private:
int m_A;
int m_B;
};
ostream & operator<<(ostream &cout,Person &p)
{
cout << "m_A = " << p.m_A << " m_B = " << p.m_B;
return cout;
}
void test02()
{
Person p(10, 10);
cout << p <<endl;
}
int main()
{
test02();
system("pause");
return 0;
}
point1:cout属于ostream;
point2:私有内容用友元访问
point3:重载目的:输出一个对象就可以把对象中的属性输出
2.递增运算符重载
递增分为前置递增和后置递增
#include<iostream>
using namespace std;
//自定义整型
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
MyInteger()
{
m_Num = 0;
}
//重载前置++运算符
MyInteger& operator++() //为什么返回引用_为了一直对一个数据进行操作
{
//先进行++运算
m_Num++;
//再将自身作为返回
return *this;
}
//重载后置++运算符
MyInteger operator++(int) //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 test03()
{
MyInteger myint;
cout << ++(++myint) << endl;
cout << myint << endl;
}
void test003()
{
MyInteger myint;
cout << myint++ << endl;
cout << myint << endl;
}
int main()
{
// test03();
test003();
system("pause");
return 0;
}
point1:前置递增返回引用(为了一直对一个数据进行操作),后置递增返回值(不需要对一个局部变量进行引用)
point2:后置递增需要占位参数
point3:需要先<<重载
3.赋值运算符重载
C++编译器至少会给一个类添加四个函数
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数体为空)
3.默认拷贝构造函数,对属性进行拷贝
4.赋值运算符operator=,对属性进行值拷贝
#include<iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
m_Age = new int(age); //把age这个数据放到堆区
}
~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;
}
int *m_Age;
};
void test04()
{
Person p1(18);
Person p2(20);
Person p3(22);
p3 = p2 = p1; //赋值操作
cout << *p1.m_Age << endl;
cout << *p2.m_Age << endl;
cout << *p3.m_Age << endl;
}
int main()
{
test04();
int a = 10;
int b = 20;
int c = 30;
c = b = a;
cout << a << b << c << endl;
system("pause");
return 0;
}
point1:为什么需要?编译器提供的是浅拷贝,如果类中有属性创建在堆区,就会带来浅拷贝堆区内存重复释放的问题,所以需要重载为深拷贝
point2:①判断自身堆区内存是否为空,否就先清空。②深拷贝。③返回自身。
4.关系运算符重载
#include<iostream>
#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;
}
string m_Name;
int m_Age;
};
void test05()
{
Person p1("Tom", 28);
Person p2("Tom", 28);
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 main5()
{
test05();
system("pause");
return 0;
}
5.函数调用运算符重载
- 函数调用运算符()也可以重载
- 由于重载后使用的方式非常像函数的调用,因此称为仿函数
- 仿函数没有固定写法,非常灵活
#include<iostream>
#include<string>
using namespace std;
//打印输出类
class MyPrint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout << test << endl;
}
};
void test06()
{
MyPrint myPrint;
myPrint("HelloWorld");
}
int main()
{
test06();
system("pause");
return 0;
}