C++第11周mooc在线测评—第11周 类的继承

    xiaoxiao2021-12-14  18

    //1公有继承中派生类Student对基类Person成员的访问(20分) //题目内容: //已知基类Person的定义如下: //class Person //{ // char Name[20]; // char Sex; // int // Age; //public: // void Register(char *name, int age, char sex); // void // ShowMe(); //}; //请通过继承的方法建立一个派生类Student,其中 //1.新增的数据成员有: //int //Number; //char ClassName[10]; //2.新增的成员函数有: //void RegisterStu(char //*classname, int number, char *name, int age, char sex) // 对数据成员赋值,并使用基类的Register //void //ShowStu() //显示数据成员信息,并使用基类的ShowMe //在主程序中建立一个派生类对象,利用已有的成员函数分别显示派生类对象和基类对象的数据成员。 // //输入格式 : //学生的班级、学号、姓名、年龄和性别 // //输出格式: //先输出派生类对象的数据成员,依次为 学号、班级、姓名、年龄和性别 //再输出基类的数据成员,依次为姓名、年龄和性别 // //输入样例: //计算机51 85071011 张弓长 18 m // //输出样例: //85071011 计算机51 张弓长 18 m //张弓长 18 m #include <iostream> #include <string.h> using namespace std; class Person { private: char Name[20]; char Sex; int Age; public: void Register(char *name, int age, char sex) { strcpy(this->Name, name); this->Sex = sex; this->Age = age; } void ShowMe() { cout << Name << " " << Age << " " << Sex<<endl; } }; class Student:public Person { private: int Number; char ClassName[10]; public: void RegisterStu(char*classname, int number, char *name, int age, char sex) { Person::Register(name, age, sex); strcpy(this->ClassName, classname); this->Number = number; } void ShowStu() { cout << Number << " " << ClassName << " " ; Person::ShowMe(); } }; int main() { char Name[20]; char Sex; int Age; int Number; char ClassName[10]; Student stu1; cin >> ClassName >> Number >> Name >> Age >> Sex; stu1.RegisterStu(ClassName, Number, Name, Age, Sex); stu1.ShowStu(); stu1.Person::ShowMe(); system("pause"); return 0; } //2一个基类Person的多个派生类(20分) //题目内容: //已知基类Person的定义如下: //class Person //{ //protected: // char Name[10]; // char // Sex; // int Age; //public: // void Register(char *name, int age, char // sex); // void // ShowMe(); //}; //请通过继承的方法建立两个派生类,其中 //派生类Teacher: //1.新增的数据成员有: //char //Dept[20]; //int //Salary; //2.新增的成员函数有: //构造函数,并使用基类的Register //3.重写的成员函数有: //void //ShowMe() //显示数据成员信息,并使用基类的ShowMe //派生类Student: //1.新增的数据成员有: //char //ID[12]; //char Class[12]; //2.新增的成员函数有: //Student(char *name, int age, char //sex, char *id, char *classid); //3.重写的成员函数有: //void //ShowMe() //显示数据成员信息,并使用基类的ShowMe //在主程序中分别建立两个派生类对象,利用已有的成员函数分别显示两个派生类对象的数据成员。 // //输入格式 : //教师对象的初始化参数 //学生对象的初始化参数 // //输出格式: //请参考输出样例严格按照格式输出教师对象和学生对象的详细信息 // //输入样例: //章立早 38 m 电信学院 2300 //李木子 22 f 02035003 能动01 // //输出样例: //姓名 章立早 //性别 男 //年龄 38 //工作单位 电信学院 //月薪 2300 //学号 02035003 //姓名 李木子 //性别 女 //年龄 22 //班级 能动01 #include <iostream> #include <string.h> using namespace std; class Person { protected: char Name[10]; char Sex; int Age; public: void Register(char *name, int age, char sex) { strcpy(Name, name); Age = age; Sex = sex; } void ShowMe() { cout << "姓名" << " "; cout << Name << endl; cout << "性别" << " "; if (Sex == 'm') { cout << "男" << endl; } else { cout << "女" << endl; } cout << "年龄" << " "; cout << Age << endl; } }; class Teacher :protected Person { private: char Dept[20]; int Salary; public: Teacher(char *name, int age, char sex, char Dept[], int Salary) { Person::Register(name, age, sex); strcpy(this->Dept, Dept); this->Salary = Salary; } void ShowMe() { Person::ShowMe(); cout << "工作单位" << " "; cout << Dept << endl; cout << "月薪" << " "; cout << Salary << endl; } }; class Student :protected Person { private: char ID[12]; char Class[12]; public: Student(char *name, int age, char sex, char *id, char *classid) { Person::Register(name, age, sex); strcpy(this->ID, id); strcpy(this->Class, classid); } void ShowMe() { cout << "学号" << " "; cout << ID << endl; Person::ShowMe(); cout << "班级" << " "; cout << Class << endl; } }; int main() { char Name1[10]; char Name2[10]; char Sex1; char Sex2; int Age1; int Age2; char Dept[20]; int Salary; char ID[12]; char Class[12]; cin >> Name1 >> Age1 >> Sex1>> Dept >> Salary; cin >> Name2 >> Age2 >> Sex2 >> ID >> Class; Teacher teacher1(Name1, Age1, Sex1, Dept, Salary); teacher1.ShowMe(); Student student1(Name2, Age2, Sex2, ID, Class); student1.ShowMe(); system("pause"); return 0; }

    //3派生类Student的构造函数和析构函数(20分) //题目内容: //已知基类Person的定义如下: //class Person //{ // char Name[10]; //姓名 // int Age; //年龄 //public: // Person(char* name, int age) // { // strcpy(Name, name); // Age = age; // cout << "constructor of person " << Name << endl; // } // ~Person() // { // cout << "deconstructor of person " << Name << endl; // }; // 请通过继承的方法建立一个派生类Student,其中 // 1.新增的数据成员有: // char ClassName[10]; //班级 // Person Monitor; //班长 // 2.新增的成员函数有: // Student(char *name, int age, char *classname, char *name1, int age1) //name1和age1是班长的信息 // ~Student() // 在主程序中建立一个派生类对象。 // // 输入格式 : // Student类的初始化信息 // // 输出格式: // 派生类和基类构造函数和析构函数输出的信息,请参考输出样例的格式。 // // 输入样例: // 张弓长 18 计算机51 李木子 20 // // 输出样例: // constructor of person 张弓长 // constructor of person 李木子 // constructor of Student // deconstructor of Student // deconstructor of person 李木子 // deconstructor of person 张弓长 // 注意:person为小写,单词间有一个空格。 #include <iostream> #include <string.h> using namespace std; class Person { private: char Name[10]; //姓名 int Age; //年龄 public: Person(char* name, int age) { strcpy(Name, name); Age = age; cout << "constructor of person " << Name << endl; } ~Person() { cout << "deconstructor of person " << Name << endl; }; }; class Student :public Person { private: char ClassName[10]; Person Monitor; public: Student(char *name, int age, char *classname, char *name1, int age1) :Person(name, age), Monitor(name1, age1) { strcpy(ClassName, classname); cout << "constructor of Student" << endl; } ~Student() { cout << "deconstructor of Student" << endl; }; }; int main() { char name[10]; int age; char classname[10]; char name1[10]; int age1; cin >> name >> age >> classname >> name1 >> age1; Student student1(name, age, classname, name1, age1); student1.~Student(); system("pause"); return 0; } //4从Point类继承的Circle类(20分) //题目内容: //已知基类Point的定义如下: // //class Point //{ // int x, y; //点的x和y坐标 //public: Point(int = 0, int = 0); // 构造函数 // void SetPoint(int, int); // 设置坐标 // int GetX() { return x; } // 取x坐标 // int GetY() { return y; } // 取y坐标 // void Print(); //输出点的坐标 }; // Point(int a, int b) { SetPoint(a, b); } // void SetPoint(int a, int b) { x = a; y = b; } // void Print() { // cout << "[" << x << "," << y << "]"; // } // // 请通过继承的方法建立一个派生类Circle,其中 // 1.新增的数据成员有: double radius; // 2.新增的成员函数有: // Circle(int x = 0, int y = 0, double r = 0.0); //对数据成员赋值,并使用SetRadius和基类的Point // void SetRadius(double); //设置半径 // double GetRadius(); //取半径 // double Area(); //计算面积 // void Print(); //输出圆心坐标和半径,并使用基类的Print // 在主程序中分别建立基类对象和派生类对象,使用用户输入的初值分别对基类对象和派生类对象的数据成员赋值后,利用已有的成员函数分别显示基类对象和派生类对象的数据成员信息。 // 圆周率取3.14。 // // 输入格式 : // 第一行是Point类的初始化信息,第二行是Circle类的初始化信息 // // 输出格式: // 请参考输出样例,严格按照样例输出,建议直接将部分文字复制粘贴进程序代码中 // // 输入样例: // 30 50 // 120 80 10 // // 输出样例: // Point p[30, 50] // Circle c Center = [120, 80] // Radius = 10 // The centre of circle c[120, 80] // The area of circle c 314 #include <iostream> #include <string.h> using namespace std; #define PI 3.14 class Point { private: int x, y; //点的x和y坐标 public: int GetX() { return x; } // 取x坐标 int GetY() { return y; } // 取y坐标 Point(int a, int b) { SetPoint(a, b); } void SetPoint(int a, int b) { x = a; y = b; } void Print() { cout << "[" << x << "," << y << "]"; } }; class Circle:public Point { private: double radius; public: Circle(int x = 0, int y = 0, double r = 0) :Point(x, y) { SetRadius(r); } void SetRadius(double r) { radius = r; } double GetRadius() { return radius; } double Area() { cout << "The centre of circle c "; Point::Print(); cout << endl; cout << "The area of circle c "; cout << PI*radius*radius << endl; return PI*radius*radius; } void print() { cout << "Circle c Center="; Point::Print(); cout << endl; cout << "Radius=" << radius << endl; } ~Circle() { } }; int main() { int x1, y1; int x2, y2; double r; cin >> x1 >> y1; cin >> x2>>y2>>r; Point p(x1,y1); Circle c(x2, y2, r); cout << "Point p "; p.Print(); cout << endl; c.print(); c.Area(); system("pause"); return 0; }

      //5从Student类和Teacher类多重派生Graduate类(20分) //题目内容: //已知基类Person定义如下: //class Person //{ // char Name[10]; // char Sex[10]; // int Age; //public: // void Register(char *name, int age, char *sex); // void ShowMe(); //}; //请通过继承的方法建立两个派生类,其中 //派生类Teacher: //1.新增的数据成员有: //char Dept[20]; //int Salary; //2.新增的成员函数有: //Teacher(char *name, int age, char *sex, char *dept, int salary); //void Show() //显示新增数据成员 //派生类Student: //1.新增的数据成员有: //char ID[12]; //char Class[12]; //2.新增的成员函数有: //Student(char *name, int age, char *sex, char *ID, char *Class); //void Show()//显示新增数据成员 //请通过继承的方法从Teacher和Student中建立派生类Graduate,其中 //1.新增的成员函数有: //Graduate(char *name, int age, char *sex, char *dept, int salary, char *id, char *classid); //2.重写的成员函数有: //void ShowMe()//显示数据成员,要求调用基类的Show和ShowMe //在主程序中建立一个派生类Graduate的对象,利用成员函数显示对象的数据成员。 // // //输入格式 : //Graduate对象的初始化信息 // //输出格式: //按照输出样例格式输出Graduate对象的信息 // //输入样例: //李木子 22 f 电信学院 2300 04035003 硕401 // //输出样例: //班级 硕401 //学号 04035003 //姓名 李木子 //性别 女 //年龄 22 //工作单位 电信学院 //月薪 2300 #include <iostream> #include <string.h> using namespace std; class Person { private: char Name[10]; char Sex[10]; int Age; public: void Register(char *name, int age, char *sex) { strcpy(Name, name); strcpy(Sex, sex); Age = age; } void ShowMe() { cout << "姓名 " << Name << endl; cout << "性别 "; if (Sex[0] == 'm') { cout << "男" << endl; } else { cout << "女" << endl; } cout << "年龄 " << Age << endl; } }; class Teacher :public Person { private: char Dept[20]; int Salary; public: Teacher(char *name, int age, char *sex, char *dept, int salary) { Register(name, age, sex); strcpy(Dept, dept); Salary = salary; } void Show() { cout << "工作单位 " << Dept << endl; cout << "月薪 " << Salary << endl; } }; class Student :public Person { private: char ID[12]; char Class[12]; public: Student(char *name, int age, char *sex, char *ID, char *Class) { Register(name, age, sex); strcpy(this->ID, ID); strcpy(this->Class, Class); } void Show() { cout << "班级 " << Class << endl; cout << "学号 " << ID << endl; } }; class Graduate :public Teacher, public Student { private: public: Graduate(char *name, int age, char *sex, char *dept, int salary, char *id, char *classid) : Student(name, age, sex, id, classid), Teacher(name, age, sex, dept, salary) { } void ShowMe() { Student::Show(); Student::ShowMe(); Teacher::Show(); } }; int main() { char name[10]; int age; char sex[10]; char dept[20]; int salary; char id[12]; char classid[12]; cin >> name >> age >> sex >> dept >> salary >> id >> classid; Graduate gra1(name, age, sex, dept, salary, id, classid); gra1.ShowMe(); system("pause"); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-971599.html

    最新回复(0)