Wednesday, March 18, 2015

virtual function of base called from derived class

#include<iostream>
#include<conio.h>


using namespace std;

class Area
{
public:
 virtual double GetArea(int length, int breadth)
 {
  return length * breadth;
 }
};

class Rectangle : Area
{
public:
 double  GetArea(int length, int breadth)
 {
  return Area::GetArea(length, breadth);
 }
};

class Triangle : Area
{

public:
 double GetArea(int length, int breadth)
 {
  return 0.5 * Area::GetArea(length, breadth);
 }
};

void main()
{
 Rectangle oRectangle;
 Triangle oTriangle;

 cout<<"\nArea of Rectangle:"<< oRectangle.GetArea(10, 2);
 cout << "\nArea of Triangle:"<<oTriangle.GetArea(10, 2);

 _getch();

}

Out Put :
Area of Rectangle: 20
Area of Triangle:10

No comments:

Post a Comment