class person //此中的虚构有问题,????????????有问题;有问题 {//public: char *name; char sex; int age; public: person(char *na,char sx,int ag); void print(void)const; ~person(void);//把这里的虚构不要,即可,要了为什么不行? }; person::person(char *na,char sx,int ag) { // name=na; name=new char(strlen(na)+1); strcpy(name,na); sex=sx; age=ag; } void person::print(void)const { cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; cout<<"age:"<<age<<endl; } person::~person () { delete[]name; } // // // // class student:public person { int number; char department[30]; public: student(char *na,char sx,int ag,int num,char *dep); void print(void)const;
}; student::student(char *na,char sx,int ag,int num,char *dep):person(na,sx,ag) { number=num; // department=dep; strcpy(department,dep); } void student::print(void)const { person::print (); cout<<"number:"<<number<<endl; cout<<"department:"<<department<<endl; }
// // // // class teacher:public person { char post[30]; char course[40]; public: teacher(char *na,char sx,int ag,char *po,char *co); void print(void)const;
}; teacher::teacher(char *na,char sx,int ag,char *po,char *co):person(na,sx,ag)
{ // person::print (); strcpy(post,po); // course=co; strcpy(course,co); } void teacher::print(void)const { person::print (); cout<<"post:"<<post<<endl; cout<<"course:"<<course<<endl; }
// // // // class graduateStudent:public student { char advisor[30]; public:
graduateStudent(char *na,char sx,int ag,int num,char *dep,char *ad); void print(void)const;
}; graduateStudent::graduateStudent(char *na,char sx,int ag,int num,char *dep,char *ad):student(na,sx,ag,num,dep)
{ strcpy(advisor,ad); } void graduateStudent::print (void)const { student::print (); cout<<"advisor:"<<advisor<<endl; }
int main(void) { person per1("ye",'m',16),per2("guang",'w',12); student stu1("ming",'m',23,1001,"comp"),stu2("yeguangming",'w',34,1002,"physics"); teacher teach1("meng",'m',23,"professor","english"); graduateStudent grad_stu("mingli",'w',45,3432,"math","wang"); per1.print (); per2.print (); stu1.print (); stu2.print (); teach1.print (); grad_stu.print (); return 0; }