Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Pos
  5. {
  6. private:
  7. int x, y;
  8.  
  9. public:
  10. Pos(int x = 0, int y = 0)
  11. {
  12. this->x = x;
  13. this->y = y;
  14. }
  15. int getX() const
  16. {
  17. return x;
  18. }
  19. int getY() const
  20. {
  21. return y;
  22. }
  23. void show() const
  24. {
  25. cout << '(' << x << ',' << y << ')';
  26. }
  27. };
  28.  
  29. class Point : public Pos
  30. {
  31. private:
  32. int color;
  33.  
  34. public:
  35. Point(int x = 0, int y = 0, int color = 0) : Pos(x, y)
  36. {
  37. this->color = color;
  38. }
  39. int getColor() const
  40. {
  41. return color;
  42. }
  43. void show() const
  44. {
  45. cout << '[';
  46. Pos::show();
  47. cout << ':' << color << ']';
  48. }
  49. };
  50.  
  51. class Circle : public Point
  52. {
  53. private:
  54. int radius;
  55.  
  56. public:
  57. Circle(int x = 0, int y = 0, int color = 0, int radius = 0) : Point(x, y, color)
  58. {
  59. this->radius = radius;
  60. }
  61. int getRadius() const
  62. {
  63. return radius;
  64. }
  65. void show() const
  66. {
  67. cout << '{';
  68. Point::show();
  69. cout << ',' << radius << '}';
  70. }
  71. };
  72.  
  73. class Square : public Circle
  74. {
  75. public:
  76. Square(int x = 0, int y = 0, int color = 0, int radius = 0) : Circle(x, y, color, radius)
  77. {
  78.  
  79. }
  80. int getSide() const
  81. {
  82. return Circle::getRadius();
  83. }
  84. int getRadius() const
  85. {
  86. throw "No radius";
  87. // cout << "No radius!\n";
  88. // return -1;
  89. }
  90. };
  91.  
  92. int main()
  93. {
  94. Pos pos(3, 6);
  95. Point t(5, 10, 255*256);
  96. Circle c(1, 4, 255, 10);
  97. Square s(5, 5, 0, 30);
  98. pos.show();
  99. cout << endl;
  100. t.show();
  101. cout << endl;
  102. c.Pos::show();
  103. cout << endl;
  104. s.show();
  105. cout << endl;
  106. cout << s.getSide();
  107. cout << endl;
  108. try
  109. {
  110. cout << s.getRadius();
  111. } catch(const char *s)
  112. {
  113. cout << "Error: " << s;
  114. }
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement