c++ day4

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

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

ostream &operator<<(ostream &out, Person &c1)
{
    out << c1.age << " " << c1.name;
    return out;
}

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;

相关推荐

  1. MSc CDA Take-Home

    2024-01-02 11:54:04       60 阅读
  2. CDA一级备考策略分享

    2024-01-02 11:54:04       32 阅读
  3. CDA-LevelⅡ【考题整理-带答案】

    2024-01-02 11:54:04       50 阅读
  4. Spring Data访问Elasticsearch----CDI集成

    2024-01-02 11:54:04       36 阅读
  5. CDA Level Ⅰ 2023认证考试大纲

    2024-01-02 11:54:04       78 阅读
  6. web server apache tomcat11-33-CDI

    2024-01-02 11:54:04       33 阅读
  7. Spring Data访问 MongoDB(十六)----CDI集成

    2024-01-02 11:54:04       41 阅读

最近更新

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

    2024-01-02 11:54:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-02 11:54:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-02 11:54:04       82 阅读
  4. Python语言-面向对象

    2024-01-02 11:54:04       91 阅读

热门阅读

  1. 微信小程序canvas手写签字

    2024-01-02 11:54:04       52 阅读
  2. C:宏:编程风格:井号与define之间的空格

    2024-01-02 11:54:04       58 阅读
  3. 将jupyter转换为python文件

    2024-01-02 11:54:04       58 阅读
  4. LeetCode 20. 有效的括号

    2024-01-02 11:54:04       56 阅读
  5. Android 10.0 截屏流程

    2024-01-02 11:54:04       47 阅读
  6. Atlas Hook 导入 Hive 元数据

    2024-01-02 11:54:04       52 阅读
  7. KVM虚拟机部署K8S重启后/etc/hosts内容丢失

    2024-01-02 11:54:04       57 阅读
  8. 深入理解@Resource与@Autowired:用法与区别解析

    2024-01-02 11:54:04       54 阅读
  9. JDK下载地址

    2024-01-02 11:54:04       69 阅读