欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

学习 C++ - 代码练习

最编程 2024-03-14 15:19:29
...
#include <iostream>

using namespace std;
class Per
{
private:
    string name;
    int age;
    double *hight;
    double *weight;
public:
    Per()
    {
        cout << "Per的无参构造函数" << endl;
    }
    Per(string name,int age,double *hight,double *weight):name(name),age(age),hight(hight),weight(weight)
    {
        cout << "Per的有参构造函数" << endl;
    }

    Per(const Per &p1):name(p1.name),age(p1.age),hight(p1.hight),weight(p1.weight)
    {
        cout << "Per的拷贝构造函数" << endl;
    }

    ~Per()
    {
        cout << "Per的析构函数" << endl;
    }

};

class Stu
{
private:
    int score;
    Per p;
public:
    Stu()
    {
        cout << "Stu的无参构造函数" << endl;
    }
    Stu(int score,string name,int age,double *hight,double *weight):score(score),p(name,age,weight,hight)
    {
        cout << "Stu的有参构造函数" << endl;
    }

    Stu(const Stu &s1):score(s1.score),p(s1.p)
    {
        cout << "Stu的拷贝构造函数" << endl;
    }

    ~Stu()
    {
        cout << "Stu的析构函数" << endl;
    }
};
int main()
{
    Per p1;
    cout << "===================" << endl;
    double hight;
    double weight;
    cin >> hight;
    cin >> weight;
    double *h = &hight;
    double *w = &weight;

    Per p2("慕容二狗", 18, h , w );
    cout << "===================" << endl;

    Per p3(p2);
    cout << "===================" << endl;

    Stu s1;
    cout << "===================" << endl;
    Stu s2(99,"慕容二狗",18, h , w);
    cout << "===================" << endl;
    Stu s3(s2);
    cout << "===================" << endl;





    return 0;
}

效果图