C++ DAY3 作业

1.定义一个Person类,包含私有成员,int *age,string &name,一个Stu类,包含私有成员double *score,Person p1,写出Person类和Stu类的特殊成员函数,并写一个Stu的show函数,显示所有信息。

#include <iostream>

using namespace std;

class Person
{
    int *age;
    string &name;  //存在引用成员
public:
    //构造函数
    Person(string name):age(new int),name(name)
    {
        cout << "Person一个参数的有参构造" << endl;
    }
    Person(int age,string name):age(new int(age)),name(name)
    {
        cout << "Person两个参数的有参构造" << endl;
    }
    //析构函数
    ~Person(){
        delete age;
        cout << "Person的析构函数,释放空间:" << age << endl;
    }
    //拷贝构造函数
    Person(const Person&other):age(new int(*(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;
    }
    int get_age();
    string get_name();
};
int Person::get_age()
{
    return *age;
}
string Person::get_name()
{
    return name;
}

class Stu
{
    double *score;
    Person p1;
public:
    //构造函数
    Stu():score(new double),p1(p1)
    {cout << "Stu的无参构造" << endl;}
    Stu(double score):score(new double(score)),p1(p1)
    {cout << "Stu的一个参数有参构造" << endl;}
    Stu(double score,Person p1):score(new double(score)),p1(p1)
    {cout << "Stu的两个参数有参构造" << endl;}
    //析构函数
    ~Stu(){
        delete score;
        cout << "Stu的析构函数,释放空间:" << score << endl;
    }
    //拷贝构造函数
    Stu(const Stu &other):score(new double(*(other.score))),p1(other.p1)
    {
        cout << "Stu的拷贝构造函数" << endl;
    }
    //拷贝赋值函数
    Stu &operator =(const Stu &other)
    {
        *(this->score) = *(other.score);
        this->p1 = other.p1;
        cout << "Stu的拷贝赋值函数" << endl;
        return *this;
    }
    void show(Person p1);
};
void Stu::show(Person p1)
{
    cout << "age:" << p1.get_age() << endl;
    cout << "name:" << p1.get_name() << endl;
    cout << "score:" << *score << endl;
}


int main()
{
    Person p1(18,"zhangsan");
    //Stu s1;
   Stu s2(50);
    s2.show(p1);
//    Person p2("lisi");
//    p2 = p1;
    //Person p2 = p1;

    return 0;
}

2.思维导图

相关推荐

  1. QT<span style='color:red;'>作业</span><span style='color:red;'>3</span>

    QT作业3

    2023-12-29 01:54:05      37 阅读
  2. 2.<span style='color:red;'>3</span><span style='color:red;'>作业</span>

    2.3作业

    2023-12-29 01:54:05      27 阅读
  3. QT<span style='color:red;'>3</span><span style='color:red;'>作业</span>

    QT3作业

    2023-12-29 01:54:05      25 阅读
  4. <span style='color:red;'>3</span>.6<span style='color:red;'>作业</span>

    3.6作业

    2023-12-29 01:54:05      27 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-29 01:54:05       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-29 01:54:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-29 01:54:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-29 01:54:05       20 阅读

热门阅读

  1. 【c++】二分查找教程

    2023-12-29 01:54:05       38 阅读
  2. 力扣:738. 单调递增的数字(贪心)

    2023-12-29 01:54:05       33 阅读
  3. 【zookeeper分布式锁】

    2023-12-29 01:54:05       20 阅读
  4. USACO08FEB Hotel G

    2023-12-29 01:54:05       34 阅读
  5. C语言初学8:函数和作用域

    2023-12-29 01:54:05       30 阅读
  6. 深入理解技术内容运营

    2023-12-29 01:54:05       33 阅读
  7. 米贸搜|LinkedIn和Facebook在营销上有哪些区别?

    2023-12-29 01:54:05       30 阅读