logo资料库

由Person类派生出学生类Student和教师类Teacher.doc

第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
资料共16页,剩余部分请下载后查看
/*------------------------------------------------------------- 由 Person 类派生出学生类 Student 和教师类 Teacher,并为他们添加必要的 数据,为这两个派生类编写必要的构造函数和析构函数。然后编写一个主 函数对以上功能进行测试。 ----------------------------------------------------------------*/ ¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥ **************************************************************************%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % #include using namespace std; class child { public: int num; child *next; child *creat(int n) { child *head,*p,*tail; head=new child; head->num=1; tail=head; for(int i=2;i<=n;i++) { p=new child; p->num=i; tail->next=p; tail=p; } p->next=head; int count = 6 ; return head; } }; class joesphus { public: void output(child *p,int m,int s,int n,int w) {
int count = w ; child *a,*b; a=p; if(s!=1) for(int i=1;inext; while(count) { count-- ; for(int i=1;inext; } b->next=a->next; delete a; a=b->next; } cout<<"最后的胜利者的编号是: " ; while(w) { cout<num<<" " ; w-- ; a=a->next ; } } }; void main() { int n,m=3,s,w; child r,*q; joesphus e; cout<<"输入人的个数:"<>n; cout<<"输入起始数数的编号:"<>s; cout<<"输入剩余多少个:"<>w ; q=r.creat(n) ; e.output(q,m,s,n,w); } &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
#include #include class Student; //学生类 class Teacher; //教师类 class Person //人类 { private: int id; char name[20]; public: Person(int x,char *p) { id=x; strcpy(name,p); } void show(Student s) { cout << "编号:" << id << endl; cout << "姓名:" << name << endl; cout << "班号:" << s.getclassid() << endl; cout << "成绩:" << s.getscore() << endl; } void show(Teacher t) //show 函数的重载 { cout << "编号:" << id << endl; cout << "姓名:" << name << endl; cout << "职称:" << t.getzhicheng() << endl; cout << "部门:" << t.getbumen() << endl; } }; class Student:public Person { private: int classid; double score; public: void setclassid(int x) //设置私有属性班级号 { classid=x; } void setscore(double y) //设置私有属性成绩 { score=y; }
int getclassid() { return classid; } double getscore() { return score; } }; class Teacher:public Person { private: char zhicheng[10]; //职称 char bumen[10]; //部门 public: void setzhicheng(char *p) //设置私有属性职称 { strcpy(zhicheng,p); } void setbumen(char *p) //设置私有属性部门 { strcpy(bumen,p); } char getzhicheng() { return zhicheng; } char getbumen() { return bumen; } }; void main() { Person s1(1,"小强"); Student p1; p1.setclassid(15); p1.setscore(90.8); s1.show(p1); } ############################################################################### ### #include #include
#include #include class person { char Name[10]; char Sex; int Age; public: person(char *name,int age,char sex) { strcpy(Name,name); Age=age; Sex=(sex=='m'?'m':'f'); } virtual void set() { int age; char name[10]; char sex; cout<<"请输入姓名:"<>name; strcpy(Name,name); cout<<"请输入年龄:"<>age; Age=age; cout<<"请输入性别:"<>sex; Sex=sex; } virtual void showinfo() { } cout<
student(char *csname,int num,float sco1,float sco2,float sco3,float average,float sum,char *name,int age, char sex):person(name,age,sex) { } { strcpy(classname,csname); number=num; score1=sco1; score2=sco2; score3=sco3; sum=score1+score2+score3; average=sum/3; void set() int sco[3]; person::set(); cout<<"请输入班级:"; cin>>classname; strcpy(classname,classname); cout<<"请输入学号:"; cin>>number; number=number; cout<<"请输入第一门课成绩:"; cin>>sco[0]; score1=sco[0]; cout<<"请输入第二门课成绩:"; cin>>sco[1]; score2=sco[1]; cout<<"请输入第三门课成绩:"; cin>>sco[2]; score3=sco[2]; sum=score1+score2+score3; cout<<"该同学的总分为:"<
} }; class teacher:public person { char title[30];//定义职称 char dept[30];//定义部门 int length;//定义工龄 public: teacher(char *tit,char *de,int len,char *name,char sex,int age):person(name,age,sex) { strcpy(title,tit); strcpy(dept,de); length=len; } void set() { char tit[30];//定义职称 char de[30];//定义部门 int len;//定义工龄 person::set(); cout<<"职称:"<>tit; strcpy(title,tit); cout<<"部门:"<>de; strcpy(dept,de); cout<<"工龄:"<>len; length=len; } void showinfo() { cout<
cout<>choice; system("cls"); if (choice>=0&&choice<=3) switch (choice) { case 1:{d[choice-1]->set(); d[choice-1]->showinfo();} break; case 2:{ d[choice]->set(); d[choice]->showinfo();} break; case 3:{ d[choice-2]->set(); d[choice-2]->showinfo();} break; case 0:exit(0); break; } else cout<<"错误输入!请重新选择:"<
分享到:
收藏