c++ 35虚函数与多态(二)

来源:互联网 发布:python 自动化 工业 编辑:程序博客网 时间:2024/06/06 00:06

基类 之指针指向派生类之对象,调用的是派生类的虚函数

这就使得我们可以一致的观点看待不同的派生类对象

抽象类不能实例化 




#include <iostream>using namespace std;#include <vector>class Shape{public:virtual void Draw()=0;//只要有一个虚函数,这个类就是抽象类  而这里去掉virtual 则会调用基类Draw/*void Draw(){   cout<<"Shape Draw"<<endl;}*/};class Circle:public Shape{public:void Draw(){   cout<<"Circle Draw"<<endl;}};class Square:public Shape{public:void Draw(){   cout<<"Square Draw"<<endl;}};void DrawAllShape(const vector<Shape*>& v){vector<Shape*>::const_iterator it;for(it=v.begin();it!=v.end();++it){(*it)->Draw();  //*it 就是Shape*}}int main(void){//Shape sp;//error 不能实例化抽象类vector<Shape*> v;Shape* ps;ps=new Circle;v.push_back(ps);ps=new Square;v.push_back(ps);DrawAllShape(v);//这里打印结构是//Circle Draw//Square Draw//而如果不用虚函数 则 调用Shape Draw  return 0;}

#include <iostream>using namespace std;#include <vector>class Shape{public:virtual void Draw()=0;//只要有一个虚函数,这个类就是抽象类  而这里去掉virtual 则会调用基类Draw/*void Draw(){   cout<<"Shape Draw"<<endl;}*/virtual ~Shape()//这样不回产生内存泄漏问题 会调用派生类析构函数,然后调用基类析构函数{   cout<<"~Shape.."<<endl;}};class Circle:public Shape{public:void Draw(){   cout<<"Circle Draw"<<endl;}~Circle(){  cout<<"~Circle"<<endl;}};class Square:public Shape{public:void Draw(){   cout<<"Square Draw"<<endl;}~Square(){   cout<<"~Square"<<endl;}};void DrawAllShape(const vector<Shape*>& v){vector<Shape*>::const_iterator it;for(it=v.begin();it!=v.end();++it){(*it)->Draw();  //*it 就是Shape*}}void DeleteAllShape(const vector<Shape*>& v){vector<Shape*>::const_iterator it;for(it=v.begin();it!=v.end();++it){delete(*it);}}int main(void){//Shape sp;//error 不能实例化抽象类vector<Shape*> v;Shape* ps;ps=new Circle;v.push_back(ps);ps=new Square;v.push_back(ps);DrawAllShape(v);DeleteAllShape(v);  return 0;}





#include <iostream>using namespace std;#include <vector>#include <string>class Shape{public:virtual void Draw()=0;//只要有一个虚函数,这个类就是抽象类  而这里去掉virtual 则会调用基类Draw/*void Draw(){   cout<<"Shape Draw"<<endl;}*/virtual ~Shape()//这样不回产生内存泄漏问题 会调用派生类析构函数,然后调用基类析构函数{   cout<<"~Shape.."<<endl;}};class Circle:public Shape{public:void Draw(){   cout<<"Circle Draw"<<endl;}~Circle(){  cout<<"~Circle"<<endl;}};class Square:public Shape{public:void Draw(){   cout<<"Square Draw"<<endl;}~Square(){   cout<<"~Square"<<endl;}};class Rentanle:public Shape{public:void Draw(){   cout<<"Rentanle Draw"<<endl;}~Rentanle(){   cout<<"~Rentanle"<<endl;}};void DrawAllShape(const vector<Shape*>& v){vector<Shape*>::const_iterator it;for(it=v.begin();it!=v.end();++it){(*it)->Draw();  //*it 就是Shape*}}void DeleteAllShape(const vector<Shape*>& v){vector<Shape*>::const_iterator it;for(it=v.begin();it!=v.end();++it){delete(*it);}}//简单工厂模式  下次研究动态创建class ShapeFactory{public:static Shape* CreateShape(const string& name){Shape* ps=0;   if(name=="Circle")   {   ps=new Circle;   }else if(name=="Square")   {   ps=new Square;   }else if(name=="Rentanle")   {       ps=new Rentanle;   }   return ps;}};int main(void){//Shape sp;//error 不能实例化抽象类vector<Shape*> v;Shape* ps;ps=ShapeFactory::CreateShape("Circle");v.push_back(ps);ps=ShapeFactory::CreateShape("Square");v.push_back(ps);    ps=ShapeFactory::CreateShape("Rentanle");v.push_back(ps);/*ps=new Circle;v.push_back(ps);ps=new Square;v.push_back(ps);ps=new Rentanle;v.push_back(ps);*///能够适应未来的变化DrawAllShape(v);DeleteAllShape(v);  return 0;}





什么时候析构函数还可以是纯虚的呢?

#include <iostream>using namespace std;//对于一个没有任何接口的类,如果想要将它定义成抽象类,//只能将虚析构函数声明为纯虚的。//通常情况下在基类中纯虚函数不需要实现//例外是:纯虚析构函数 要给出实现(给出一个空的即可)class Base{  //如果一个类没有任何成员函数,并且希望这个类是抽象类public:virtual ~Base()=0{//析构函数可以是纯虚的。cout<<"~Base.."<<endl;}};class Drived:public Base{  };int main(void){//Base b; error 析构函数是纯虚的,抽象类  通常情况下,纯虚函数是不需要实现的。但是纯虚析构函数是例外Drived d; //编译失败的原因,是先调用派生类的析构函数,再调用基类的析构函数,但是这里没有实现。  return 0;}


0 0
原创粉丝点击