目录
一、加法运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Person {
public:
Person()
{
}
Person(int a, int b):m_A(a),m_B(b){}
//利用成员函数实现加法运算符重载
//Person operator+(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;
};
//利用全局函数实现加法运算符重载
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 test01()
{
Person p1(10,10);
Person p2(20, 20);
//Person p3 = operator+(p1, p2); //全局函数本质
//Person p3 = p1.operator+(p2); //成员函数本质
Person p3 = p1 + p2; //简化
cout << "p3.m_A = " << p3.m_A << " p3.m_B = " << p3.m_B << endl;
}
void test02()
{
Person p1(10, 10);
Person p2 = p1 + 10;
cout << "p2.m_A = " << p2.m_A << " p2.m_B = " << p2.m_B << endl;
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
二、左移运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Person {
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
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 test01()
{
Person p1(10, 10);
cout << p1 << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
三、前置递增运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class MyInt {
friend ostream& operator<<(ostream& cout, MyInt& p);
public:
MyInt()
{
this->m_A = 0;
}
//前置++ 重载
MyInt& operator++()
{
this->m_A++;
return *this;
}
private:
int m_A;
};
ostream& operator<<(ostream& cout, MyInt& p)
{
cout << p.m_A;
return cout;
}
void test01()
{
MyInt p1;
cout << ++(++p1) << endl;
cout << p1 << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
四、后置递增运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class MyInt {
friend ostream& operator<<(ostream& cout, MyInt p);
public:
MyInt()
{
this->m_A = 0;
}
//后置++ 重载
MyInt operator++(int)
{
MyInt temp = *this;
this->m_A++;
return temp;
}
private:
int m_A;
};
ostream& operator<<(ostream& cout, MyInt p)
{
cout << p.m_A;
return cout;
}
void test02()
{
MyInt p;
cout << p++ << endl;
cout << p++ << endl;
//cout << p << endl;
}
int main()
{
test02();
system("pause");
return EXIT_SUCCESS;
}
五、指针运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Person {
public:
Person(int a)
{
cout << "Person的有参构造函数调用" << endl;
m_Age = a;
}
void showAge()
{
cout << "年龄 : "<< this->m_Age << endl;
}
~Person()
{
cout << "Person的析构函数调用" << endl;
}
private:
int m_Age;
};
class SmartPoint {
public:
SmartPoint(Person *Person)
{
this->m_Person = Person;
}
//重载->
Person* operator->()
{
return this->m_Person;
}
//重载*
Person& operator*()
{
return *m_Person;
}
~SmartPoint()
{
if (this->m_Person)
{
delete this->m_Person;
this->m_Person = NULL;
}
}
private:
Person *m_Person;
};
void test01()
{
//Person *p1 = new Person(18);
//p1->showAge();
//(*p1).showAge();
//delete p1;
//利用智能指针 管理new出来的Person的释放操作
SmartPoint sp(new Person(18));
sp->showAge(); //本质sp->->showAge() 编译器优化sp->showAge()
(*sp).showAge();
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
六、等号运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//编译器默认给一个类添加四个函数 默认构造 析构函数 拷贝构造(值拷贝) operator=(值拷贝)
class Person {
public:
Person(const char* name, int age)
{
this->m_Name = new char[strlen(name) + 1];
strcpy(m_Name, name);
this->m_Age = age;
}
//重载=
Person& operator=(const Person& p)
{
if (this->m_Name != NULL)
{
delete [] this->m_Name;
this->m_Name = NULL;
}
this->m_Name = new char[strlen(p.m_Name) + 1];
strcpy(this->m_Name, p.m_Name);
this->m_Age = p.m_Age;
return *this;
}
~Person()
{
if (this->m_Name != NULL)
{
delete [] this->m_Name;
this->m_Name = NULL;
}
}
char* m_Name;
int m_Age;
};
void test01()
{
Person p1("Tom",18);
Person p2("Jerry",20);
Person p3("", 30);
p3 = p2 = p1;
cout << "姓名: " << p1.m_Name << " 年龄 = " << p2.m_Age << endl;
cout << "姓名: " << p2.m_Name << " 年龄 = " << p2.m_Age << endl;
cout << "姓名: " << p3.m_Name << " 年龄 = " << p2.m_Age << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
七、关系运算符重载
#define _CRT_SECURE_NO_WARNINGS
#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_Name == p.m_Name && this->m_Age == p.m_Age)
return true;
else
return false;
}
bool operator!=(Person& p)
{
return !(this->m_Name == p.m_Name && this->m_Age == p.m_Age);
}
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 EXIT_SUCCESS;
}
八、函数调用运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class MyPrint {
public:
void operator()(string test)
{
cout << test << endl;
}
};
void myPrint2(string str)
{
cout << str << endl;
}
void test01()
{
MyPrint myPrint;
myPrint("hello world"); //仿函数 本质是一个对象 函数对象
myPrint2("hello world"); //普通函数
}
class MyAdd {
public:
int operator()(int a,int b)
{
return a + b;
}
};
void test02()
{
//MyAdd myAdd;
//cout << myAdd(1, 2) << endl;
cout << MyAdd()(1, 2) << endl; //匿名函数对象 特点 :当前执行完立即释放
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
九、函数重载注意事项
不要重载&&和||