- 定义一个Person类,包含私有成员,int *age,string &name,一个Stu类,包含私有成员double *score,Person p1,写出Person类和Stu类的特殊成员函数,并写一个Stu的show函数,显示所有信息。
-
2.思维导图#include <iostream> using namespace std; class Person { int *age; string &name; public: Person(int age,string &name):age(new int(age)),name(name) { cout<<"Person的有参构造"<<endl; } ~Person() { cout<<"P析构"<<endl; delete age; } int getp(); string getps(); int *gage() { return age; } }; int Person::getp() { return *age; } string Person::getps() { return name; } class Stu { double *score; Person p1; public: void show(); Stu (double a,int age,string name):score(new double(a)),p1(age,name) { cout<<"Stu有参构造"<<endl; } ~Stu() { cout<<"S析构"<<endl; delete score; } Stu (const Stu&other):score(new double(*(other.score))),p1(other.p1) { cout<<"S拷贝"<<endl; } }; void Stu::show() { cout<<"score"<<score<<endl; cout<<"*SCORE="<<*score<<endl; cout<<"age"<<p1.gage()<<endl; cout<<"*age"<<p1.getp()<<endl; cout<<"name="<<p1.getps()<<endl; } int main() { Stu s1(90.1,10,"zhangss"); s1.show(); cout<<"----------------------"<<endl; Stu s2(s1); s2.show(); return 0; }
DAY3C++
于 2023-12-28 20:50:18 首次发布
本文介绍了如何在C++中定义Person和Stu类,包括私有成员变量age和name,以及score。创建了构造函数、析构函数和特殊成员函数如getp和getps,以及Stu类的show函数用于显示对象信息。
摘要由CSDN通过智能技术生成