第十六章 多态性(一)
//第一节的内容是多重继承,由于前面在继承那一章已经粗略地了解了多种继承,所以本节及后面几节主要是说明"为什么要使用多重继承" /* #include <iostream> using namespace std; class father { public: void smart() { cout<<"父亲很聪明"<<endl; } //由于父亲不美丽,所以该类中的beautiful函数什么功能都没有,只是起到一个接口的作用, //目的是为了让son类来继承 virtual void beautiful(){} virtual ~father(){cout<<"析构father"<<endl;} }; class son:public father { public: virtual void beautiful(){cout<<"儿子也很帅"<<endl;} ~son(){ cout<<"析构son"<<endl; } }; int main() { father *pf; int choice = 0; bool quit = false; while(quit == false){ cout<<"(0)退出 (1)父亲 (2)儿子"<<endl; cin>>choice; switch(choice) { case 0: quit = true; break; case 1: pf = new father; pf->beautiful(); break; case 2: pf = new son; pf->beautiful(); pf->smart(); delete pf; break; default: cout<<"请输入从0到2之间的数字"<<endl; break; } if(quit) { break; } } return 0; }*/ //2 在派生类中增加函数 //假如我们不想使用多重继承,又不想在基类中添加多余的接口函数,那 /* #include <iostream> using namespace std; class father { public: void smart() { cout<<"父亲很聪明"<<endl; } //由于父亲不美丽,所以该类中的beautiful函数什么功能都没有,只是起到一个接口的作用, //目的是为了让son类来继承 //virtual void beautiful(){} virtual ~father(){cout<<"析构father"<<endl;} }; class son:public father { public: virtual void beautiful(){cout<<"儿子也很帅"<<endl;} ~son(){ cout<<"析构son"<<endl; } }; int main() { father *pf; int choice = 0; bool quit = false; while(quit == false){ cout<<"(0)退出 (1)父亲 (2)儿子"<<endl; cin>>choice; switch(choice) { case 0: quit = true; break; case 1: pf = new father; //pf->beautiful(); break; case 2: pf = new son; //pf->beautiful(); //father类已经没有beautiful()函数,所以这里不能访问 //只能用指针转换 dynamic_cast<son*>(pf)->beautiful(); //因此我们必须将基类指针强制转换为子类指针,这里要用到一个方法--dynamic_cast //dynacic_cast的作用是对不同类之间的数据类型进转换,它可一个基类的指针转换成一个派生类的指针 pf->smart(); delete pf; break; default: cout<<"请输入从0到2之间的数字"<<endl; break; } if(quit) { break; } } return 0; }*/ //3 使用多重继承 /* #include <iostream> using namespace std; class father { public: void smart() { cout<<"父亲很聪明"<<endl; } //由于父亲不美丽,所以该类中的beautiful函数什么功能都没有,只是起到一个接口的作用, //目的是为了让son类来继承 //virtual void beautiful(){} father(){cout<<"构造函数father"<<endl;} virtual ~father(){cout<<"析构father"<<endl;} }; class mother { public: virtual void beautiful(){cout<<"母亲很漂亮"<<endl;} mother(){cout<<"构造monther"<<endl;} virtual ~mother(){cout<<"析构mother类"<<endl;} }; class son:public father, public mother { public: virtual void beautiful(){cout<<"儿子也很帅"<<endl;} son(){cout<<"构造son"<<endl;} virtual ~son(){ cout<<"析构son"<<endl; } }; int main() { father *pf; mother *pm; int choice = 0; bool quit = false; while(quit == false){ cout<<"(0)退出 (1)父亲 (2)儿子"<<endl; cin>>choice; switch(choice) { case 0: quit = true; break; case 1: pf = new father; break; case 2: pm = new son; pm->beautiful(); delete pm; break; default: cout<<"请输入从0到2之间的数字"<<endl; break; } if(quit) { break; } } return 0; }*/ //4 模拟抽像类 /* #include <iostream> using namespace std; class Human { public: Human(){cout<<"构造Human"<<endl;} virtual void smart(){} virtual void beautiful(){} virtual ~Human(){cout<<"析构Human"<<endl;} }; //将Human继承为 class father : virtual public Human { public: void smart() { cout<<"父亲很聪明"<<endl; } father(){cout<<"构造函数father"<<endl;} virtual ~father(){cout<<"析构father"<<endl;} }; class mother: virtual public Human { public: virtual void beautiful(){cout<<"母亲很漂亮"<<endl;} mother(){cout<<"构造monther"<<endl;} virtual ~mother(){cout<<"析构mother类"<<endl;} }; class son:public father, public mother { public: virtual void beautiful(){cout<<"儿子也很帅"<<endl;} virtual void smart(){cout<<"儿子也很聪明"<<endl;} son(){cout<<"构造son"<<endl;} virtual ~son(){ cout<<"析构son"<<endl; } }; int main() { //father *pf; //mother *pm; Human *p; int choice = 0; bool quit = false; while(quit == false){ cout<<"(0)退出 (1)父亲 (2)儿子 (3)母亲"<<endl; cin>>choice; switch(choice) { case 0: quit = true; break; case 1: p = new father; p->beautiful(); delete p; break; case 2: p = new son; //这里产生了两义性,因为父亲和母亲类都是从人类来的,一分是从母类,一分是从父类 p->beautiful(); p->smart(); delete p; break; case 3: p = new mother; p->beautiful(); delete p; default: cout<<"请输入从0到2之间的数字"<<endl; break; } if(quit) { break; } } return 0; }*/ //最后我们需要注意的是,这个抽像类并不是真正的抽像类,它只是一个虚拟的抽像类,因为它的虚函数仍然有功能 //那就是什么事也不做,真正的抽像类必须一个或者一个以上真正没有任何功能的虚函数,这个虚函数的存在仅仅是为了让它的子类来继承并 //有功能,关于这一点将在下节课中讲解 //5 纯虚函数与抽像类 //一个虚函数通过初始化为0就变成了纯虚函数 /* #include <iostream> using namespace std; class Human { public: Human(){cout<<"构造Human"<<endl;} virtual void smart()=0; //定义一个纯虚函数 //一个类可以有多个纯虚函数,包含有纯函虚函数的类叫抽像类 //注意一点,继承抽像类的子类,必须要定重新定义纯虚函数 //它不是上一节的那个模拟的抽像类,而是真正的抽像类 virtual void beautiful()=0; virtual ~Human(){cout<<"析构Human"<<endl;} }; //将Human继承为 class father : virtual public Human { public: virtual void smart() { cout<<"父亲很聪明"<<endl; } virtual void beautiful(){cout<<"父亲很帅"<<endl;} father(){cout<<"构造函数father"<<endl;} virtual ~father(){cout<<"析构father"<<endl;} }; class mother: virtual public Human { public: virtual void beautiful(){cout<<"母亲很漂亮"<<endl;} virtual void smart(){cout<<"母亲不太聪明"<<endl;} mother(){cout<<"构造monther"<<endl;} virtual ~mother(){cout<<"析构mother类"<<endl;} }; class son:public father, public mother { public: virtual void beautiful(){cout<<"儿子也很帅"<<endl;} virtual void smart(){cout<<"儿子也很聪明"<<endl;} son(){cout<<"构造son"<<endl;} virtual ~son(){ cout<<"析构son"<<endl; } }; int main() { //father *pf; //mother *pm; //Human *p = new Human; //这样是错误的 Human *p; int choice = 0; bool quit = false; while(quit == false){ cout<<"(0)退出 (1)父亲 (2)儿子 (3)母亲"<<endl; cin>>choice; switch(choice) { case 0: quit = true; break; case 1: p = new father; p->beautiful(); p->smart(); delete p; break; case 2: p = new son; //这里产生了两义性,因为父亲和母亲类都是从人类来的,一分是从母类,一分是从父类 p->beautiful(); p->smart(); delete p; break; case 3: p = new mother; p->beautiful(); p->smart(); delete p; default: cout<<"请输入从0到2之间的数字"<<endl; break; } if(quit) { break; } } return 0; }*/ //在该例中,Human类仅仅起到了为派生类提供一个接口的作用,这个接口类的纯虚函数可以没有任何作用 //派生类将这些纯虚函数继承过来,然后给予它不同的功能。 //注意:抽像类不是一个实际的类,所以不能定义一个抽像是类的对像 //需要你不可以定义一个抽像类的对像,但你能定义一个指向抽像类的指针 //这样就可以的,p既是一个指向抽像类的指针,同时又是一个指向基类的指针,因此在程序编译或运行时 //这个指针可以动态地指向子类的对像,从而实现程序的多态性 //抽像类的实例 /* #include <iostream> using namespace std; class Shape { public: virtual double area()=0; }; //三角形 class Trigon: public Shape { protected: double h, w; public: Trigon(double H, double W) { h = H; w = W; } double area() { return h * w / 2; }; }; //正方形 class Square : public Trigon { public: Square(double H, double W):Trigon(H, W) { } double area(){return h*w;} }; //圆 class Circle: public Shape { protected: double radius; public: Circle(double r){radius = r;} double area(){ return radius * radius * 3.14; } }; int main() { Shape *p; int choice = 0; while(1) { bool quit = false; cout<<"(0)退出 (1)三角形 (2)正方形 (3)圆"<<endl; cout<<"请选择"<<endl; cin>>choice; switch(choice) { case 0: quit = true; break; case 1: p = new Trigon(5.0,6.0); cout<<"三角形的面积是:"<<p->area()<<endl; break; case 2: p = new Square(70.0,70.0); cout<<"正方形的面积是:"<<p->area()<<endl; break; case 3: p = new Circle(9.0); cout<<"圆的面积是:"<<p->area()<<endl; break; default: cout<<"请输入0到3之间的数字"<<endl; break; } if(choice < 4 && choice > 0){ delete p; } if(quit == true){ break; } }; return 0; }*/