overring virtual functions

#include <iostream>
#include <complex>

using namespace std;

class Base
{
public:

  virtual void f( int );
  virtual void f(double);
  virtual void g(int i = 10);
};


void Base::f( int )
{
  cout << "Base::f(int)" << endl;
}

void Base::f( double )
{
  cout << "Base::f(double)" << endl;
}

void Base::g( int i )
{
  cout << i << endl;
}

class Derived: public Base
{
public:
// using Base::f
  void f(complex<double>); // hide the Base f.
  // overrides Base::g but changes the default parameter.
  void g( int i = 20 );
};

void Derived::f(complex<double>)
{
  cout << "Derived::f(complex)" << endl;
}

void Derived::g(int i)
{
 cout << "Derived::g() " << i << endl;
}

void main() // int
{

  Base    b;
  Derived d;
  Base*   pb = new Derived;

  b.f(1.0); // Base::f(double)

  d.f(1.0); // Derived::f(complex)

  pb->f(1.0); // Base::f( double ) notice? *****

  b.g(); // 10

  d.g(); //Derived::g() 20

  pb->g(); //Derived::g() 10 notice?  *****
  delete pb; // unsafe ?


}


發佈了42 篇原創文章 · 獲贊 0 · 訪問量 8600
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章