1、递增运算符重载
#include <iostream>
using namespace std;
class Person
{
friend ostream & operator<<(ostream &cout, Person& p);
public:
Person()
{
m_A = 0;
}
//前置++运算符
//重置前置++运算符 返回引用为了一直对一个数据进行递增操作
Person& operator++()
{
m_A++;
return *this;
}
//后置++运算符
//void operator++(int) int表示占位参数
Person operator++(int)
{
Person tmp = *this;
m_A++;
return tmp;
}
private:
int m_A;
};
ostream & operator<<(ostream &cout, Person& p)
{
cout << p.m_A << endl;
return cout;
}
//前置++
void test1()
{
Person P1;
cout << ++(++P1) << endl;
cout << P1 << endl;
}
//后置++
void test2()
{
Person P1;
cout << P1++ << endl;
cout << P1 << endl;
}
int main()
{
test2();
return 0;
}
2、赋值运算符重载
#include <iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
m_Age = new int(age);//在堆区开辟内存 要自己进行释放
}
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 test()
{
Person P1(10);
Person P2(20);
Person P3(30);
P1 = P2 = P3;
cout << "P1 = " << *(P1.m_Age) << " P2 = " << *(P2.m_Age) << " P3 = " << *(P3.m_Age) << endl;
}
int main()
{
test();
return 0;
}
3、加号运算符重载
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person()
{
func(10,10);
}
//成员函数的加法重载
/*Person operator+(Person &p)
{
Person tmp;
tmp.m_A = this->m_A + p.m_A;
tmp.m_B = this->m_B + p.m_B;
return tmp;
}*/
void func(int a, int b)
{
m_A = a;
m_B = b;
}
int m_A;
int m_B;
};
//全局函数的加法重载
Person operator+(Person&P1, Person&P2)
{
Person tmp;
tmp.m_A = P1.m_A + P2.m_A;
tmp.m_B = P1.m_A + P2.m_B;
return tmp;
}
void test()
{
Person P1;
Person P2;
Person P3;
P3 = P1 + P2;
cout << "P3_m_A = " << P3.m_A << endl;
cout << "P3_m_B = " << P3.m_B << endl;
}
int main()
{
test();
return 0;
}
4、左移运算符重载
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person()
{
func(10,10);
}
//成员函数的加法重载
/*Person operator+(Person &p)
{
Person tmp;
tmp.m_A = this->m_A + p.m_A;
tmp.m_B = this->m_B + p.m_B;
return tmp;
}*/
void func(int a, int b)
{
m_A = a;
m_B = b;
}
int m_A;
int m_B;
};
//全局函数的加法重载
Person operator+(Person&P1, Person&P2)
{
Person tmp;
tmp.m_A = P1.m_A + P2.m_A;
tmp.m_B = P1.m_A + P2.m_B;
return tmp;
}
void test()
{
Person P1;
Person P2;
Person P3;
P3 = P1 + P2;
cout << "P3_m_A = " << P3.m_A << endl;
cout << "P3_m_B = " << P3.m_B << endl;
}
int main()
{
test();
return 0;
}
5、函数调用运算符重载
#include <iostream>
using namespace std;
#include <string>
//函数调用运算符重载
class Person
{
public:
//重载函数调用运算符
void operator()(string str)
{
cout << str << endl;
}
};
//仿函数非常灵活 没有固定的写法
class MyAdd
{
public:
int operator()(int a, int b)
{
return a + b;
}
};
void test()
{
Person P1;
MyAdd myadd;
P1("你好!");//由于使用起来非常类似函数调用 因此称为仿函数
cout << "两数之和为:" << myadd(10, 20) << endl;
cout << MyAdd()(10, 20) << endl;//匿名函数对象 MyAdd()匿名函数
}
int main()
{
test();
return 0;
}
6、关系运算符重载
#include <iostream>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
m_name = name;
m_age = age;
}
//成员函数关系运算符重载
bool operator==(Person& P)
{
if (this->m_age == P.m_age && this->m_name == P.m_name)
{
return true;
}
else
return false;
}
private:
string m_name;
int m_age;
};
void test()
{
Person P1("朱振麟", 23);
Person P3("朱振麟", 23);
Person P2("张萌", 22);
if (P1 == P3)
{
cout << "两者相等" << endl;
}
else
cout << "两者不相等" << endl;
}
int main()
{
test();
return 0;
}
|