Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. class Shape {
  9. protected:
  10.     int width, height;
  11. public:
  12.     Shape(int a = 0, int b = 0) {
  13.         width = a;
  14.         height = b;
  15.     }
  16.  
  17.     virtual void area() {
  18.         cout << "Parent class area" << endl;
  19.     }
  20. };
  21.  
  22. class Rectangle : public Shape {
  23. public:
  24.     Rectangle(int a = 0, int b = 0) : Shape(a, b) {}
  25.  
  26.     void area() {
  27.         cout << "Rectangle class area : " << (width * height) << endl;
  28.     }
  29. };
  30.  
  31. class Triangle : public Shape {
  32. public:
  33.     Triangle(int a = 0, int b = 0) : Shape(a, b) {}
  34.  
  35.     void area() {
  36.         cout << "Triangle class area : " << (width * height / 2) << endl;
  37.     }
  38. };
  39.  
  40. int main()
  41. {
  42.     Shape *shape[4];
  43.     Rectangle r1(5,3);
  44.     Rectangle r2(25,7);
  45.     Triangle t1(2, 6);
  46.     Triangle t2(7, 9);
  47.  
  48.     shape[0] = &r1;
  49.     shape[1] = &r2;
  50.     shape[2] = &t1;
  51.     shape[3] = &t2;
  52.  
  53.     for (int i = 0; i < 4; i++)
  54.     {
  55.         (*shape[i]).area();
  56.     }
  57.  
  58.     system("PAUSE");
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement