编程处理教师的基本情况。要求:
1、定义一个“person”类,用来存储及处理人的姓名、性别、年龄,成员函数自定;
2、定义“teacher”类,公有继承“person”类用来存储教师所在学院、所学专业、学历、学位、职称、教龄等,成员函数自定。
3、处理程序,主要包括:
⑴显示姓名、性别、年龄函数:既能显示person对象的姓名、性别、年龄,又能显示teacher对象的姓名、性别、年龄(用person引用对象为形参);
⑵显示教师所在学院、所学专业、学历、学位、职称、教龄的函数;
⑶ main()函数:分别定义persor对象及teacher对象,并输入不同对象相关值,调用成员函数设置对象的值,调用显示函数显示相应值。
#ifndef __person__person__ #define __person__person__ #include<iostream> using namespace std; #include<string.h> class person { protected: char *name; char *sex; int age; public: person(char *na,char *se,int ag); void set_person(char *na,char *se,int ag); char* get_name() {return name;} char* get_sex() {return sex;} int get_age() {return age;} void print(); ~person() {delete []name;delete []sex;} }; person::person(char *na,char *se,int ag) { name=new char[strlen(na)+1]; strcpy(name,na); sex=new char[strlen(se)+1]; strcpy(sex,se); age = 0; } void person:: set_person(char *na,char *se,int ag) { name=new char[strlen(na)+1]; strcpy(name,na); sex=new char[strlen(se)+1]; strcpy(sex,se); age=ag; } void person:: print() { for(int i = 0;i<=strlen(name);i++) { cout<<name[i]; } cout<<endl; for(int i = 0;i<=strlen(sex);i++) { cout<<sex[i]; } cout<<endl; cout<<"age:"<<age<<endl; cout<<endl; } #endif /* defined(__person__person__) */
#ifndef person_teacher_h #define person_teacher_h #include"person.h" class teacher:public person { protected: char *college;//学院 char *speciality;//专业 char *school;//学历 char *degree;//学位 char *title;//职称 int teacherage;//教龄 public: teacher(char *na,char* se,int ag,char *co,char *sp,char* sc,char* de,char* ti,int te); void set_t(char *na,char* se,int ag,char *co,char *sp,char* sc,char* de,char* ti,int te); char* get_college() {return college;} char* get_speciality() {return speciality;} char* get_school() {return school;} char* get_degree() {return degree;} char* get_title() {return title;} int get_teacher_age() {return teacherage;} ~teacher(); void print_t(person&p); }; teacher::teacher(char *na,char* se,int ag,char *co,char *sp,char* sc,char* de,char* ti,int te):person(na,se,ag) { //person::set_person(na, se, ag); college=new char[strlen(co)+1]; strcpy(college,co); speciality=new char[strlen(sp)+1]; strcpy(speciality,sp); school=new char[strlen(sc)+1]; strcpy(school,sc); degree=new char[strlen(de)+1]; strcpy(degree,de); title=new char[strlen(ti)+1]; strcpy(title,ti); teacherage =0; } void teacher:: set_t(char *na,char* se,int ag,char *co,char *sp,char* sc,char* de,char* ti,int te) { name=new char[strlen(na)+1]; strcpy(name,na); sex=new char[strlen(se)+1]; strcpy(sex,se); age=ag; college=new char[strlen(co)+1]; strcpy(college,co); speciality=new char[strlen(sp)+1]; strcpy(speciality,sp); school=new char[strlen(sc)+1]; strcpy(school,sc); degree=new char[strlen(de)+1]; strcpy(degree,de); title=new char[strlen(ti)+1]; strcpy(title,ti); teacherage =te; } teacher::~teacher() { delete []college; delete []speciality; delete []school; delete []degree; delete [] title; } void teacher:: print_t(person &p) { p.print(); cout<<"-------------------"<<endl; for(int i = 0;i<=strlen(college);i++) { cout<<college[i]; } cout<<endl; for(int i = 0;i<=strlen(speciality);i++) { cout<<speciality[i]; } cout<<endl; for(int i = 0;i<=strlen(school);i++) { cout<<school[i]; } cout<<endl; for(int i = 0;i<=strlen(degree);i++) { cout<<degree[i]; } cout<<endl; for(int i = 0;i<=strlen(title);i++) { cout<<title[i]; } cout<<endl; cout<<teacherage<<endl; cout<<endl; } void fun(person &p) { p.print(); cout<<endl; } #endif
#include "teacher.h" //#include"person.h" int main() { person p("s","nv",22); teacher T("张老师","女",44,"计科","网络","本科","博士","教授",20); T.print_t(p); T.set_t("张老师","女",44,"计科","网络","本科","博士","教授",20); T.print_t(p); cout<<T.get_name()<<endl; cout<<T.get_sex()<<endl; cout<<T.get_age()<<endl; cout<<T.get_college()<<endl; cout<<T.get_degree()<<endl; cout<<T.get_school()<<endl; cout<<T.get_speciality()<<endl; cout<<T.get_title()<<endl; cout<<T.get_teacher_age()<<endl; cout<<"--------------------"<<endl; fun(p); return 0; } //int main() //{ // person p("s","nv",22); // p.print(); // p.set_person("w","female",44); // p.print(); // return 0; //}
原文地址:http://blog.csdn.net/cherry_ermao/article/details/46355811