实验一随堂测试请同学们将以下代码按照要求补充完整#include iostream#include stringhusing namespace std;class Student public Studentchar name1float score1; ~Student; 此处请完成你的附加代码 private char name;
//补充完整的代码如下:
#include
class Student{ public: Student(char *name1,float score1); ~Student(); void display(); //新增的成员函数,用于输出学生姓名和成绩 private: char *name; //学生姓名 float score; //学生成绩 };
Student::Student(char *name1,float score1) { cout<<"构造函数使用中…"<<name1<<endl; name=new char[strlen(name1)+1]; if(name!=0) { strcpy(name,name1); score=score1; } }
Student::~Student() { cout<<"析构函数使用中…"<<name<<endl; name[0]='\0'; delete []name; }
void Student::display() { cout << "姓名:" << name << ",成绩:" << score << endl; }
int main() { Student stu1("黎明",90); //定义类Student的对象stu1,调用默认的拷贝构造函数 Student stu2=stu1;
//定义一个学生数组,数组长度为3。通过循环完成数组元素的数据成员赋值。
Student stu[3] = { {"张三",80}, {"李四",70}, {"王五",85} };
for(int i=0; i<3; i++)
{
stu[i].display();
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/b1Kx 著作权归作者所有。请勿转载和采集!