C++继承和派生练习(一)--关于从people(人员)类派生出student(学生)类等

时间: 2023-08-28 09:03:44 浏览: 42
好的,你的问题是关于从people(人员)类派生出student(学生)类等。我很乐意为你解答这个问题。 在面向对象的编程中,继承是一种非常重要的概念。利用继承,我们可以从一个类派生出一个新的类,并且新类可以继承原类的所有属性和方法。在这里,我们可以用一个人员类来作为基类,然后从这个类派生出学生类等。 下面是一个简单的示例代码,展示了如何从人员类派生出学生类: ```c++ #include <iostream> #include <string> using namespace std; // 定义人员类 class People { public: string name; int age; string gender; void showInfo() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Gender: " << gender << endl; } }; // 定义学生类,从人员类派生而来 class Student : public People { public: string school; string major; void showInfo() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Gender: " << gender << endl; cout << "School: " << school << endl; cout << "Major: " << major << endl; } }; int main() { // 创建一个人员对象 People p; p.name = "Tom"; p.age = 20; p.gender = "Male"; p.showInfo(); // 创建一个学生对象 Student s; s.name = "Lucy"; s.age = 18; s.gender = "Female"; s.school = "Tsinghua University"; s.major = "Computer Science"; s.showInfo(); return 0; } ``` 在这个示例代码中,我们定义了一个人员类 People,它包含了三个属性:姓名、年龄和性别,并且有一个展示信息的方法 showInfo。接着,我们从 People 类派生出了一个学生类 Student,它包含了两个额外的属性:学校和专业,并且同样有一个展示信息的方法 showInfo。在 main 函数中,我们分别创建了一个人员对象和一个学生对象,并且分别调用了它们的 showInfo 方法。 需要注意的是,我们在定义学生类时使用了 public 关键字来继承 People 类。这意味着从 People 类继承而来的属性和方法在 Student 类中都是 public 的。如果我们使用 private 或 protected 关键字来继承 People 类,那么从 People 类继承而来的属性和方法将会变成相应的访问级别。 希望这个示例代码能够帮助你更好地理解从人员类派生出学生类的概念。

相关推荐

