多态 Poly

来源:互联网 发布:网络共享中心打不开 编辑:程序博客网 时间:2024/04/29 23:48
#include<iostream>
 using namespace std;
 class A
 {
 public:
  virtual void fun1();   //纯虚函数?
  void fun2(){fun1();}
 };
 void A::fun1()
 {
  cout<<"A::fun1 was called."<<endl;
 }
 //inherit
 class B : public A
 {
 public:
  void fun1();
 };
 void B::fun1()
 {
  cout<<"B::fun1 was called."<<endl;
 }
 int main()            //template designed model
 {
  A aa;
  aa.fun2();
  A *a=new B;
  a->fun2();
  B b;
  b.fun2();
  A *bb=new A;
  bb->fun2();
  delete bb;
  delete a;
  return 0;

 }


原创粉丝点击