以人类Person为基类公有派生学生类Student和教师类Teacher,main(void)函数完成对其的测试。
Person类结构说明:
Person类的数据成员包括: ①私有数据成员:身份证号no(char [20]型),姓名name(char [20]型)。 Person类成员函数包括: ①有参构造函数Person(char *, char *)和拷贝构造函数Person(const Person &),其中有参构造函数参数默认值为空串或0(当参数为NULL时视为空串处理),输出信息“Person Constructor run”,拷贝构造函数输出信息“Person CopyConstructor run” ②析构函数,析构函数输出信息“Person Destructor run” ③公有函数成员:void setNo(char *)和char * getNo()分别返回和设置身份证号no(当参数为NULL时视为空串处理) ④公有函数成员:void setName(char *)和char* getName()分别返回和设置姓名name(当参数为NULL时视为空串处理) ⑤公有成员函数void show() const用于显示人的基本信息,显示格式为: No=身份证号,Name=姓名
#include <cmath> int i; class Student : private Person { private: char sNo[10]; char className[20]; double score = 0; public: Student(char *a = NULL,char *b = NULL, char *c = NULL, char *d = NULL, double x = 0) { for(i = 0; i < 20; i++) { no[i] = a[i]; name[i] = b[i]; className[i] = d[i]; } for(i = 0; i < 10; i++) sNo[i] = c[i]; score = x; cout << "Student Constructor run" << endl; } Student(const Student &r):Person(r) { for(i = 0; i < 20; i++) { no[i] = r.no[i]; name[i] = r.name[i]; className[i] = r.className[i]; } for(i = 0; i < 10; i++) sNo[i] = r.sNo[i]; score = r.score; cout << "Student CopyConstructor run" << endl; } ~Student() { cout << "Student Destructor run" << endl; } void setSNo(char *a = NULL) { for(i = 0; i < 10; i++) sNo[i] = a[i]; } char *getSNo() { return sNo; } void setClassName(char *a = NULL) { for(i = 0; i < 20; i++) className[i] = a[i]; } char *getClassName() { return className; } void setScore(double x = 0) { score = x; } double getSocre() { return score; } void show() const { cout << "No=" << no << ",Name=" << name << endl; cout << "SNo=" << sNo << ",ClassName=" << className << ",Score=" << round(score) << endl; } }; class Teacher : private Person { private: char tNo[10]; char departmentName[20]; double wages; public: Teacher(char *a = NULL,char *b = NULL, char *c = NULL, char *d = NULL, double x = 0) { for(i = 0; i < 20; i++) { no[i] = a[i]; name[i] = b[i]; departmentName[i] = d[i]; } for(i = 0; i < 10; i++) tNo[i] = c[i]; wages = x; cout << "Teacher Constructor run" << endl; } Teacher(Teacher &r):Person(r) { for(i = 0; i < 20; i++) { no[i] = r.no[i]; name[i] = r.name[i]; departmentName[i] = r.departmentName[i]; } for(i = 0; i < 10; i++) tNo[i] = r.tNo[i]; wages = r.wages; cout << "Teacher CopyConstructor run" << endl; } ~Teacher() { cout << "Teacher Destructor run" << endl; } void setTNo(char *a = NULL) { for(i = 0; i < 10; i++) tNo[i] = a[i]; } char * getTNo() { return tNo; } void setDepartmentName(char *a = NULL) { for(i = 0; i < 20; i++) departmentName[i] = a[i]; } char *getDepartmentName() { return departmentName; } void setWages(double a) { wages = a; } double getWages() { return wages; } void show() const { cout << "No=" << no << ",Name=" << name << endl; cout << "TNo=" << tNo << ",DepartmentName=" << departmentName << ",Wages=" << round(wages) << endl; } };
标签:20,函数,第十七,char,Person,NULL,空串 From: https://www.cnblogs.com/gjsgjs/p/17413057.html