《C++课程设计程序》由会员分享,可在线阅读,更多相关《C++课程设计程序(22页珍藏版)》请在金锄头文库上搜索。
1、#include #include using std:cout;using std:endl;class Coursepublic: Course():name(0),next(0) Course(char n); Course()delete name; void setName(char n); void setNextCourse(Course* c); char *getName()return name; Course* getNext()return next;private: char* name; Course* next;Course:Course(char n) name
2、=new charstrlen(n)+1; strcpy(this-name,n);void Course:setName(char n) delete name; this-name=new charstrlen(n)+1; strcpy(this-name,n);void Course:setNextCourse(Course* c) this-next=c;class Personpublic: Person():id(0),name(0),courses(0) Person(char i,char n); Person(); void setID(char i); void setNa
3、me(char n); char *getID()return id; char *getName()return name; void addCourses(Course* c); virtual void display();protected: char* id; char* name; Course* courses;Person:Person(char i,char n) this-id=new charstrlen(i)+1; strcpy(this-id,i); this-name=new charstrlen(n)+1; strcpy(this-name,n);void Per
4、son:setID(char i) delete id; this-id=new charstrlen(i)+1; strcpy(this-id,i);Person:Person() Course* c; while(courses!=0) c=courses-getNext(); delete courses; courses=c; delete id; delete name;void Person:setName(char n) delete name; this-name=new charstrlen(n)+1; strcpy(this-name,n);void Person:disp
5、lay() coutID: id Name: name Courses: ; Course* c=courses; while(c!=0) coutgetName()getNext(); coutgetNext()!=0) t=t-getNext(); t-setNextCourse(c); class Teacher:public Personpublic: Teacher():Person(),position(0) Teacher(char id,char name,char position); Teacher(const Teacher& t); Teacher(); void di
6、splay(); void setPosition(char p); char *getPosition()return position;private: char* position;Teacher:Teacher(char i, char n, char p):Person(i,n) position=new charstrlen(position)+1; strcpy(this-position,p);Teacher:Teacher(const Teacher& t):Person() this-id=new charstrlen(t.id)+1; strcpy(this-id,t.i
7、d); this-name=new charstrlen(t.name)+1; strcpy(this-name,t.name); this-position=new charstrlen(t.position)+1; strcpy(this-position,t.position);Teacher:Teacher() delete position;void Teacher:display() coutID: id Name: name Position: position Courses to teacher: ; Course* c=courses; while(c!=0) coutge
8、tName()getNext(); coutposition=new charstrlen(p)+1; strcpy(this-position,p);class Postgraduate:public Personpublic: Postgraduate():Person()degree=new charstrlen(Postgraduate)+1;strcpy(this-degree,Postgraduate); Postgraduate(char i,char n); Postgraduate(const Postgraduate& p); Postgraduate(); void di
9、splay(); char *getDegree()return degree;private: char* degree;Postgraduate:Postgraduate(char i,char n):Person(i,n) this-degree=new charstrlen(Postgraduate)+1; strcpy(this-degree,Postgraduate);Postgraduate:Postgraduate(const Postgraduate& p):Person() this-id=new charstrlen(p.id)+1; strcpy(this-id,p.i
10、d); this-name=new charstrlen(p.name)+1; strcpy(this-name,p.name); this-degree=new charstrlen(Postgraduate)+1; strcpy(this-degree,Postgraduate);Postgraduate:Postgraduate() delete degree;void Postgraduate:display() coutID: id Name: name Degree: degree Courses to study: ; Course* c=courses; while(c!=0)
11、 coutgetName()getNext(); coutdegree,Undergraduate); Undergraduate(char i,char n); Undergraduate(const Undergraduate& u); Undergraduate(); void display(); char *getDegree()return degree;private: char* degree;Undergraduate:Undergraduate(char i, char n):Person(i,n) this-degree=new charstrlen(Undergradu
12、ate)+1; strcpy(this-degree,Undergraduate);Undergraduate:Undergraduate(const Undergraduate& u):Person() this-id=new charstrlen(u.id)+1; strcpy(this-id,u.id); this-name=new charstrlen(u.name)+1; strcpy(this-name,u.name); this-degree=new charstrlen(Undergraduate)+1; strcpy(this-degree,Undergraduate);Undergraduate:Undergraduate() delete degree;void Undergraduate:display() coutID: id Name: name Degree: degree Courses to study: ; Course* c=courses; while(c!=0) coutget