设计一个动态数组类ArrayOfStudent,数组元素是Student类对象,重载取下标运算符[]

2022-08-03 社会 56阅读
#include
#include
using namespace std;
class student
{
private:
int num;
string name;
int score;
public:
student& operator=(const student& s)
{
num=s.num;
name=s.name+"*";
score=s.score;
return *this;
}
student(int n=0,string na="a",int s=0)
{
num=n;
name=na;
score=s;
}
Print()
{
cout< }
};
class ArrayOfStudent
{
public:
ArrayOfStudent(int n)
{
numberOfstudent=n;
stu=new student[n];
}
~ArrayOfStudent()
{
numberOfstudent=0;
delete[] stu;
}
student& operator[](const int n)
{
return stu[n];
}
private:
student *stu;
int numberOfstudent;
};
void main()
{
int n,i;
int num,score;
string name;

student b,c;
c=b;
b.Print();
c.Print();
cin>>n;
ArrayOfStudent a(n);
for(i=0;i {
cin>>num>>name>>score;
student d(num,name,score);
a[i]=d;
}
for(i=0;i a[i].Print();

}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com