DAY3C++

2023-12-28 21:39:54
  1. 定义一个Person类,包含私有成员,int *age,string &name,一个Stu类,包含私有成员double *score,Person p1,写出Person类和Stu类的特殊成员函数,并写一个Stu的show函数,显示所有信息。
  2. #include <iostream>
    
    
    using namespace std;
    
    class Person
    {
        int *age;
        string &name;
    public:
        Person(int age,string &name):age(new int(age)),name(name)
        {
            cout<<"Person的有参构造"<<endl;
        }
        ~Person()
        {
            cout<<"P析构"<<endl;
            delete age;
        }
        int getp();
        string getps();
        int *gage()
        {
            return age;
        }
    };
    
    int Person::getp()
    {
        return *age;
    }
    string Person::getps()
    {
        return name;
    }
    class Stu
    {
        double *score;
        Person p1;
    public:
        void show();
        Stu (double a,int age,string name):score(new double(a)),p1(age,name)
        {
            cout<<"Stu有参构造"<<endl;
        }
        ~Stu()
        {
            cout<<"S析构"<<endl;
            delete  score;
        }
        Stu (const Stu&other):score(new double(*(other.score))),p1(other.p1)
        {
            cout<<"S拷贝"<<endl;
        }
    };
    
    void Stu::show()
    {
        cout<<"score"<<score<<endl;
        cout<<"*SCORE="<<*score<<endl;
        cout<<"age"<<p1.gage()<<endl;
        cout<<"*age"<<p1.getp()<<endl;
        cout<<"name="<<p1.getps()<<endl;
    }
    
    int main()
    {
        Stu s1(90.1,10,"zhangss");
        s1.show();
        cout<<"----------------------"<<endl;
        Stu s2(s1);
        s2.show();
        return 0;
    }
    
    2.思维导图

文章来源:https://blog.csdn.net/Aklkimg/article/details/135278062
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。