A,simple,example,using,a,virtual,function,in,c++

Yavuz 9/19/2016 0

A simple example using a virtual function in C++

C++
#include <iostream>
using namespace std;

class BaseClass {
public:
  int i;
  BaseClass(int x) { 
     i = x; 
  }
  virtual void myFunction()
  {
    cout << "Using BaseClass version of myFunction(): ";
    cout << i << '\n';
  }
};

class DerivedClass1 : public BaseClass {
public:
  DerivedClass1(int x) : BaseClass(x) {}
  void myFunction() 
  {
    cout << "Using DerivedClass1's version of myFunction(): ";
    cout << i*i << '\n';
  }
};

class DerivedClass2 : public BaseClass {
public:
  DerivedClass2(int x) : BaseClass(x) {}
  void myFunction() 
  {
    cout << "Using DerivedClass2's version of myFunction(): ";
    cout << i i << '\n';
  }
};

int main()
{
  BaseClass *p;
  BaseClass ob(10);  
  DerivedClass1 derivedObject1(10);   
  DerivedClass2 derivedObject2(10); 

  p = &ob;
  p->myFunction();                         // use BaseClass's myFunction()

  p = &derivedObject1;
  p->myFunction();                         // use DerivedClass1's myFunction()

  p = &derivedObject2;
  p->myFunction();                         // use DerivedClass2's myFunction()

  return 0;
}
 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments