C++day4

1.定义一个Person类,私有成员int age,string &name,定义一个Stu类,包含私有成员double *score,写出两个类的构造函数、析构函数、拷贝构造和拷贝赋值函数,完成对Person的运算符重载(算术运算符、条件运算符、逻辑运算符、自增自减运算符、插入/提取运算符)

#include <iostream>
#include <cstring>
using namespace std;


class Person
{
    int age;
    string &name;
 public:


    Person(int age,string &name):name(name)
    {
        cout<<"P的构造函数"<<endl;
        this->age = age;
    }

    ~Person()
    {
        cout<<"P的析构函数"<<endl;
    }
  
    Person (const Person &other):age(other.age),name(other.name)
    {
        cout<<"P的拷贝构造函数"<<endl;
    }

    Person &operator = (const Person &other)
    {
        this->age = other.age;
        this->name = other.name;
        cout<<"P的拷贝赋值函数"<<endl;
        return *this;
    }

    Person operator+(const Person &other);
    Person operator-(const Person &other);
    Person operator*(const Person &other);
    Person operator/(const Person &other);
    Person operator%(const Person &other);
    friend Person operator+(Person &p1,Person &p2);
    friend Person operator-(Person &p1,Person &p2);
    friend Person operator*(Person &p1,Person &p2);
    friend Person operator/(Person &p1,Person &p2);
    friend Person operator%(Person &p1,Person &p2);
    /*------------------------------------*/

    bool operator>=(const Person &other);
    bool operator<=(const Person &other);
    bool operator==(const Person &other);

    bool operator&&(const Person &other);

    Person operator--(int);//成员函数后--
    friend Person operator++(Person &p);//全局前++


    friend ostream &operator<<(ostream &out,Person &p1);
    friend istream &operator>>(istream &out,Person &p1);
    void show();
};
void Person::show()
{
    cout<<age<<endl;
}
/*--------------------类外定义------------------*/
Person Person::operator+(const Person &other)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age+other.age;

    return temp;
}
Person Person::operator-(const Person &other)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age-other.age;

    return temp;
}
Person Person::operator*(const Person &other)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age*other.age;

    return temp;
}
Person Person::operator/(const Person &other)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age/other.age;

    return temp;
}
Person Person::operator%(const Person &other)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age%other.age;

    return temp;
}

