|
发表于 2020-6-13 18:00:01
|
显示全部楼层
#include<iostream.h>
#include<string.h>
#define MAX 2
class student
{
public:
student()
{
}
student(char n[],char s[],int t)
{
strcpy(name,n);
strcpy(reseach,s);
no=t;
}
~student()
{
}
void ShowInfo()
{
cout << "学生: "<< name << " "
<< "研究方向: "<< reseach << " "
<< "学号: "<< no << " "
<< endl;
}
private:
char name[20];
char reseach[20];
int no;
};
class teacher
{
private:
int top;
char name[20];
student stu[MAX];
public:
teacher(char t[])
{
top=0;
strcpy(name,t);
}
~teacher()
{
// delete []stu;
}
void add(student& s)
{
stu[top]=s;
top++;
}
void getname()
{
cout<<"老师:"<<name<<endl;
}
void ShowStudentInfo()
{
for(int i=0;i<top;i++)
{
stu[i].ShowInfo();
cout << endl;
}
}
};
void main()
{
teacher t[]={teacher("Tom"),teacher("Marry")};
// student("Pirlo","Java",100);
// student("Gattuso","C++",101)
// student("kaka","C",102)
t[0].add(student("Pirlo","Java",100));
t[1].add(student("Gattuso","C++",101));
t[1].add(student("kaka","C",102));
for(int i=0;i<2;i++)
{
t[i].getname();
cout<< endl;
t[i].ShowStudentInfo();
cout << endl;
}
}
这个样子是可以的,不过有点乱,自己再看看吧! |
|