Pages

Sunday, July 21, 2013

Overriding method example

/* Programmer: ARUN ANOOP M
   Job Details: Asst.professor,CSE,MESCE */

#include <iostream.h>
#include<conio.h>
class arithmetic
{
  protected:
    int a, b, sum, sub;
  public:
    void values (int x, int y)
      {
a=x, b=y;
      }
    virtual int operations ()
      {
sum= a + b;
cout<< "Addition = "<< sum<<"\n";

      }
};

class Subtract: public arithmetic
{
  public:
    int operations ()
      {
sub= a - b;
cout<< "Difference = "<<sub <<"\n";
      }
};                        


int main()
{                      
    clrscr();
    arithmetic *arith;
   
Subtract subt;
    arithmetic ar;
 
    arith=&ar;
    arith->values(30,12);
    arith->operations();

    arith=&subt;
    arith->values(42,5);
    arith->operations();

getch();
return 0;
}

No comments:

Post a Comment