#include #include using namespace std;

class Student{ private: char* pname; int score; public: Student():score(0){ pname=new char[5]; strcpy(pname,"None"); cout<<"construct None"<<endl; } Student(char* name,int sc):score(sc){ pname=new char[strlen(name)+1]; strcpy(pname,name); cout<<"construct "<<name<<endl; } Student(const Student& s){ pname=new char[strlen(s.pname)+1]; strcpy(pname,s.pname); score=s.score; cout<<"copy "<<s.pname<<endl; } ~Student(){ cout<<"destruct "<<pname<<endl; delete[] pname; } void Disp(){ cout<<pname<<" "<<score<<endl; } };

int main(){ char name[20]; int score; cin>>name>>score; Student s1, s2(name, score), s3(s2); s1.Disp(); s2.Disp(); s3.Disp(); Student s4=s3; s4.Disp(); return 0; }

设计一个学生类有两个成员:其一为指针变量char pname用来存储名字在堆中的存储位置;其二为score用来存储分数int 型两者都是私有的: private: char pname; 名字在堆中找空间存放pname存储其起始位置 int score; 请你完善该类使得执行下面的程序时输出正确信息。 int main char name20; int score; cinnamescore;

原文地址: https://www.cveoy.top/t/topic/bC2p 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录