大佬教程收集整理的这篇文章主要介绍了C++继承(详解版),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
class Person { private: String name; public: Person () { setName (""); } Person(String pName) {setName(pName);} void setName(String pName) { name = pName; } String getName() const { return name;} };进一步假设还有以下枚举类型:
enum Discipline {ARCHEOLOGY,BIOLOGY,COMPUTER_SCIENCE}; enum Classification {FRESHMAN,SOPHOMORE,JUNIOR,SENIOR};这些枚举类型定义了学科范围和学生分类。可以使用这些类型来定义 student 和 Faculty 类,这两个类都继承自 Person 类,这样做当然是有道理的,因为@L_801_0@ student 是@L_801_0@ Person,@L_801_0@ Faculty 也是@L_801_0@ Person。
class student : public Person { private: Discipline major; shared_ptr<Person> advisor; public: void setMajor(Discipline d) {major = d;} Discipline getMajor() const {return major;} void setAdvisor(Person *p) {advisor = p;} shared_ptr<Person> getAdvisor() const {return advisor;} };假设很多不同的学生可能有相同的指导老师,所以,如果每个 student 对象都存储@L_801_0@指导老师的副本将导致不必要的重复,并且在指导老师的数据更新时也会导致其每个 student 对象不得不更新。为了避免这些问题,可以让 student 对象存储@L_801_0@指向指导老师的指针。
class Faculty : public Person { private: Discipline department; public: void setDepartment(Discipline d) {department = d;} Discipline getDepartment( ) const {return department;} };因此,@L_801_0@ Faculty 对象也是@L_801_0@ Person 对象,而且有自己的学科部门。还有一点很重要,派生类的每个对象都将包含基类的所有成员。如图 4 所示是 student 类和 Person 类所包含成员的示意图。
Person p; student s; p = s;但是,student 对象有它自己的成员,无法纳入为 Person 对象分配的内存位置中。为了执行这样的赋值语句,编译器将需要裁剪掉 student 中不属于 Person 的部分,仅保留继承的部分,这样就导致了信息的丢失。如果使用指针的话就不会出现这种问题了,因为所有的指针都是一样大小的。
//Inheritance.h的内容 #include <String> #include <memory> using namespace std; enum Discipline { ARCHEOLOGY,COMPUTER_SCIENCE }; enum Classification { FRESHMAN,SENIOR }; class Person { private: String name; public: Person() { setName(""); } Person(const String& pName) { setName(pName); } void setName(const String& pName) { name = pName; } String getName() const { return name; } }; class student:public Person { private: Discipline major; shared_ptr<Person> advisor; public: void setMajor(Discipline d) { major = d; } Discipline getMajor() const { return major; } void setAdvisor(shared_ptr<Person> p) { advisor = p; } shared_ptr<Person> getAdvisor() const { return advisor; } }; class Faculty:public Person { private: Discipline department; public: void setDepartment(Discipline d) { department = d; } Discipline getDepartment( ) const { return department; } }; //main函数 // This program demonstrates the creation and use // of objects of derived classes. #include <iostream> #include "inheritance.h" using namespace std; // these arrays of String are used to print the // enumerated types. const String dName[] = {"Archeology","Biology","Computer Science"}; const String cName[] = {"Freshman","Sophomore","Junior","Senior"}; int main() { // Create a Faculty object shared_ptr<Faculty> prof = make_shared<Faculty>(); // Use a Person member function to set name prof->setName ("Indiana Jones"); // Use a Faculty member function to set Department prof->setDepartment(Discipline::ARCHEOLOGY); cout << "Professor " << prof->getName() << " teaches in the " << "Department of "; // Get department as an enumerated type Discipline dept = prof->getDepartment(); // Print out the department in String form cout << dName[static_cast<int>(dept)] << endl; return 0; }程序输出结果:
Professor Indiana Jones teaches in the Department of Archeology
以上是大佬教程为你收集整理的C++继承(详解版)全部内容,希望文章能够帮你解决C++继承(详解版)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。