bool Person::operator>=(const Person &other)
{
    if(this->age >= other.age)
    {
        return this->age >= other.age;
    }else
    {
        return this->age >= other.age;
    }
}
bool Person::operator<=(const Person &other)
{
    if(this->age <= other.age)
    {
        return this->age <= other.age;
    }else
    {
        return this->age <= other.age;
    }
}
bool Person::operator==(const Person &other)
{
   return this->age==other.age;;
}
//逻辑
bool Person::operator&&(const Person &other)
{
    return  (this->age) && other.age;
}
//全局 前自增
Person operator++(Person &p)
{
    ++p.age;
    return p;
}
//成员函数后自减
Person Person::operator--(int)
{
    string s = "a";
    Person temp(1,s);
    temp.age = this->age--;
    return temp;

}
//
ostream &operator<<(ostream &out,Person &p1)
{
    out<<"age="<<p1.age;
    return out;
}
istream &operator>>(istream &in,Person &p1)
{
    in>>p1.age;
    return in;
}
/*----------------全局运算符--------------------*/
Person operator+(Person &p1,Person &p2)
{
    string s = "a";
    Person temp(1,s);
    temp.age = p1.age+p2.age;
    return temp;
}
Person operator-(Person &p1,Person &p2)
{
    string s = "a";
    Person temp(1,s);
    temp.age = p1.age-p2.age;
    return temp;
}
Person operator*(Person &p1,Person &p2)
{
    string s = "a";
    Person temp(1,s);
    temp.age = p1.age*p2.age;
    return temp;
}
Person operator/(Person &p1,Person &p2)
{
    string s = "a";
    Person temp(1,s);
    temp.age = p1.age/p2.age;
    return temp;
}
Person operator%(Person &p1,Person &p2)
{
    string s = "a";
    Person temp(1,s);
    temp.age = p1.age%p2.age;
    return temp;
}
/*--------------------------------------------------------------*/
class  Stu
{
    double *score;
public:
    //构造函数
    Stu (double score):score(new double(score))
    {
        cout<<"Stu的构造函数"<<endl;
    }
    //析构函数
    ~Stu ()
    {
        delete score;
        cout<<"S的析构函数"<<endl;
    }
    //拷贝构造函数
    Stu (const Stu &other):score(other.score)
    {
        cout<<"S的构造函数"<<endl;
    }
    //拷贝赋值
    Stu &operator =(const Stu &other)
    {
        *(this->score)=*(other.score);
        cout<<"S的拷贝赋值"<<endl;
        return *this;
    }
    void show();
};
void Stu::show()
{
    cout<<*score<<endl;
}
int main()
{
    string cc="lls";
    string dd="sll";

    Person p1(18,cc);
    Person p2(12,dd);
    cout<<p1<<"" <<p2<<"lllll"<<endl;
    cin>>p1>>p2;
    p1.show();
    p2.show();
//    Person p3 = operator*(p1,p2);
//    p3 = p1.operator*(p2);
//    cout<<p1.operator&&(p2)<<endl;
//    Person p3(1,dd);
//    p3 = p1.operator--(3);
//    p3 = operator++(p1);
//    p1.show();
//    p3.show();
//    p1.show();
//    cout<<"---------------"<<endl;
//    Person p2=p1;
//    p2.show();
//    cout<<"---------------"<<endl;
//    Person p3(17,dd);
//    p3.show();
//    p3 = p2;
//    p3.show();
//    Stu s1(98.9);
//    s1.show();
//    cout<<"---------------"<<endl;
//    Stu s2(s1);
//    s2.show();
//    cout<<"---------------"<<endl;
//    Stu s3(60);
//    s3.show();
//    s3 = s2;
//    s3.show();

    return 0;
}

思维导图

相关推荐

  1. MSc CDA Take-Home

    2024-01-05 16:00:03       61 阅读
  2. CDA一级备考策略分享

    2024-01-05 16:00:03       32 阅读
  3. CDA-LevelⅡ【考题整理-带答案】

    2024-01-05 16:00:03       50 阅读
  4. Spring Data访问Elasticsearch----CDI集成

    2024-01-05 16:00:03       37 阅读
  5. CDA Level Ⅰ 2023认证考试大纲

    2024-01-05 16:00:03       79 阅读
  6. web server apache tomcat11-33-CDI

    2024-01-05 16:00:03       34 阅读
  7. Spring Data访问 MongoDB(十六)----CDI集成

    2024-01-05 16:00:03       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-05 16:00:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-05 16:00:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-05 16:00:03       87 阅读
  4. Python语言-面向对象

    2024-01-05 16:00:03       96 阅读

热门阅读

  1. Unity3D 如何实现多玩家语音聊天详解

    2024-01-05 16:00:03       64 阅读
  2. c++ move,可变参数模板,折叠表达式,...

    2024-01-05 16:00:03       54 阅读
  3. 见路不走1

    2024-01-05 16:00:03       56 阅读
  4. 聊聊PowerJob的DispatchStrategy

    2024-01-05 16:00:03       59 阅读
  5. Python 面向对象(3)

    2024-01-05 16:00:03       83 阅读
  6. 用redis广播消息更新集群环境下本地缓存

    2024-01-05 16:00:03       62 阅读
  7. TCL学习笔记(持续更新)

    2024-01-05 16:00:03       57 阅读
  8. Centos7.9和Debian12部署Minio详细流程

    2024-01-05 16:00:03       61 阅读
  9. UE5.1_AssetEditorSubsystem&UE4_AssetEditorManager

    2024-01-05 16:00:03       50 阅读