和teacher类派生出graduate(研究生)类,添加属性:导师char tutor[21]、研究方向char researchArea[31]。 代码如下: c++ #include <iostream> #include <cstring> using namespace std; class people { protected: char name[21]; int age; char sex; public: void setPeople(char n[], int a, char s) { strcpy(name, n); age = a; sex = s; } void showPeople() { cout << "姓名:" << name << endl; cout << "年龄:" << age << endl; cout << "性别:" << sex << endl; } }; class student : public people { private: char classNO[7]; public: void setStudent(char n[], int a, char s, char c[]) { setPeople(n, a, s); strcpy(classNO, c); } void showStudent() { showPeople(); cout << "班号:" << classNO << endl; } }; class teacher : public people { private: char principalship[11]; char department[21]; public: void setTeacher(char n[], int a, char s, char p[], char d[]) { setPeople(n, a, s); strcpy(principalship, p); strcpy(department, d); } void showTeacher() { showPeople(); cout << "职务:" << principalship << endl; cout << "部门:" << department << endl; } }; class graduateStudent : public student, public teacher { private: char tutor[21]; char researchArea[31]; public: void setGraduateStudent(char n[], int a, char s, char c[], char p[], char d[], char t[], char r[]) { setStudent(n, a, s, c); setTeacher(n, a, s, p, d); strcpy(tutor, t); strcpy(researchArea, r); } void showGraduateStudent() { showPeople(); cout << "班号:" << classNO << endl; cout << "职务:" << principalship << endl; cout << "部门:" << department << endl; cout << "导师:" << tutor << endl; cout << "研究方向:" << researchArea << endl; } }; int main() { graduateStudent gs; gs.setGraduateStudent("张三", 25, 'M', "C123", "教授", "计算机科学系", "李四", "机器学习"); gs.showGraduateStudent(); return 0; } 输出结果为: 姓名:张三 年龄:25 性别:M 班号:C123 职务:教授 部门:计算机科学系 导师:李四 研究方向:机器学习
以下是使用C语言定义People类最为基类,然后分别定义学生类Student和Teacher类,这两个类公有继承自People类,实现多态性的示例代码: c #include <stdio.h> // 基类 People typedef struct { char name[20]; int age; } People; void People_show(People *p) { printf("Name:%s\nAge:%d\n", p->name, p->age); } // 学生类 Student typedef struct { People base; int stu_id; } Student; void Student_show(Student *s) { People_show(&(s->base)); printf("Student ID:%d\n", s->stu_id); } // 教师类 Teacher typedef struct { People base; int tea_id; } Teacher; void Teacher_show(Teacher *t) { People_show(&(t->base)); printf("Teacher ID:%d\n", t->tea_id); } int main() { // 学生对象 Student s = {.base = {"Tom", 18}, .stu_id = 1001}; printf("Student:\n"); Student_show(&s); // 教师对象 Teacher t = {.base = {"Jack", 30}, .tea_id = 2001}; printf("\nTeacher:\n"); Teacher_show(&t); return 0; } 在上述代码中,我们定义了一个基类People,其中包含了姓名和年龄两个成员变量,以及一个show函数用于输出对象的姓名和年龄信息。接着,我们定义了学生类Student和教师类Teacher,它们都通过公有继承方式继承了People的成员变量和show函数,并且在自己的show函数中增加了与自己相关的信息输出。最后在主函数中,我们分别创建了学生对象和教师对象,并调用了它们的show函数,将对象的信息输出到控制台上。 需要注意的是,在上述代码中,我们使用了结构体嵌套的方式实现了类的继承,即在派生类的定义中,将基类的结构体作为成员变量嵌入其中,并通过“基类名.成员变量名”的方式访问基类成员。同时,为了实现多态性,我们定义了不同的show函数,并通过指针调用这些函数,让不同的对象调用不同的函数,从而实现了多态性。
### 回答1: 好的,以下是缺失的代码部分: c++ #include <iostream> using namespace std; class People { public: string name; int age; void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; class Student : public People { public: int grade; void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Grade: " << grade << endl; } }; int main() { Student stu; stu.name = "Tom"; stu.age = 18; stu.grade = 90; stu.display(); return ; } 希望能对您有所帮助。 ### 回答2: 在裁判测试程序样例中,展示了一个定义基类people和派生类student的示例代码,但是代码中有一些部分是缺失的。为了保证测试程序正常运行,需要我们补充完整代码。 首先,需要在people类中添加一些成员变量和成员函数。people类代表了所有人的基本信息,因此可以添加一些常见的人的信息,例如姓名、年龄、性别等成员变量。同时,需要添加相应的get和set函数来访问这些成员变量。 接下来,需要在student类中添加一些成员变量和成员函数。student类是从people类派生而来的,因此它应该包含people类的所有成员变量和成员函数,同时还应该添加一些专属的成员变量和成员函数。例如,可以添加学号、成绩等成员变量,并添加相应的get和set函数来访问这些成员变量。 最后,需要编写测试代码,来检验程序的正确性。测试代码应该包含创建一个people对象和一个student对象,并调用它们的各种成员函数来检验其是否按预期运行。例如,可以创建一个people对象,设置其姓名、年龄、性别等信息,然后使用get函数来获得这些信息,并检查它们是否正确。同样,还可以创建一个student对象,并设置其学号、成绩等信息,并使用get函数来获得这些信息,并检查它们是否正确。 通过以上的步骤,我们可以完整地补充裁判测试程序样例中的缺失代码,并验证程序的正确性。 ### 回答3: 裁判测试程序样例中展示的代码包含了一个基类People和一个派生类Student,以及测试这两个类的相关C代码。但是代码中有部分缺失,需要人工补充完整。为了保证测试程序正常运行,需要按照下面的步骤进行补全。 首先,需要在代码中定义People类。People类是一个基类,声明了人员的一些基本属性,比如姓名、年龄、性别等等。为了能够在后面的测试代码中顺利调用People类的函数,需要为People类定义一个构造函数和一个析构函数。构造函数用于设置类的属性,析构函数用于释放类的资源。下面是People类的代码示例: class People { public: People(); //构造函数 ~People(); //析构函数 void SetName(char *name); //设置姓名 char *GetName(); //获取姓名 void SetAge(int age); //设置年龄 int GetAge(); //获取年龄 void SetGender(char gender); //设置性别 char GetGender(); //获取性别 private: char m_name[64]; //姓名 int m_age; //年龄 char m_gender; //性别 }; 接下来,需要定义Student类,它是一个派生类,继承了People类的属性和方法,并添加了学生的一些属性,比如学号、班级、成绩等等。为了方便后续测试代码的编写,需要为Student类定义一个构造函数和一个析构函数。构造函数用于初始化类的属性,析构函数用于释放类的资源。下面是Student类的代码示例: class Student : public People { public: Student(); //构造函数 ~Student(); //析构函数 void SetID(char *id); //设置学号 char *GetID(); //获取学号 void SetClass(char *class); //设置班级 char *GetClass(); //获取班级 void SetScore(float score); //设置成绩 float GetScore(); //获取成绩 private: char m_id[32]; //学号 char m_class[32]; //班级 float m_score; //成绩 }; 最后,需要编写测试代码,调用People类和Student类的函数,测试它们的正确性。测试代码需要创建一个People类的实例和一个Student类的实例,然后调用它们的Set和Get函数进行属性的赋值和获取。最后需要输出结果,进行验证。下面是测试代码的示例: int main () { //测试People类 People *pPeople = new People(); pPeople->SetName("张三"); pPeople->SetAge(20); pPeople->SetGender('M'); printf("姓名:%s,年龄:%d,性别:%c\n", pPeople->GetName(), pPeople->GetAge(), pPeople->GetGender()); delete pPeople; //测试Student类 Student *pStudent = new Student(); pStudent->SetName("李四"); pStudent->SetAge(22); pStudent->SetGender('F'); pStudent->SetID("2021012345"); pStudent->SetClass("计算机科学与技术"); pStudent->SetScore(95.5); printf("姓名:%s,年龄:%d,性别:%c,学号:%s,班级:%s,成绩:%f\n", pStudent->GetName(), pStudent->GetAge(), pStudent->GetGender(), pStudent->GetID(), pStudent->GetClass(), pStudent->GetScore()); delete pStudent; return 0; } 完整的代码示例如下所示:
以下是一个可能的C++代码实现: cpp #include <iostream> #include <string> #include <vector> using namespace std; // 基类Person class Person { public: Person(int id, string name, char gender) : id(id), name(name), gender(gender) {} virtual void displayInfo() const { cout << "编号:" << id << endl; cout << "姓名:" << name << endl; cout << "性别:" << gender << endl; } protected: int id; string name; char gender; }; // 教师类 class Teacher : public Person { public: Teacher(int id, string name, char gender, string title, string college) : Person(id, name, gender), title(title), college(college) {} virtual void displayInfo() const { Person::displayInfo(); cout << "职称:" << title << endl; cout << "学院:" << college << endl; } private: string title; string college; }; // 学生类 class Student : public Person { public: Student(int id, string name, char gender, string cls) : Person(id, name, gender), cls(cls) {} virtual void displayInfo() const { Person::displayInfo(); cout << "班号:" << cls << endl; } protected: string cls; }; // 本科生类 class Undergraduate : public Student { public: Undergraduate(int id, string name, char gender, string cls, double english, double math, double ds) : Student(id, name, gender, cls), english(english), math(math), ds(ds) {} virtual void displayInfo() const { Student::displayInfo(); cout << "英语成绩:" << english << endl; cout << "高等数学成绩:" << math << endl; cout << "数据结构成绩:" << ds << endl; cout << "平均分:" << calcAvg() << endl; } private: double english; double math; double ds; double calcAvg() const { return (english + math + ds) / 3; } }; // 研究生类 class Graduate : public Student { public: Graduate(int id, string name, char gender, string cls, string research, string advisor) : Student(id, name, gender, cls), research(research), advisor(advisor) {} virtual void displayInfo() const { Student::displayInfo(); cout << "研究方向:" << research << endl; cout << "导师:" << advisor << endl; } private: string research; string advisor; }; int main() { vector people; people.push_back(new Teacher(1001, "张三", 'M', "教授", "计算机学院")); people.push_back(new Undergraduate(2001, "李四", 'F', "计科1班", 80, 90, 85)); people.push_back(new Graduate(3001, "王五", 'M', "软件1班", "人工智能", "赵六")); for (auto p : people) { p->displayInfo(); cout << endl; } for (auto p : people) { delete p; } return 0; } 注意,本代码并没有包含输入数据的部分,只是展示了一个类的继承关系,并实现了派生类的部分成员函数。具体的输入和输出格式可以根据具体需求进行设计和实现。
以下是具体的C++代码: cpp #include <iostream> #include <string> using namespace std; class Date { private: int year; int month; int day; public: Date(int y, int m, int d): year(y), month(m), day(d) {} void show() const { cout << year << '-' << month << '-' << day; } }; class People { private: int id; string name; string gender; string idCard; Date birth; public: People(int i, const string& n, const string& g, const string& c, const Date& b): id(i), name(n), gender(g), idCard(c), birth(b) {} People(const People& p): id(p.id), name(p.name), gender(p.gender), idCard(p.idCard), birth(p.birth) {} virtual ~People() = default; virtual void show() const { cout << "编号:" << id << endl; cout << "姓名:" << name << endl; cout << "性别:" << gender << endl; cout << "身份证号:" << idCard << endl; cout << "出生日期:"; birth.show(); cout << endl; } }; class Student : virtual public People { private: string classNo; public: Student(const People& p, const string& c): People(p), classNo(c) {} void show() const override { People::show(); cout << "班号:" << classNo << endl; } }; class Teacher : virtual public People { private: string principalship; string department; public: Teacher(const People& p, const string& ps, const string& d): People(p), principalship(ps), department(d) {} void show() const override { People::show(); cout << "职务:" << principalship << endl; cout << "部门:" << department << endl; } }; class Graduate : public Student, public Teacher { private: string subject; Teacher* adviser; public: Graduate(const Student& s, const string& sub, Teacher* a): People(s), Student(s), Teacher(*a), subject(sub), adviser(a) {} void show() const override { People::show(); cout << "班号:" << classNo << endl; cout << "专业:" << subject << endl; cout << "导师:"; adviser->show(); } }; class TA : public Graduate, public Teacher { public: TA(const Graduate& g, const Teacher& t): People(g), Student(g), Graduate(g), Teacher(t) {} void show() const override { Graduate::show(); } }; int main() { Date t_d(1970, 1, 1); People t_p(14007, "李四", "man", "420107197001012020", t_d); Teacher t_t(t_p, "教授", "计算机科学与技术"); Date s_d(1998, 2, 2); People s_p(2020001, "张三", "man", "420107199802022046", s_d); Student s_s(s_p, "2002"); Graduate s_g(s_s, "计算机科学与技术", &t_t); Teacher s_t(s_s, "讲师", "计算机科学与技术"); TA ta(s_g, s_t); ta.show(); return 0; } 其中,为了实现虚基类的特性,我们在派生类中使用了虚拟继承。同时,为了避免出现二义性问题,我们在派生类的构造函数中通过明确调用基类的构造函数来初始化虚基类的成员。在派生类中重载基类的成员函数时,需要加上 override 关键字。最后,为了防止内存泄漏,我们在类的析构函数中使用了 default 关键字。
以下是一个基本的大学人员管理程序的实现: C++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person(string n, int a, string s) : name(n), age(a), sex(s) { sum++; } virtual void print() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Sex: " << sex << endl; } static int getSum() { return sum; } protected: string name; int age; string sex; static int sum; }; class Student : public Person { public: Student(string n, int a, string s, string spec) : Person(n, a, s), speciality(spec) {} void print() { Person::print(); cout << "Speciality: " << speciality << endl; } void saveToFile() { ofstream file("student.txt"); file << "Name: " << name << endl; file << "Age: " << age << endl; file << "Sex: " << sex << endl; file << "Speciality: " << speciality << endl; file.close(); } private: string speciality; }; class Teacher : public Person { public: Teacher(string n, int a, string s, string dept) : Person(n, a, s), department(dept) {} void print() { Person::print(); cout << "Department: " << department << endl; } void saveToFile() { ofstream file("teacher.txt"); file << "Name: " << name << endl; file << "Age: " << age << endl; file << "Sex: " << sex << endl; file << "Department: " << department << endl; file.close(); } private: string department; }; class Staff : public Person { public: Staff(string n, int a, string s, string dept, string position) : Person(n, a, s), department(dept), position(position) {} void print() { Person::print(); cout << "Department: " << department << endl; cout << "Position: " << position << endl; } void saveToFile() { ofstream file("staff.txt"); file << "Name: " << name << endl; file << "Age: " << age << endl; file << "Sex: " << sex << endl; file << "Department: " << department << endl; file << "Position: " << position << endl; file.close(); } private: string department; string position; }; int Person::sum = 0; int main() { Student s("Tom", 20, "Male", "Computer Science"); Teacher t("Jack", 35, "Male", "Computer Science"); Staff st("Lucy", 30, "Female", "Administration", "Secretary"); s.print(); s.saveToFile(); cout << endl; t.print(); t.saveToFile(); cout << endl; st.print(); st.saveToFile(); cout << endl; cout << "Total number of people: " << Person::getSum() << endl; return 0; } 在这个程序中,我们首先定义了一个抽象类 Person,其中包含了人员的最基本信息:姓名、年龄和性别,并声明了一个静态数据成员 sum,用于统计人数。接下来,我们定义了 Student、Teacher 和 Staff 三个类,它们分别继承自 Person,并在其中添加了各自的特性。每个类都有一个输出函数 print(),用于输出该类相关信息;以及一个保存函数 saveToFile(),用于将相关信息保存到文件中。 在 main() 函数中,我们创建了一个学生、一个教师和一个职工,并分别调用它们的 print() 和 saveToFile() 函数。最后,我们输出了总人数。 注意,在这里我们使用了 C++11 的初始化列表语法来对基类的成员进行初始化。同时,为了方便起见,我们在每个类的保存函数中都使用了 ofstream 类来将信息保存到文件中,而没有使用更加复杂的文件流操作。
好的,我可以帮你编写这个程序。下面是所需的三个类的代码实现: c++ #include <iostream> #include <string> using namespace std; // 基类Person class Person { protected: string name; char gender; int age; public: Person(string name, char gender, int age) : name(name), gender(gender), age(age) {} virtual void display() { cout << "Name: " << name << endl; cout << "Gender: " << gender << endl; cout << "Age: " << age << endl; } }; // 教师类Teacher class Teacher : public Person { private: string title; string course; public: Teacher(string name, char gender, int age, string title, string course) : Person(name, gender, age), title(title), course(course) {} void display() { cout << "Teacher:" << endl; Person::display(); cout << "Title: " << title << endl; cout << "Course: " << course << endl; } }; // 学生类Student class Student : public Person { private: string id; string department; public: Student(string name, char gender, int age, string id, string department) : Person(name, gender, age), id(id), department(department) {} void display() { cout << "Student:" << endl; Person::display(); cout << "ID: " << id << endl; cout << "Department: " << department << endl; } }; // 研究生类GradStudent class GradStudent : public Student { private: string tutor; public: GradStudent(string name, char gender, int age, string id, string department, string tutor) : Student(name, gender, age, id, department), tutor(tutor) {} void display() { cout << "Graduate Student:" << endl; Student::display(); cout << "Tutor: " << tutor << endl; } }; // 主函数 int main() { Person* people[3]; people[0] = new Teacher("Tom", 'M', 35, "Associate Professor", "Computer Science"); people[1] = new Student("Lucy", 'F', 20, "20191001", "Computer Science"); people[2] = new GradStudent("Jack", 'M', 25, "20202001", "Computer Science", "Mary"); for (int i = 0; i < 3; i++) { people[i]->display(); cout << endl; } for (int i = 0; i < 3; i++) { delete people[i]; } return 0; } 这个程序定义了三个类:Person、Teacher、Student和GradStudent,分别表示人员、教师、学生和研究生。每个类都只设立构造函数以及显示类对象数据的成员函数,并使用了虚函数实现多态。在主函数中,定义了一个基类指针数组,分别指向三个派生类的对象,并分别调用了它们的 display() 函数,实现了多态效果。

