Polymorphism with base class pointer : Polymorphism « Class « C++






Polymorphism with base class pointer

  
#include <iostream>
#include <stdlib.h>
using namespace std;
class Base 
{
 public:
   virtual int add(int a, int b) { return(a + b); };
   virtual int sub(int a, int b) { return(a - b); };
   virtual int mult(int a, int b) { return(a * b); };
};

class ShowMath : public Base 
{
   virtual int mult(int a, int b) 
    { 
      cout << a * b << endl; 
      return(a * b); };
};

class PositiveSubt : public Base 
{
   virtual int sub(int a, int b) { return(abs(a - b)); };
};

int main(void)
 {
   Base *poly = new ShowMath;

   cout << poly->add(562, 531) << ' ' << poly->sub(1500, 407) << endl; 
   poly->mult(1093, 1);

   poly = new PositiveSubt;
   cout << poly->add(892, 201) << ' ' << poly->sub(0, 1093) << endl;
   cout << poly->mult(1, 1093);
 }
  
    
  








Related examples in the same category

1.Use virtual functions and polymorphism. Use virtual functions and polymorphism.
2.Object array: polymorphism. Object array: polymorphism.
3.An example that uses typeid on a polymorphic class hierarchyAn example that uses typeid on a polymorphic class hierarchy
4.dynamic allocation and polymorphism