代码练习
#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;
}
效果图
![](https://i-blog.csdnimg.cn/blog_migrate/9db9d92c52cee0eb29e89dc313602a6d.png)
思维导图
![](https://i-blog.csdnimg.cn/blog_migrate/cefc6d1204232da8b894629dbebeedbf.png)