fork download
  1. #include <iostream>
  2. #include <cmath> //sqrt
  3. #include <vector> //std::vector
  4.  
  5. //You can use double instead of float if you want it to be more accurate
  6. //but for these I don't think they really need to be more than a few
  7. //digits
  8. struct Point //this will be a basic point without operators
  9. {
  10. float x , y;
  11. };
  12.  
  13. struct BoundingBox
  14. {
  15. float top , left , width , height;
  16. };
  17.  
  18. class Shape
  19. {
  20. public:
  21. virtual ~Shape(){}
  22. virtual float area( void ) const = 0;
  23. virtual float perimeter( void ) const = 0; //circumference for circles but oh well
  24. protected:
  25. Point _origin;
  26. BoundingBox bounds;
  27. };
  28.  
  29. //if you have a square derive from rectangle
  30. //you might want to use virtual publics or consider
  31. //composition instead of inheritance hierarchy
  32.  
  33. class Square : public Shape
  34. {
  35. public:
  36. Square( const float width , const Point origin );
  37. float area( void ) const;
  38. float perimeter( void ) const;
  39. private:
  40. float width;
  41. };
  42.  
  43. Square::Square( const float width , const Point origin ) : width( width )
  44. {
  45. _origin = origin;
  46. bounds.left = origin.x - width / 2.0f;
  47. bounds.top = origin.y - width / 2.0f;
  48. bounds.width = width;
  49. bounds.height = width;
  50. }
  51.  
  52. float Square::area( void ) const
  53. {
  54. return width * width;
  55. }
  56.  
  57. float Square::perimeter( void ) const
  58. {
  59. const int sides = 4;
  60. return sides * width;
  61. }
  62.  
  63.  
  64. class Circle : public Shape
  65. {
  66. public:
  67. Circle( const float radius , const Point origin );
  68. float area( void ) const;
  69. float perimeter( void ) const; //yeah its circum but in my example its perim
  70. private:
  71. float radius;
  72. };
  73.  
  74. Circle::Circle( const float radius , const Point origin ) : radius( radius )
  75. {
  76. _origin = origin;
  77. bounds.left = origin.x - radius;
  78. bounds.top = origin.y + radius;
  79. bounds.width = radius * 2; //radius * 2 = diameter
  80. bounds.height = bounds.width; //height and width are same in a circle
  81. }
  82.  
  83. float Circle::area( void ) const
  84. {
  85. const float pi = 3.14159f;
  86. return pi * radius * radius;
  87. }
  88.  
  89. float Circle::perimeter( void ) const
  90. {
  91. const float pi = 3.14159;
  92. return 2 * pi * radius;
  93. }
  94.  
  95. //mine is a Equilateral Triangle
  96. class Triangle : public Shape
  97. {
  98. public:
  99. Triangle( const float length , const Point origin );
  100. float area( void ) const;
  101. float perimeter( void ) const;
  102. private:
  103. float length;
  104. };
  105.  
  106. Triangle::Triangle( const float length , const Point origin ) : length( length )
  107. {
  108. _origin = origin;
  109. bounds.left = origin.x - length / 2.0f;
  110. bounds.top = origin.y - length / 2.0f;
  111. bounds.width = length;
  112. bounds.height = sqrt( length * length - length / 2.0f * length / 2.0f );
  113. }
  114.  
  115. float Triangle::area( void ) const
  116. {
  117. return length * bounds.height / 2.0f;
  118. }
  119.  
  120. float Triangle::perimeter( void ) const
  121. {
  122. const int sides = 3;
  123. return sides * length;
  124. }
  125.  
  126. int main()
  127. {
  128. Point origin{ 10 , 5 }; //center of shape is at 10 , 5
  129.  
  130. std::vector<Shape *> shapes; //smart pointer would be better
  131.  
  132. shapes.push_back( new Triangle(10,origin) );
  133. shapes.push_back( new Circle(5,origin) );
  134. shapes.push_back( new Square(10,origin) );
  135.  
  136. for( const auto &it : shapes )
  137. {
  138. //triangle , circle , square
  139. std::cout << "Area: " << it->area() << " Perimeter: " << it->perimeter() << std::endl;
  140. delete it;
  141. }
  142.  
  143. return 0;
  144. }
  145.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Area: 43.3013 Perimeter: 30
Area: 78.5397 Perimeter: 31.4159
Area: 100 Perimeter: 40