'Interface' for subclass : Interface « Class « C++






'Interface' for subclass

'Interface' for subclass

#include <iostream>
using namespace std;

class AreaClass {
public:
  double height;
  double width;
};

class Rectangle : public AreaClass {
public:
  Rectangle(double h, double w);
  double area();
};

class Isosceles : public AreaClass {
public:
  Isosceles(double h, double w);
  double area();
};

Rectangle::Rectangle(double h, double w)
{
  height = h;
  width = w;
}

Isosceles::Isosceles(double h, double w)
{
  height = h;
  width = w;
}

double Rectangle::area()
{
  return width * height;
}
 
double Isosceles::area()
{
  return 0.5 * width * height;
}

int main()
{
  Rectangle rectangleObject(10.0, 5.0);
  Isosceles isoscelesObject(4.0, 6.0);

  cout << "Rectangle: " << rectangleObject.area() << endl;
  cout << "Triangle: " << isoscelesObject.area() << endl;

  return 0;
}


           
       








Related examples in the same category

1.Use virtual function to define interface.Use virtual function to define interface.
2.Demonstrate virtual functons: list interfaceDemonstrate virtual functons: list interface
3.Virtual functions: interface classVirtual functions: interface class