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;
}
相关推荐
唐诺2 分钟前
几种广泛使用的 C++ 编译器
c++·编译器
高山我梦口香糖1 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
冷眼看人间恩怨1 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
信号处理学渣1 小时前
matlab画图,选择性显示legend标签
开发语言·matlab
红龙创客1 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
Lenyiin1 小时前
第146场双周赛:统计符合条件长度为3的子数组数目、统计异或值为给定值的路径数目、判断网格图能否被切割成块、唯一中间众数子序列 Ⅰ
c++·算法·leetcode·周赛·lenyiin
jasmine s1 小时前
Pandas
开发语言·python
biomooc2 小时前
R 语言 | 绘图的文字格式(绘制上标、下标、斜体、文字标注等)
开发语言·r语言
骇客野人2 小时前
【JAVA】JAVA接口公共返回体ResponseData封装
java·开发语言
black^sugar2 小时前
纯前端实现更新检测
开发语言·前端·javascript