Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. #include "shape.hpp"
  2.  
  3.  
  4.  
  5. //Base class
  6. //Please implement Shape's member functions
  7. //constructor, getName()
  8. //
  9. //Base class' constructor should be called in derived classes'
  10. //constructor to initizlize Shape's private variable
  11. Shape::Shape(std::string name) {
  12.     name_ = name;
  13. }
  14.  
  15. std::string Shape::getName() {
  16.     return name_;
  17. }
  18.  
  19.  
  20.  
  21. //Rectangle
  22. //Please implement the member functions of Rectangle:
  23. //constructor, getArea(), getVolume(), operator+, operator-
  24. //@@Insert your code here
  25. Rectangle::Rectangle(double width, double length) : Shape("Rectangle") {
  26.     width_  = width;
  27.     length_ = length;
  28. }
  29.  
  30.  
  31.  
  32. double Rectangle::getVolume()const{return (double) 0;}
  33. double Rectangle::getArea()const{return width_ * length_;}
  34. double Rectangle::getWidth()const{return width_;}
  35. double Rectangle::getLength()const{return length_;}
  36.  
  37. Rectangle Rectangle::operator + (const Rectangle& rec) {
  38.     double len = rec.getLength() + length_;
  39.     double wid = rec.getWidth() + width_;
  40.     Rectangle newRect(wid, len);
  41.     return newRect;
  42. }
  43. Rectangle Rectangle::operator - (const Rectangle& rec) {
  44.     double len = length_ - rec.getLength();
  45.     double wid = width_ - rec.getWidth();
  46.     Rectangle newRect(wid > 0 ? wid : (double) 0, len > 0 ? len : (double) 0);
  47.     return newRect;
  48. }
  49.  
  50.  
  51. //Circle
  52. //Please implement the member functions of Circle:
  53. //constructor, getArea(), getVolume(), operator+, operator-
  54. //@@Insert your code here
  55. Circle::Circle(double radius) : Shape("Circle") {
  56.     radius_ = radius;
  57. }
  58.  
  59. double Circle::getRadius()const{return radius_;}
  60. double Circle::getVolume()const{return (double) 0;}
  61. double Circle::getArea()const{return radius_ * radius_ * M_PI;}
  62.  
  63. Circle Circle::operator + (const Circle& cir) {
  64.     double rad = cir.getRadius() + radius_;
  65.     Circle newCir(rad);
  66.     return newCir;
  67. }
  68. Circle Circle::operator - (const Circle& cir) {
  69.     double rad = radius_ - cir.getRadius();
  70.     Circle newCir(rad > 0 ? rad : (double) 0);
  71.     return newCir;
  72. }
  73.  
  74. //Sphere
  75. //Please implement the member functions of Sphere:
  76. //constructor, getArea(), getVolume(), operator+, operator-
  77. //@@Insert your code here
  78. Sphere::Sphere(double radius) : Shape("Sphere") {
  79.     radius_ = radius;
  80. }
  81.  
  82.  
  83. double Sphere::getRadius()const{return radius_;}
  84. double Sphere::getArea()const{return 4.0 * radius_ * radius_ * M_PI;}
  85. double Sphere::getVolume()const{return 4.0 * radius_ * radius_ * radius_ * M_PI / 3.0;}
  86.  
  87. Sphere Sphere::operator + (const Sphere& cir) {
  88.     double rad = cir.getRadius() + radius_;
  89.     Sphere newCir(rad);
  90.     return newCir;
  91. }
  92. Sphere Sphere::operator - (const Sphere& cir) {
  93.     double rad = radius_ - cir.getRadius();
  94.     Sphere newCir(rad > 0 ? rad : (double) 0);
  95.     return newCir;
  96. }
  97.  
  98. //Rectprism
  99. //Please implement the member functions of RectPrism:
  100. //constructor, getArea(), getVolume(), operator+, operator-
  101. //@@Insert your code here
  102. RectPrism::RectPrism(double w, double l, double h) : Shape("RectPrism") {
  103.     width_ = w;
  104.     height_ = h;
  105.     length_ = l;
  106. }
  107.  
  108. double RectPrism::getWidth()const{return width_;}
  109. double RectPrism::getHeight()const{return height_;}
  110. double RectPrism::getLength()const{return length_;}
  111. double RectPrism::getArea()const{return 2 * (width_ * length_ + width_ * height_ + length_ * height_);}
  112. double RectPrism::getVolume()const{return length_ * width_ * height_;}
  113.  
  114. RectPrism RectPrism::operator + (const RectPrism& rec) {
  115.     double len = rec.getLength() + length_;
  116.     double wid = rec.getWidth() + width_;
  117.     double hgt = rec.getHeight() + height_;
  118.     RectPrism newRect(wid, len, hgt);
  119.     return newRect;
  120. }
  121. RectPrism RectPrism::operator - (const RectPrism& rec) {
  122.     double len = length_ - rec.getLength();
  123.     double wid = width_ - rec.getWidth();
  124.     double hgt = height_ - rec.getHeight();
  125.     RectPrism newPrism(wid > 0 ? wid : (double) 0, len > 0 ? len : (double) 0, hgt > 0 ? hgt : (double) 0);
  126.     return newPrism;
  127. }
  128.  
  129.  
  130.  
  131. // Read shapes from test.txt and initialize the objects
  132. // Return a vector of pointers that points to the objects
  133. vector<Shape*> CreateShapes(char* file_name){
  134.     std::vector<Shape*> shapes;
  135.     std::ifstream ifs(file_name, std::ifstream::in);
  136.     int num;
  137.     ifs >> num;
  138.     std::string name;
  139.     for(int i = 0; i < num; i++) {
  140.         ifs >> name;
  141.         if(name =="Rectangle") {
  142.             double len, wid;
  143.             ifs >> wid >> len;
  144.             shapes.push_back(new Rectangle(wid, len));
  145.         } else if (name == "Circle") {
  146.             double rad;
  147.             ifs >> rad;
  148.             shapes.push_back(new Circle(rad));
  149.         } else if (name == "Sphere") {
  150.             double rad;
  151.             ifs >> rad;
  152.             shapes.push_back(new Sphere(rad));
  153.         } else if (name == "RectPrism") {
  154.             double len, wid, hgt;
  155.             ifs >> wid >> len >> hgt;
  156.             shapes.push_back(new RectPrism(wid, len, hgt));
  157.         }
  158.     }
  159.     ifs.close();
  160.     return shapes; // please remeber to modify this line to return the correct value
  161. }
  162.  
  163. // call getArea() of each object
  164. // return the max area
  165. double MaxArea(vector<Shape*> shapes){
  166.     double max_area = 0;
  167.     for (int i = 0; i < shapes.size(); i++) {
  168.         double area = shapes[i]->getArea();
  169.         if (area > max_area) max_area = area;
  170.     }
  171.     return max_area;
  172. }
  173.  
  174.  
  175. // call getVolume() of each object
  176. // return the max volume
  177. double MaxVolume(vector<Shape*> shapes){
  178.     double max_volume = 0;
  179.     for (int i = 0; i < shapes.size(); i++) {
  180.         double vol = shapes[i]->getVolume();
  181.         if (vol > max_volume) max_volume = vol;
  182.     }
  183.     return max_volume;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement