Advertisement
Guest User

Untitled

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