大佬教程收集整理的这篇文章主要介绍了C++多态和虚函数详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
//Inheritance4.h 的内容 #include <String> #include <memory> using namespace std; enum class Discipline { ARCHEOLOGY,BIOLOGY,COMPUTER_SCIENCE }; enum class Classification { FRESHMAN,SOPHOMORE,JUNIOR,SENIOR }; class Person { protected: 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: student(const String& sname,Discipline d,const shared_ptr<Person>& adv) : Person(sName) { major = d; advisor = adv; } 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: Faculty(const String& fname,Discipline d) : Person(fName) { department = d; } void setDepartment(Discipline d) { department = d; } Discipline getDepartment() const { return department; } }; class TFaculty : public Faculty { private: String title; public: TFaculty(const String& fname,String titlE):Faculty(fname,d) { settitle(titlE); } void settitle (const String& titlE) { this->title = title; } // Override getName() String getName() const { return title + " " + Person::getName(); } }; //main主程序 // This exhibits the default non-polymorphic behavior of C++. #include "inheritance4.h" #include <vector> #include <iostream> using namespace std; int main() { // Create a vector of pointers to Person objects vector<shared_ptr<Person>> people { make_shared<TFaculty> ("Indiana Jones",Discipline::ARCHEOLOGY,"Dr."),make_shared<student>("Thomas Cruise",Discipline::COMPUTER_SCIENCE,nullptr),make_shared<Faculty>("James Stock",Discipline::BIOLOGY),make_shared<TFaculty>("Sharon Rock",Discipline::BIOLOGY,"Professor"),make_shared<TFaculty>("Nicole Eweman","Dr.") }; // Print the names of the Person objects for (int k = 0; k < people.size (); k++) { cout << people[k]->getName() << endl; } return 0; }程序输出结果:
Indiana Jones
Thomas Cruise
James Stock
Sharon Rock
Nicole Eweman
people[k]->getName();
该函数用于检索要打印的名称。在每次调用中,指向基类 Person 的指针 people[k] 用于调用不同派生类的对象中的 getName 函数。其中的一些类,如 TFaculty,提供了更特殊化的版本来覆盖 getName 函数。当 people[k] 指向 TFaculty 对象时,编译器必须在 Person 中定义的 getName 与 TFaculty 中定义的 getName 之间进行选择。Person 是指针所属的类,TFaculty 是对象实际所属的类。class B { public: void mfun() { cout << "Base class version"; } }; class D : public B { public: void mfun() { cout << "Derived class version"; } }; shared_ptr<Base> ptr = make_shared<D>();根据前面的介绍可知,此时如果写入以下语句,那么将被调用的应该是基类 B 的成员函数而不是派生类 D 的成员函数:
ptr->mfun()
如果想让编译器选择使用派生类 D 中更特殊化的 mfun() 版本,那该怎么办呢?在 C++ 中,可以通过将 mfun() 声明为基类中的虚函数来执行此操作。C++ 中使用虚函数来支持多态行为。因此,为了实现基类 B 及其所有派生类中 mfun() 函数的多态行为,必须修改基类 B 中的定义如下:class B { public: virtual void mfun() { cout << "Base class version"; } };虚拟特性是可继承的。即如果派生类的成员函数覆盖了基类中的虚函数,那么该成员函数也会自动虚拟它本身。因此,在基类 B 中将 mfun 声明为虚函数,也将使 D 以及所有从 D 中派生的类中的 mfun 函数变成虚函数。
class D : public B { public: virtual void mfun() { cout << "Derived class version"; } };在本示例中,虚函数己经定义在类声明中。如果虚函数定义在类声明之外,则 virtual 关键字将继续对其类中声明有效,而对定义无效。C++ 不允许在类外面定义虚函数时加 virtual 关键字。
//Inheritance5.h 的内容 #include <String> #include <memory> using namespace std; enum class Discipline { ARCHEOLOGY,SENIOR }; class Person { protected: String name; public: Person() { setName(""); } Person(const String& pName) { setName(pName); } void setName(const String& pName) { name = pName; } //virtual function virtual String getName() const { return name; } }; class student:public Person { private: Discipline major; shared_ptr<Person> advisor; public: student(const String& sname,const shared_ptr<Person>& adv) : Person(sName) { major = d; advisor = adv; } 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: Faculty(const String& fname,const String& titlE):Faculty(fname,d) { settitle(titlE); } void settitle (const String& titlE) { this->title = title; } // Override getName() String getName() const { return title + " " + Person::getName(); } }; //main主程序 // This exhibits the default non-polymorphic behavior of C++. #include "inheritance5.h" #include <vector> #include <iostream> using namespace std; int main() { // Create a vector of pointers to Person objects vector<shared_ptr<Person>> people { make_shared<TFaculty> ("Indiana Jones","Dr.") }; // Print the names of the Person objects for (int k = 0; k < people.size (); k++) { cout << people[k]->getName() << endl; } return 0; }程序输出结果:
Dr. Indiana Jones
Thomas Cruise
James Stock
Professor Sharon Rock
Dr. Nicole Eweman
以上是大佬教程为你收集整理的C++多态和虚函数详解全部内容,希望文章能够帮你解决C++多态和虚函数详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。