c++学习-多态性
强制转换父类对象为子类
#include<iostream> #include<string> #include <typeinfo> using namespace std; class father{ public: void smart(){} virtual ~father(){} }; class son : public father { public: void say() { cout << "say" << endl; } }; void main() { father *p; p= new son; dynamic_cast<son*>(p)->say(); }
多重继承
#include<iostream> #include<string> #include <typeinfo> using namespace std; class father{ public: father(){ cout << "father cons" << endl; } virtual ~father(){ cout << "father des" << endl; } virtual void say() { cout << "father say" << endl; } }; class mother{ public: mother(){ cout << "mother cons" << endl; } virtual ~mother(){ cout << "mother des" << endl; } virtual void run() { cout << "mother run" << endl; } }; class son : public mother, public father { public: void run() { cout << "son run" << endl; } son(){ cout << "son cons" << endl; } ~son(){ cout << "son des" << endl; } }; void main() { mother *p; p = new son; p->run(); delete p; //dynamic_cast<son*>(p)->say(); }
同时访问 父类和母类中的函数:
模拟抽象类
#include<iostream> #include<string> #include <typeinfo> using namespace std; class human{ public: virtual void say(){} virtual void run(){} virtual ~human(){ cout << "human des" << endl; } human(){ cout << "human cons" << endl; } }; class father:virtual public human{ //虚基类 public: father(){ cout << "father cons" << endl; } ~father(){ cout << "father des" << endl; } void say() { cout << "father say" << endl; } }; class mother :virtual public human{ public: mother(){ cout << "mother cons" << endl; } ~mother(){ cout << "mother des" << endl; } void run() { cout << "mother run" << endl; } }; class son : public mother, public father { public: son(){ cout << "son cons" << endl; } ~son(){ cout << "son des" << endl; } }; void main() { human *p; p = new son; delete p; }
抽象类:
#include<iostream> #include<string> #include <typeinfo> using namespace std; class human{ public: virtual void say() = 0;//纯虚函数 virtual void run() = 0; virtual ~human(){ cout << "human des" << endl; } human(){ cout << "human cons" << endl; } }; class father:virtual public human{ //虚基类 public: father(){ cout << "father cons" << endl; } ~father(){ cout << "father des" << endl; } void say() { cout << "father say" << endl; } }; class mother :virtual public human{ public: mother(){ cout << "mother cons" << endl; } ~mother(){ cout << "mother des" << endl; } void run() { cout << "mother run" << endl; } }; class son : public mother, public father { public: son(){ cout << "son cons" << endl; } ~son(){ cout << "son des" << endl; } }; void main() { human *p; p = new son; p->say(); delete p; }
抽象类实例:
#include<iostream> #include<string> #include <typeinfo> using namespace std; class alpha{ public: virtual void ee(){}; virtual ~alpha(){ cout << "alpha des" << endl; }; alpha(){ cout << "alpha cons" << endl; }; }; class A: public alpha{ public: void ee(){ cout <<"A"<< endl; } A(){ cout <<"A cons"<< endl; }; ~A(){ cout << "A des" << endl; }; }; class B : public A{ public: void ee(){ cout << "B" << endl; } ~B(){} }; void main() { alpha *p; p = new A; p->ee(); p = new B; p->ee(); delete p; }