C++ DAY4

1.思维导图

2.运算符重载

#include <iostream>
using namespace std;
 
class Person
{
private:
    int age;
    int *p;
public:
    //1.无参构造
    Person():p(new int(89))
    {
        age = 18;
    }
    //2.有参构造
    Person(int age,int num)
    {
        this->age = age;
        this->p=new int(num);
    }
    //3.拷贝构造函数
    Person(Person &other)
    {
        age=other.age;
        //浅拷贝
//        p=other.p;
        //深拷贝
        p=new int;
        *p=*(other.p);
        cout << "Person拷贝构造函数" << endl;
 
    }
    //4.拷贝赋值函数
    Person &operator=(Person &other)
       {
           if(this!=&other)
           {
               age = other.age;
               //浅拷贝
               //point = other.point;
               //深拷贝
               *p = *(other.p);
               cout << "Person拷贝赋值函数" << endl;
           }
           return *this;   //返回类对象本身
       }
    //5.析构函数
    ~Person()
    {
        delete p;
        p=nullptr;
        cout << "释放那个了p指向的空间" << endl;
        cout << "Person的析构函数" << endl;
    }
    void show()
    {
        cout<<"age="<<age<<endl;
        cout<<"*p="<<*p<<endl;
        cout<<"p="<<p<<endl;
    }


    //前自增
    Person &operator++(){
        this->age++;
        *(this->p)++;
        return *this;
    }
    //后自增
    Person operator++(int){
        Person temp;
        temp=*this;
        this->age++;
        *(this->p)++;
        return temp;
    }
    //前自减
    Person &operator--(){
        this->age--;
        *(this->p)--;
        return *this;
    }
    //后自减
    Person operator--(int){
        Person temp;
        temp=*this;
        this->age--;
        *(this->p)--;
        return temp;
    }
    //逻辑非!重载
    Person &operator!(){
        this->age=!this->age;
        *(this->p)=!*(this->p);
        return *this;
    }
    //按位取反~重载
    Person &operator~(){
        this->age=~this->age;
        *(this->p)=~*(this->p);
        return *this;
    }
    //负号-重载
    Person &operator-(){
        this->age=-this->age;
        *(this->p)=-*(this->p);
        return *this;
    }
    //乘法重载
    Person operator*(Person &per){
        Person temp;
        temp.age=this->age*per.age;
        *(temp.p)=*(this->p)*(*(per.p));
        return temp;
    }
    //除法重载
    Person operator/(Person &per){
        Person temp;
        temp.age=this->age/per.age;
        *(temp.p)=*(this->p)/(*(per.p));
        return temp;
    }
    //求余重载
    Person operator%(Person &per){
        Person temp;
        temp.age=this->age%per.age;
        *(temp.p)=*(this->p)%(*(per.p));
        return temp;
    }
    //加法重载
    Person operator+(Person &per){
        Person temp;
        temp.age=this->age+per.age;
        *(temp.p)=*(this->p)+(*(per.p));
        return temp;
    }
    //减法重载
    Person operator-(Person &per){
        Person temp;
        temp.age=this->age-per.age;
        *(temp.p)=*(this->p)-(*(per.p));
        return temp;
    }
    //小于重载
    bool operator<(Person &per){ 
        return this->age<per.age;
    }
    //小于等于重载
    bool operator<=(Person &per){ 
        return this->age<=per.age;
    }
    //大于重载
    bool operator>(Person &per){ 
        return this->age>per.age;
    }
    //大于等于重载
    bool operator>=(Person &per){ 
        return this->age>=per.age;
    }
    //等于重载
    bool operator==(Person &per){ 
        return this->age==per.age;
    }
    //不等于重载
    bool operator!=(Person &per){ 
        return this->age!=per.age;
    }
    //逻辑与重载
    bool operator&&(Person &per){
        return (this->age||*(this->p))&&(per.age||*(per.p));
    }
    //逻辑或重载
    bool operator||(Person &per){
        return (this->age||*(this->p))||(per.age||*(per.p));
    }
    //+=重载
    Person &operator+=(Person &per){
        this->age+=per.age;
        *(this->p)+=*(per.p);
        return *this;
    }
    //-=重载
    Person &operator-=(Person &per){
        this->age-=per.age;
        *(this->p)-=*(per.p);
        return *this;
    }
    //*=重载
    Person &operator*=(Person &per){
        this->age*=per.age;
        *(this->p)*=(*(per.p));
        return *this;
    }
    ///=重载
    Person &operator/=(Person &per){
        this->age/=per.age;
        *(this->p)/=(*(per.p));
        return *this;
    }
    //%=重载
    Person &operator%=(Person &per){
        this->age%=per.age;
        *(this->p)%=(*(per.p));
        return *this;
    }

 
    //输出重载
    ostream &operator<<(ostream &out, Person &per)
    {
        out << per.age << endl;
        out << *(per.p) << endl;
        return out;
    }    
    //输入重载
    istream &operator>>(istream &in, Person &per)
    {
        in >> per.age >> *(per.p);
        return in;
    }  

};
 
int main()
{
    Person k1;
    cout<<"k1:"<<endl;
    k1.show();
    cout<<"-----------------------------"<<endl;
    Person k2(20,99);
    cout<<"k2:"<<endl;
    k2.show();
    cout<<"-----------------------------"<<endl;
    cout<<"---------拷贝构造函数----------"<<endl;
    Person k3=k2;
    cout<<"k3:"<<endl;
    k3.show();
    cout<<"-----------------------------"<<endl;
    cout<<"---------拷贝赋值函数----------"<<endl;
    Person k4;
    k4=k2;
    cout<<"k4:"<<endl;
    k4.show();
 
    cout<<"-----------------------------"<<endl<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值