MeehoweCK

Untitled

May 29th, 2021
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Shape
  6. {
  7. public:
  8.     Shape(double w, double h) : width(w), height(h) {}
  9.     double width;
  10.     double height;
  11.     virtual void hello() {cout << "hello from shape!";}
  12. };
  13.  
  14. class SecondShape
  15. {
  16. public:
  17.     SecondShape(double w, double h) : width(w), height(h) {}
  18.     double width;
  19.     double height;
  20.     bool other;
  21.     virtual void hello() {cout << "hello from shape!";}
  22. };
  23.  
  24. class Rectangle : public Shape, public SecondShape
  25. {
  26. public:
  27.     Rectangle(double w, double h) : Shape(w, h) {}
  28.     double area() {return width * height;}
  29.     double get_width() {return width;}
  30.     double get_height() {return height;}
  31. };
  32.  
  33. class Triangle : public Shape
  34. {
  35. public:
  36.     Triangle(double w, double h) : Shape(w, h) {}
  37.     double area() {return width * height / 2;}
  38.     double get_width() {return width;}
  39.     double get_height() {return height;}
  40. };
  41.  
  42. class VerySpecialTriangle : public Triangle
  43. {
  44. public:
  45.     VerySpecialTriangle(double w, double h) : Triangle(w, h), special(true) {}
  46.     bool special;
  47.     void display();
  48. };
  49.  
  50. void VerySpecialTriangle::display()
  51. {
  52.     cout << "Obiekt vst:\n";
  53.     cout << "\twysokosc: " << height << endl;
  54.     cout << "\tszerokosc: " << width << endl;
  55.     cout << "\tpole: " << area() << endl;
  56. }
  57.  
  58. int main()
  59. {
  60.     Triangle tri(4, 3);
  61.     Rectangle recti(10, 6);
  62.     cout << "Obiekt recti:\n";
  63.     cout << "\twysokosc: " << recti.get_height() << endl;
  64.     cout << "\tszerokosc: " << recti.get_width() << endl;
  65.     cout << "\tpole: " << recti.area() << endl;
  66.     /*cout << "Obiekt tri:\n";
  67.     cout << "\twysokosc: " << tri.get_height() << endl;
  68.     cout << "\tszerokosc: " << tri.get_width() << endl;
  69.     cout << "\tpole: " << tri.area() << endl;*/
  70.  
  71.     VerySpecialTriangle vst(10, 5);
  72.     cout << "Obiekt vst:\n";
  73.     cout << "\twysokosc: " << vst.get_height() << endl;
  74.     cout << "\tszerokosc: " << vst.get_width() << endl;
  75.     cout << "\tpole: " << vst.area() << endl;
  76.     vst.display();
  77.  
  78.     Shape* sptr = &vst;
  79.     sptr->hello();
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment