思维导图

深拷贝和浅拷贝
运算符重载
#include <iostream>
using namespace std;
class Person
{
private:
int age;
int weight;
public:
Person():age(18),weight(100){}
Person(int age,int weight)
{
this->age = age;
this->weight = weight;
}
void show()
{
cout << "age = " << age << " weight = " << weight << endl;
}
Person operator =(const Person &a)
{
Person temp;
temp.age = a.age;
temp.weight = a.weight;
return temp;
}
Person &operator +=(const Person &a)
{
this->age = this->age + a.age;
this->weight = this->weight + a.weight;
cout << "******* += *******" << endl;
return *this;
}
Person &operator -=(const Person &a)
{
this->age = this->age - a.age;
this->weight = this->weight - a.weight;
cout << "******* -= *******" << endl;
return *this;
}
Person &operator *=(const Person &a)
{
this->age = this->age * a.age;
this->weight = this->weight * a.weight;
cout << "******* *= *******" << endl;
return *this;
}
Person &operator /=(const Person &a)
{
this->age = this->age / a.age;
this->weight = this->weight / a.weight;
cout << "******* /= *******" << endl;
return *this;
}
Person &operator %=(const Person &a)
{
this->age = this->age % a.age;
this->weight = this->weight % a.weight;
cout << "******* %= *******" << endl;
return *this;
}
bool operator &&(Person &a)
{
cout << "******* && *******" << endl;
return this->weight && a.weight;
}
bool operator ||(Person &a)
{
cout << "******* || *******" << endl;
return this->weight || a.weight;
}
bool operator <(Person &p2)
{
cout << "******* < *******" << endl;
return this->weight < p2.weight;
}
bool operator <=(Person &p2)
{
cout << "******* <= *******" << endl;
return this->weight <= p2.weight;
}
bool operator >(Person &p2)
{
cout << "******* > *******" << endl;
return this->weight > p2.weight;
}
bool operator >=(Person &p2)
{
cout << "******* >= *******" << endl;
return this->weight >= p2.weight;
}
bool operator ==(Person &a)
{
cout << "******* == *******" << endl;
return this->weight == a.weight;
}
bool operator !=(Person &p2)
{
cout << "******* != *******" << endl;
return this->weight != p2.weight;
}
Person operator *(Person &a)
{
Person temp;
temp.age = this->age * a.age;
temp.weight = this->weight * a.weight;
cout << "******* * *******" << endl;
return temp;
}
Person operator /(Person &a)
{
Person temp;
temp.age = this->age / a.age;
temp.weight = this->weight / a.weight;
cout << "******* / *******" << endl;
return temp;
}
Person operator %(Person &a)
{
Person temp;
temp.age = this->age % a.age;
temp.weight = this->weight % a.weight;
cout << "******* % *******" << endl;
return temp;
}
Person operator +(Person &a)
{
Person temp;
temp.age = this->age + a.age;
temp.weight = this->weight + a.weight;
cout << "******* + *******" << endl;
return temp;
}
Person operator -(Person &a)
{
Person temp;
temp.age = this->age - a.age;
temp.weight = this->weight - a.weight;
cout << "******* - *******" << endl;
return temp;
}
bool operator !()
{
cout << "******* ! *******" << endl;
return !this->weight;
}
Person operator ~()
{
Person temp;
temp.age = ~this->age;
temp.weight = ~this->weight;
cout << "******* ~ *******" << endl;
return temp;
}
Person &operator ++()
{
this->age++;
this->weight++;
cout << "******* 前++ *******" << endl;
return *this;
}
Person &operator ++(int)
{
static Person temp;
temp.age = this->age++;
temp.weight = this->weight++;
cout << "******* 后++ *******" << endl;
return temp;
}
Person &operator --()
{
this->age--;
this->weight--;
cout << "******* 前-- *******" << endl;
return *this;
}
Person &operator --(int)
{
static Person temp;
temp.age = this->age--;
temp.weight = this->weight--;
cout << "******* 后-- *******" << endl;
return temp;
}
Person operator -()
{
static Person temp;
temp.age = -this->age;
temp.weight = -this->weight;
cout << "******* - *******" << endl;
return temp;
}
};
int main()
{
Person p1(18,100);
Person p2(20,150);
Person p3;
cout << (p1<=p2) << endl;
cout << endl;
cout << (!p1) << endl;
cout << endl;
p3 = ~p1;
p3.show();
cout << endl;
cout << "自加前: ";
p1.show();
p3 = p1++;
cout << "自加后: ";
p1.show();
p3.show();
cout << endl;
cout << "自加前: ";
p1.show();
p3 = ++p1;
cout << "自加后: ";
p1.show();
p3.show();
cout << endl;
return 0;
}