最新推荐

《面向对象c++》课程设计

(3)从people中派生student类,添加属性:班号 Char classNo[7]; (4)从people中派生出Teacher类,添加属性:职务 char principalship[11]、部门char department[21]; (5)从student类中派生出研究生...

ns_strings_zh.xml

ns_strings_zh.xml

库房物品统计表.xlsx

库房物品统计表.xlsx

用于全志 SOC 的微型 FEL 工具

XFEL系列,用于全志 SOC 的微型 FEL 工具。

对销售记录进行高级筛选.xlsx

对销售记录进行高级筛选.xlsx

基于51单片机的usb键盘设计与实现(1).doc

基于51单片机的usb键盘设计与实现(1).doc

"海洋环境知识提取与表示:专用导航应用体系结构建模"

对海洋环境知识提取和表示的贡献引用此版本:迪厄多娜·察查。对海洋环境知识提取和表示的贡献:提出了一个专门用于导航应用的体系结构。建模和模拟。西布列塔尼大学-布雷斯特,2014年。法语。NNT:2014BRES0118。电话:02148222HAL ID:电话:02148222https://theses.hal.science/tel-02148222提交日期:2019年HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire论文/西布列塔尼大学由布列塔尼欧洲大学盖章要获得标题西布列塔尼大学博士(博士)专业:计算机科学海洋科学博士学院对海洋环境知识的提取和表示的贡献体系结构的建议专用于应用程序导航。提交人迪厄多内·察察在联合研究单位编制(EA编号3634)海军学院

