![](https://img-blog.csdnimg.cn/direct/6bee9d98bbbe4b1f868469dbdce6c622.png)
#include <iostream>
using namespace std;
class Per {
private:
string name;
unsigned int age;
unsigned int* height;
unsigned int* weight;
public:
Per() : name("undefined"), age(0), height(new unsigned int(0)), weight(new unsigned int(0)) {
cout << "Per()" << endl;
}
Per(const string& n, const unsigned int a, const unsigned int h, const unsigned int w)
: name(n), age(a), height(new unsigned int(h)), weight(new unsigned int(w)) {
cout << "Per(const string& n, ..." << endl;
}
~Per() {
cout << "~Per()" << endl;
delete height;
delete weight;
}
friend ostream& operator<<(ostream& os, const Per& p) {
os << p.name << " " << p.age << " " << *(p.height) << " " << *(p.weight);
return os;
}
};
class Stu {
private:
Per p1;
double score;
public:
Stu() : score(0) { cout << "Stu()" << endl; };
Stu(const string& n,
const unsigned int a,
const unsigned int h,
const unsigned int w,
const double s)
: p1(n, a, h, w), score(s) {
cout << "Stu(const string& n, ..." << endl;
}
Stu(const Per& p, double s) : p1(p), score(s) { cout << "Stu(const Per& p, double s)" << endl; }
~Stu() { cout << "~Stu()" << endl; }
friend ostream& operator<<(ostream& os, const Stu& s) {
os << s.p1 << " " << s.score;
return os;
}
};
int main() {
Stu s1;
cout << "========================" << endl;
Stu s2("lucy", 18, 160, 50, 99);
cout << s2 << endl;
cout << "========================" << endl;
}
![](https://img-blog.csdnimg.cn/direct/122a9964120b4d5c85b82ed7e33b247d.png)