#include <iostream>
using namespace std;
class Person
{
private:
int age;
int *p;
public:
//无参构造
Person():p(new int(89))
{
age = 18;
cout<<"无参构造完成"<<endl;
}
//有参构造
Person(int age,int num)
{
this->age = age;
p=new int(num);
cout<<"有参构造完成"<<endl;
}
//拷贝构造函数
Person (Person &other)
{
this->age=other.age;
p=new int(*(other.p));
cout<<"拷贝构造完成"<<endl;
}
//拷贝赋值函数
Person &operator=(const Person &other)
{
if(this!=&other)
{
this->age = other.age;
*p = *(other.p);
}
cout<<"拷贝赋值完成"<<endl;
return *this;
}
//析构函数
~Person()
{
delete p;
p = nullptr;
cout<<"析构完成"<<endl;
}
friend Person operator +(Person &P3,Person &P6);
friend Person operator -(Person &P3,Person &P6);
friend Person operator *(Person &P3,Person &P6);
friend Person operator /(Person &P3,Person &P6);
friend Person operator %(Person &P3,Person &P6);
//<,<=,>,>=,==,!=成员函数版
bool operator < (Person &P6)
{
if (this->age < P6.age)
return this->age < P6.age;
else
return this->age >= P6.age;
}
bool operator <= (Person &P6)
{
if (this->age <= P6.age)
return this->age <= P6.age;
else
return this->age > P6.age;
}
bool operator > (Person &P6)
{
if (this->age > P6.age)
return this->age > P6.age;
else
return 0;
}
bool operator >= (Person &P6)
{
if (this->age >= P6.age)
return this->age >= P6.age;
else
return 0;
}
bool operator == (Person &P6)
{
if (this->age == P6.age)
return this->age == P6.age;
else
return 0;
}
bool operator != (Person &P6)
{
if (this->age != P6.age)
return this->age != P6.age;
else
return 0;
}
bool operator && (Person &P6)
{
if (this->age && P6.age )
return 1;
else
return 0;
}
bool operator || (Person &P6)
{
if (this->age || P6.age )
return 1;
else
return 0;
}
//!,~,-,++,--成员函数版
// Person operator =(Person &P6)
// {
// this->age = P6.age;
// }
Person &operator +=(Person &P6)
{
this->age += P6.age;
}
Person &operator -=(Person &P6)
{
this->age -=P6.age;
}
Person &operator *=(Person &P6)
{
this->age *= P6.age;
}
Person &operator /=(Person &P6)
{
this->age /= P6.age;
}
Person &operator %=(Person &P6)
{
this->age %= P6.age;
}
Person &operator !()
{
!this->age;
}
Person &operator ~()
{
~this->age;
}
// Person &operator -()
// {
// -(this->age);
// }
//a++ 需要哑元
Person &operator ++(int)
{ static Person temp;
temp.age = this->age;
(this->age)++;
return temp;
}
//++a
Person &operator ++()
{
++this->age;
return *this;
}
//a-- 需要哑元
Person &operator --(int)
{ static Person temp;
temp.age = this->age;
this->age--;
return temp;
}
//--a
Person &operator --()
{
--this->age;
return *this;
}
void show();
void show1();
};
//Person fun(Person other)
//{
//}
void Person::show()
{
cout<<"输出的结果:"<<age<<endl;
}
void Person::show1()
{
cout<<"自增前:"<<age<<endl;
}
//+-*/用全局版的运算符重载
Person operator +(Person &P3,Person &P6)
{
Person temp;
temp.age = P3.age+P6.age;
return temp;
}
Person operator -(Person &P3,Person &P6)
{
Person temp;
temp.age = P3.age-P6.age;
return temp;
}
Person operator *(Person &P3,Person &P6)
{
Person temp;
temp.age = P3.age*P6.age;
return temp;
}
Person operator /(Person &P3, Person &P6)
{
Person temp;
temp.age = P3.age/P6.age;
return temp;
}
Person operator %(Person &P3, Person &P6)
{
Person temp;
temp.age = P3.age%P6.age;
return temp;
}
int main()
{
int num=90;
// Person p1;
// Person p2(14,num);
// fun(p2);
// Person p4;
// p4 = p2;
Person P3(20,num);
Person P6(60,num);
cout<<"P3:"<<20<<'\t'<<"P6:"<<60<<endl;
Person p5;
p5 = P3+P6;
p5.show();
p5 = P3-P6;
p5.show();
p5 = P3*P6;
p5.show();
p5 = P3/P6;
p5.show();
p5 = P3%P6;
p5.show();
bool res = P3 < P6;
cout<<res<<endl;
res = P3 <= P6;
cout<<res<<endl;
res = P3 > P6;
cout<<res<<endl;
res = P3 >=P6;
cout<<res<<endl;
res = P3 == P6;
cout<<res<<endl;
res = P3 != P6;
cout<<res<<endl;
res = P3 && P6;
cout<<res<<endl;
res = P3 || P6;
cout<<res<<endl;
// P3 = P6;
// P3.show();
P3 += P6;
P3.show();
P3 -= P6;
P3.show();
P3 *= P6;
P3.show();
P3 /= P6;
P3.show();
P3 %= P6;
P3.show();
(!P6).show();
~P6;
P6.show();
// -P6;
// P6.show();
P6.show1();
P6++;
P6.show();
P6.show1();
(++P6).show();
P6.show1();
(P6--).show();
P6.show1();
(--P6).show();
return 0;
}