react中antd组件库里有个 rangepicker 我需要默认显示的当前月1号到最后一号的数据 要求选择不同月的时候 开始时间为一号 结束时间为选定的那个月的最后一号

你可以使用 RangePicker 的 defaultValue 属性来设置默认值。具体来说,你可以使用 moment.js 库来获取当前月份和最后一天的日期,然后将它们设置为 RangePicker 的 defaultValue。当用户选择不同的月份时,你可以在 onChange 回调中获取用户选择的月份,然后使用 moment.js 计算出该月份的第一天和最后一天,更新 RangePicker 的 value 属性。 以下是示例代码: ```jsx import { useState } from 'react'; import { DatePicker } from 'antd';

基于plc的楼宇恒压供水系统学位论文.doc

基于plc的楼宇恒压供水系统学位论文.doc

"用于对齐和识别的3D模型计算机视觉与模式识别"

表示用于对齐和识别的3D模型马蒂厄·奥布里引用此版本:马蒂厄·奥布里表示用于对齐和识别的3D模型计算机视觉与模式识别[cs.CV].巴黎高等师范学校,2015年。英语NNT:2015ENSU0006。电话:01160300v2HAL Id:tel-01160300https://theses.hal.science/tel-01160300v22018年4月11日提交HAL是一个多学科的开放获取档案馆,用于存放和传播科学研究文件,无论它们是否已这些文件可能来自法国或国外的教学和研究机构,或来自公共或私人研究中心。L’archive ouverte pluridisciplinaire博士之路博士之路博士之路在获得等级时,DOCTEURDE L'ÉCOLE NORMALE SUPERIEURE博士学校ED 386:巴黎中心数学科学Discipline ou spécialité:InformatiquePrésentée et soutenue par:马蒂厄·奥布里le8 may 2015滴度表示用于对齐和识别的Unité derechercheThèse dirigée par陪审团成员équipe WILLOW(CNRS/ENS/INRIA UMR 8548)慕尼黑工业大学(TU Munich