Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. // virtual members
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Polygon {
  7.  
  8. protected:
  9.  
  10. int width, height;
  11.  
  12. public:
  13.  
  14. void set_values (int a, int b)
  15. {
  16. width = a;
  17.  
  18. height = b;
  19. }
  20.  
  21. virtual int area ()
  22. {
  23.  
  24. return 0;
  25.  
  26. }
  27.  
  28. };
  29.  
  30. class Rectangle: public Polygon
  31. {
  32.  
  33. public:
  34.  
  35. int area ()
  36. {
  37. return width * height;
  38.  
  39. }
  40.  
  41. };
  42.  
  43. class Triangle: public Polygon {
  44.  
  45. public:
  46.  
  47. int area ()
  48. {
  49. return (width * height / 2);
  50. }
  51.  
  52. };
  53.  
  54. int main ()
  55. {
  56. Rectangle rect;
  57.  
  58. Triangle trgl;
  59.  
  60. Polygon poly;
  61.  
  62. Polygon * ppoly1 = &rect;
  63.  
  64. Polygon * ppoly2 = &trgl;
  65.  
  66. Polygon * ppoly3 = &poly;
  67.  
  68. ppoly1->set_values (4,5);
  69.  
  70. ppoly2->set_values (4,5);
  71.  
  72. ppoly3->set_values (4,5);
  73.  
  74. cout << ppoly1->area() << endl;
  75.  
  76. cout << ppoly2->area() << endl;
  77.  
  78. cout << ppoly3->area() << endl;
  79.  
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement