设计老师类和学生类。每个老师有姓名和研究方向,还可以指导若干个研究生。研究生的私有数据成
员包括姓名,专业和学号;编写一个程序,创建包含三个教师的数组。输入每个老师的指导所有研究
生的信息,输出所有教师的相关信息,包括指导学生情况。注意:要求不能使用友元函数!
#include<iostream>
#include<cstring>
using namespace std;
class Student
{
public:
Student(char*name_,char* major_,int id_);
Student();
~Student();
void Setstudent();
void show();
private:
char* name;
char* major;
int id;
};
class Teacher
{
public:
Teacher(char* name_, char* major_, int n_);
Teacher();
~Teacher();
void show();
private:
char* name;
char* major;
Student* student;
int n;
};
Student::Student(char* name_, char* major_, int id_):id(id_)
{
name = new char[20];
strcpy(name, name_);
major = new char[20];
strcpy(major, major_);
}
Student::Student(){
name = new char[20];
major = new char[20];
id = 0;
}
Student::~Student()
{
delete[]name;
delete[]major;
}
void Student::Setstudent() {
cout << "Student's name:" << endl;
cin >> name;
cout << "major:" << endl;
cin >> major;
cout << "id:" << endl;
cin >> id;
}
void Student::show() {
cout << "Student's name:" << name << endl << "major:" << major << endl << "id:" << id << endl;
}
Teacher::Teacher(char* name_, char* major_, int n_):n(n_)
{
name = new char[20];
strcpy(name, name_);
major = new char[20];
strcpy(major, major_);
student = new Student[n];
for (int i = 0; i < n; i++) {
student[i].Setstudent();
}
}
Teacher::Teacher() {
name = new char[20];
cout << "Teacher's name:" << endl;
cin >> name;
major = new char[20];
cout << "major:" << endl;
cin >> major;
cout << "How many students?" << endl;
cin >> n;
student = new Student[n];
for (int i = 0; i < n; i++) {
student[i].Setstudent();
}
}
Teacher::~Teacher()
{
delete[]name;
delete[]major;
delete[]student;
}
void Teacher::show() {
cout << "Teacher's name:" << name << endl << "major:" << major << endl;
for (int i = 0; i < n; i++) {
student[i].show();
}
}
int main() {
Teacher t[3];
for (int i = 0; i < 3; i++) {
t[i].show();
}
return 0;
}