C++ day4

目录

思维导图

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


思维导图

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

cpp 复制代码
#include <iostream>

using namespace std;
class Person
{
    int age;
    string &name;
public:

    Person(int age,string &name):age(age),name(name){cout << "Person的构造函数" << endl;}
    ~Person(){cout << "Person的析构函数"<< endl;}
    Person(const Person &other):age(other.age),name(other.name)
    {
        cout << "Person的拷贝构造函数" << endl;
    }
    Person &operator=(const Person &other)
    {
        this->age=other.age;
        this->name=other.name;
        cout << "Person的拷贝赋值函数" << endl;
        return *this;
    }

    Person operator+(Person &other);
    Person operator-(Person &other);
    Person operator*(Person &other);
    Person operator/(Person &other);
    friend Person operator%(Person &c1,Person &c2);

    friend bool operator>(Person &c1,Person &c2);
    friend bool operator<(Person &c1,Person &c2);
    friend bool operator==(Person &c1,Person &c2);
    friend bool operator>=(Person &c1,Person &c2);
    friend bool operator<=(Person &c1,Person &c2);
    friend bool operator!=(Person &c1,Person &c2);
    friend bool operator&&(Person &c1,Person &c2);
    friend bool operator||(Person &c1,Person &c2);
    void operator()();
    friend Person operator++(Person &c1);
    friend Person operator--(Person &c1);
    Person operator++(int);
    Person operator--(int);
    friend ostream &operator<<(ostream &out,Person &c1);
    friend istream &operator>>(istream &out,Person &c1);
};
string n="";
Person temp(0,n);
//+运算符重载
Person Person::operator+(Person &other)
{
    temp.age = this->age+other.age;
    temp.name=this->name;
    return temp;
}
//-运算符重载
Person Person::operator-(Person &other)
{
    temp.age = this->age-other.age;
    temp.name=this->name;
    return temp;
}
//*运算符重载
Person Person::operator*(Person &other)
{
    temp.age = this->age*other.age;
    temp.name=this->name;
    return temp;
}
//  /运算符重载
Person Person::operator/(Person &other)
{
    temp.age = this->age/other.age;
    temp.name=this->name;
    return temp;
}
//%运算符重载
Person operator%(Person &c1,Person &c2)
{
      temp.age=c1.age%c2.age;
      temp.name=c1.name;
    return temp;
}
//>运算符重载
bool operator>(Person &c1,Person &c2)
{
    return c1.age>c2.age;
}
//<运算符重载
bool operator<(Person &c1,Person &c2)
{
    return c1.age<c2.age;
}
//==运算符重载
bool operator==(Person &c1,Person &c2)
{
    return c1.age==c2.age;
}
//>=运算符重载
bool operator>=(Person &c1,Person &c2)
{
    return c1.age>=c2.age;
}
//<=运算符重载
bool operator<=(Person &c1,Person &c2)
{
    return c1.age<=c2.age;
}
//!=运算符重载
bool operator!=(Person &c1,Person &c2)
{
    return c1.age!=c2.age;
}
//&&运算符重载
bool operator&&(Person &c1,Person &c2)
{
    return c1.age&&c2.age;
}
//||运算符重载
bool operator||(Person &c1,Person &c2)
{
    return c1.age||c2.age;
}
//()运算符重载
void Person::operator()()
{
    cout << "hello" << endl;
}
//前自增
Person operator++(Person &c1)
{
    ++(c1.age);
    return c1;
}
//前自减
Person operator--(Person &c1)
{
    --(c1.age);
    return c1;
}
//后自增
Person Person::operator++(int)
{
    temp.age=this->age++;
    temp.name=this->name;
    return temp;
}
//后自减
Person Person::operator--(int)
{
    temp.age=this->age--;
    temp.name=this->name;
    return temp;
}
//cout运算符重载
ostream &operator<<(ostream &out,Person &c1)
{
    out << c1.age << " " << c1.name;
    return out;
}
//cin运算符重载
istream &operator>>(istream &in,Person &c1)
{
    in >> c1.age;
    in >> c1.name;
    return in;
}

class Stu
{
    double *score;
public:
    Stu(double score):score(new double(score)){cout << "Stu的构造函数" << endl;}
    ~Stu()
    {
        delete score;
        cout << "Stu的析构函数"<< endl;
    }
    Stu(const Stu &other):score(new double(*(other.score)))
    {
        cout << "Stu的拷贝构造函数" << endl;
    }
    Stu &operator=(const Stu &other)
    {
        *(this->score)=*(other.score);
        cout << "Stu的拷贝赋值函数" << endl;
        return *this;
    }
};
int main()
{
    string name="zhangsan";
    Person s1(12,name);
    return 0;
}
相关推荐
hopetomorrow9 分钟前
学习路之PHP--使用GROUP BY 发生错误 SELECT list is not in GROUP BY clause .......... 解决
开发语言·学习·php
小牛itbull19 分钟前
ReactPress vs VuePress vs WordPress
开发语言·javascript·reactpress
怀澈12221 分钟前
高性能服务器模型之Reactor(单线程版本)
linux·服务器·网络·c++
请叫我欧皇i27 分钟前
html本地离线引入vant和vue2(详细步骤)
开发语言·前端·javascript
闲暇部落30 分钟前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
GIS瞧葩菜39 分钟前
局部修改3dtiles子模型的位置。
开发语言·javascript·ecmascript
chnming198744 分钟前
STL关联式容器之set
开发语言·c++
威桑1 小时前
MinGW 与 MSVC 的区别与联系及相关特性分析
c++·mingw·msvc
熬夜学编程的小王1 小时前
【C++篇】深度解析 C++ List 容器:底层设计与实现揭秘
开发语言·数据结构·c++·stl·list
yigan_Eins1 小时前
【数论】莫比乌斯函数及其反演
c++·经验分享·算法