思维导图
练习题
cpp
#include <iostream>
using namespace std;
class Per
{
private:
string name;
int age;
int *height;
double weight;
public:
Per()
{
cout << "Per::无参构造函数" << endl;
}
Per(string name, int age, int height, double weight) : name(name), age(age), height(new int(height)), weight(weight)
{
cout << "Per::有参构造函数" << endl;
}
~Per()
{
cout << "Per::析构函数" << endl;
delete height;
height = nullptr;
}
};
class Stu
{
private:
double score;
Per p1;
public:
Stu()
{
cout << "Stu::无参构造函数" << endl;
}
Stu(double score, string name, int age, int height, double weight) : score(score), p1(name, age, height, weight)
{
cout << "Stu::有参构造函数" << endl;
}
~Stu()
{
cout << "Stu::析构函数" << endl;
}
void show()
{
cout << score << endl;
}
};
int main(int argc, char const *argv[])
{
Stu p(98, "葛飞飞", 23, 179, 86);
p.show();
return 0;
}