#include <iostream>
using namespace std;
class student
{
int age;
string name;
int score;
public:
void show()
{
cout << "age:" << age <<endl;
cout << "name:" << name <<endl;
cout << "score:" << score << endl;
}
//无参构造
student()
{
cout << "无参函数" << endl;
}
//有参构造
student(int age,string name,int score):age(age),name(name),score(score)
{
cout << "有参构造" << endl;
}
//析构函数
~student()
{
cout << "析构函数" << this << endl;
}
//拷贝构造函数
student(student &p):age(p.age),name(p.name),score(p.score)
{
cout << "拷贝构造函数" << endl;
}
//拷贝赋值函数
student &operator=(student &p)
{
this->age=p.age;
this->name=p.name;
this->score=p.score;
cout << "拷贝赋值" << endl;
return *this;
}
//运算符重载:+
student &operator+(student &p1)
{
static student p3;
p3.age=this->age+p1.age;
p3.name=this->name+p1.name;
p3.score=this->score+p1.score;
cout << "+运算符重载" << endl;
return p3;
}
//运算符重载:+=
student &operator+=(int sum)
{
this->age+=sum;
cout << "+= 运算符重载" << endl;
return *this;
}
//运算符重载:>
bool operator>(student &p)
{
cout << ">运算符重载" << endl;
return (this->age>p.age);
}
};
int main()
{
student p1(18,"zhangsan",90);
p1.show();
cout << "-----------------------------------"<<endl;
student p2=p1;
p2.show();
cout << "-----------------------------------"<<endl;
student p3;
p3=p1;
//p3.operator=(p1);
p3.show();
cout << "-----------------------------------"<<endl;
student p4=p1+p2;
p4.show();
cout << "-----------------------------------"<<endl;
p1+=5;
p1.show();
cout << "-----------------------------------"<<endl;
bool sum= p1>p2;
cout <<"条件运算符"<< sum << endl;
cout << "Hello World!" << endl;
return 0;
}