学校类的属性包含班级类指针,班级类属性包含学生类指针
班级类指针指向堆内存中的班级对象数组,
班级类中的学生指针指向堆内存中的学生对象数组。
#include<iostream>
#include <sstream>
#include <string>
using namespace std;
class Student{
public:
string name;
int age;
};
class Class{
public:
string className;
Student *student;
};
class School{
public:
string schoolName;
Class *pclass;
};
int main(){
School *school=new School();
school->pclass=new Class[10];
school->schoolName="XXX大学";
for(int i=0;i<10;i++){
school->pclass[i].student=new Student[30];
stringstream ss;
ss<<i;
string str=ss.str();
school->pclass[i].className="班级"+str;
for (int j=0;j<30;j++)
{
school->pclass[i].student[j].age=i+10;
stringstream ss;
ss<<j;
string str=ss.str();
school->pclass[i].student[j].name="学生"+str;
}
}
cout<<school->schoolName<<endl;
for(int i=0;i<10;i++){
for (int j=0;j<30;j++)
{
cout<<"班级名:"<<school->pclass[i].className<<" 学生名:"<<school->pclass[i].student[j].name<<" 年龄:"<<school->pclass[i].student[j].age<<endl;
}
}
return 0;
}