Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Shape {
  5.    protected:
  6.       int width, height;
  7.    public:
  8.       Shape( int a=0, int b=0)
  9.       {
  10.          width = a;
  11.          height = b;
  12.       }
  13.       int area()
  14.       {
  15.          cout << "Parent class area :" <<endl;
  16.          return 0;
  17.       }
  18. };
  19. class Rectangle: public Shape{
  20.    public:
  21.       Rectangle( int a=0, int b=0):Shape(a, b) { }
  22.       int area ()
  23.       {
  24.          cout << "Rectangle class area :" <<endl;
  25.          return (width * height);
  26.       }
  27. };
  28. class Triangle: public Shape{
  29.    public:
  30.       Triangle( int a=0, int b=0):Shape(a, b) { }
  31.       int area ()
  32.       {
  33.          cout << "Triangle class area :" <<endl;
  34.          return (width * height / 2);
  35.       }
  36. };
  37. // Main function for the program
  38. int main( )
  39. {
  40.    Shape *shape;
  41.    Rectangle rec(10,7);
  42.    Triangle  tri(10,5);
  43.  
  44.    // store the address of Rectangle
  45.    shape = &rec;
  46.    // call rectangle area.
  47.    shape->area();
  48.  
  49.    // store the address of Triangle
  50.    shape = &tri;
  51.    // call triangle area.
  52.    shape->area();
  53.    
  54.    return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement