C++LIANXI

 

 

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值