C++ 运算符重载
加号运算符:+ 重载
#include<iostream>
#include<string>
using namespace std;
class Person {
public:
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;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;
Person p4 = p1 + 20;
cout << "p3.m_A是:" << p3.m_A << "\np3.m_B是:" << p3.m_B << endl;
cout << "p4.m_A是:" << p4.m_A << "\np4.m_B是:" << p4.m_B << endl;
}
int main() {
test01();
return 0;
}
左移运算符:<< 重载
#include<iostream>
#include<string>
using namespace std;
class Person {
friend void test01();
friend ostream& operator<<(ostream& cout, Person p);
public:
private:
int m_A;
int m_B;
};
ostream& operator<<(ostream &cout,Person p) {
cout << "m_A=" << p.m_A << "\nm_B=" << p.m_B ;
return cout;
}
void test01()
{
Person p;
p.m_A = 10;
p.m_B = 10;
cout << p << endl;;
}
int main() {
test01();
system("pause");
return 0;
}
递增运算符:++ 重载
#include<iostream>
#include<string>
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) {
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 myint;
cout << ++myint << endl;
cout << myint << endl;
}
void test02() {
MyInteger myint;
cout << myint++ << endl;
cout << myint << endl;
}
int main() {
test02();
system("pause");
return 0;
}
关系运算符:== != 重载
#include<iostream>
#include<string>
using namespace std;
class Person {
public:
string m_Name;
int m_Age;
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;
}
return false;
}
bool operator!=(Person& p) {
if (this->m_Age == p.m_Age && this->m_Name == p.m_Name) {
return false;
}
return true;
}
};
void test01() {
Person p1("Tom", 18);
Person p2("Jerry", 18);
if (p1 == p2) {
cout << "p1和p2是相等的!" << endl;
}
else if (p1 != p2){
cout << "p1和p2是不相等的!" << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
赋值运算符:= 重载
#include<iostream>
#include<string>
using namespace std;
class Person {
public:
Person(int age) {
m_Age = new int(age);
}
~Person() {
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
}
Person& operator=(Person& p) {
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
m_Age = new int(*p.m_Age);
return *this;
}
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();
int a = 10;
int b = 20;
int c = 30;
c = b = a;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
system("pause");
return 0;
}
函数调用运算符:() 重载
#include<iostream>
#include<string>
using namespace std;
class MyPrint
{
public:
void operator()(string test) {
cout << test << endl;
}
};
void test01() {
MyPrint myprint;
myprint("Hello World");
}
class MyAdd {
public:
int operator()(int num1,int num2) {
return num1 + num2;
}
};
void test02() {
MyAdd myadd;
cout << "myadd的值是:" << myadd(202, 30) << endl;
cout << MyAdd()(100, 100) << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}