主页 > 游戏开发  > 

c++day3

c++day3
#include <iostream> using namespace std; class Per { private: string name; int age; int *hight; int *weight; public: Per() { cout << "无参构造函数" << endl; } Per(string name, int age, int *hight, int *weight):name(name),age(age),hight(new int(*hight)),weight(new int(*weight)) { cout << "有参构造函数" << endl; } Per(const Per &other):name(other.name),age(other.age),hight(new int(*(other.hight))),weight(new int(*(other.weight))) { cout << "拷贝构造函数" << endl; } ~Per() { delete hight; delete weight; cout << "析构函数" << endl; } void show() { cout << "name: " << name << '\t'; cout << "age: " << age << '\t'; cout << "hight: " << *hight << '\t'; cout << "weight: " << *weight << '\t'; } }; class Stu { private: float score; Per p1; public: Stu() { cout << "无参构造函数" << endl; } Stu(float score, Per p1):score(score),p1(p1) { cout << "有参构造函数" << endl; } Stu(const Stu &other):score(other.score),p1(other.p1) { cout << "拷贝构造函数" << endl; } ~Stu() { cout << "析构函数" << endl; } void show() { p1.show(); cout << "score: " << score << endl; } }; int main() { int w = 70; int h = 178; Per p1("张三", 18, &h, &w); Stu s1(95, p1); s1.show(); Per p2(p1); Stu s2(s1); s2.show(); return 0; }

标签:

c++day3由讯客互联游戏开发栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“c++day3