Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "shape.hpp"
- //Base class
- //Please implement Shape's member functions
- //constructor, getName()
- //
- //Base class' constructor should be called in derived classes'
- //constructor to initizlize Shape's private variable
- Shape::Shape(std::string name) {
- name_ = name;
- }
- std::string Shape::getName() {
- return name_;
- }
- //Rectangle
- //Please implement the member functions of Rectangle:
- //constructor, getArea(), getVolume(), operator+, operator-
- //@@Insert your code here
- Rectangle::Rectangle(double width, double length) : Shape("Rectangle") {
- width_ = width;
- length_ = length;
- }
- double Rectangle::getVolume()const{return (double) 0;}
- double Rectangle::getArea()const{return width_ * length_;}
- double Rectangle::getWidth()const{return width_;}
- double Rectangle::getLength()const{return length_;}
- Rectangle Rectangle::operator + (const Rectangle& rec) {
- double len = rec.getLength() + length_;
- double wid = rec.getWidth() + width_;
- Rectangle newRect(wid, len);
- return newRect;
- }
- Rectangle Rectangle::operator - (const Rectangle& rec) {
- double len = length_ - rec.getLength();
- double wid = width_ - rec.getWidth();
- Rectangle newRect(wid > 0 ? wid : (double) 0, len > 0 ? len : (double) 0);
- return newRect;
- }
- //Circle
- //Please implement the member functions of Circle:
- //constructor, getArea(), getVolume(), operator+, operator-
- //@@Insert your code here
- Circle::Circle(double radius) : Shape("Circle") {
- radius_ = radius;
- }
- double Circle::getRadius()const{return radius_;}
- double Circle::getVolume()const{return (double) 0;}
- double Circle::getArea()const{return radius_ * radius_ * M_PI;}
- Circle Circle::operator + (const Circle& cir) {
- double rad = cir.getRadius() + radius_;
- Circle newCir(rad);
- return newCir;
- }
- Circle Circle::operator - (const Circle& cir) {
- double rad = radius_ - cir.getRadius();
- Circle newCir(rad > 0 ? rad : (double) 0);
- return newCir;
- }
- //Sphere
- //Please implement the member functions of Sphere:
- //constructor, getArea(), getVolume(), operator+, operator-
- //@@Insert your code here
- Sphere::Sphere(double radius) : Shape("Sphere") {
- radius_ = radius;
- }
- double Sphere::getRadius()const{return radius_;}
- double Sphere::getArea()const{return 4.0 * radius_ * radius_ * M_PI;}
- double Sphere::getVolume()const{return 4.0 * radius_ * radius_ * radius_ * M_PI / 3.0;}
- Sphere Sphere::operator + (const Sphere& cir) {
- double rad = cir.getRadius() + radius_;
- Sphere newCir(rad);
- return newCir;
- }
- Sphere Sphere::operator - (const Sphere& cir) {
- double rad = radius_ - cir.getRadius();
- Sphere newCir(rad > 0 ? rad : (double) 0);
- return newCir;
- }
- //Rectprism
- //Please implement the member functions of RectPrism:
- //constructor, getArea(), getVolume(), operator+, operator-
- //@@Insert your code here
- RectPrism::RectPrism(double w, double l, double h) : Shape("RectPrism") {
- width_ = w;
- height_ = h;
- length_ = l;
- }
- double RectPrism::getWidth()const{return width_;}
- double RectPrism::getHeight()const{return height_;}
- double RectPrism::getLength()const{return length_;}
- double RectPrism::getArea()const{return 2 * (width_ * length_ + width_ * height_ + length_ * height_);}
- double RectPrism::getVolume()const{return length_ * width_ * height_;}
- RectPrism RectPrism::operator + (const RectPrism& rec) {
- double len = rec.getLength() + length_;
- double wid = rec.getWidth() + width_;
- double hgt = rec.getHeight() + height_;
- RectPrism newRect(wid, len, hgt);
- return newRect;
- }
- RectPrism RectPrism::operator - (const RectPrism& rec) {
- double len = length_ - rec.getLength();
- double wid = width_ - rec.getWidth();
- double hgt = height_ - rec.getHeight();
- RectPrism newPrism(wid > 0 ? wid : (double) 0, len > 0 ? len : (double) 0, hgt > 0 ? hgt : (double) 0);
- return newPrism;
- }
- // Read shapes from test.txt and initialize the objects
- // Return a vector of pointers that points to the objects
- vector<Shape*> CreateShapes(char* file_name){
- std::vector<Shape*> shapes;
- std::ifstream ifs(file_name, std::ifstream::in);
- int num;
- ifs >> num;
- std::string name;
- for(int i = 0; i < num; i++) {
- ifs >> name;
- if(name =="Rectangle") {
- double len, wid;
- ifs >> wid >> len;
- shapes.push_back(new Rectangle(wid, len));
- } else if (name == "Circle") {
- double rad;
- ifs >> rad;
- shapes.push_back(new Circle(rad));
- } else if (name == "Sphere") {
- double rad;
- ifs >> rad;
- shapes.push_back(new Sphere(rad));
- } else if (name == "RectPrism") {
- double len, wid, hgt;
- ifs >> wid >> len >> hgt;
- shapes.push_back(new RectPrism(wid, len, hgt));
- }
- }
- ifs.close();
- return shapes; // please remeber to modify this line to return the correct value
- }
- // call getArea() of each object
- // return the max area
- double MaxArea(vector<Shape*> shapes){
- double max_area = 0;
- for (int i = 0; i < shapes.size(); i++) {
- double area = shapes[i]->getArea();
- if (area > max_area) max_area = area;
- }
- return max_area;
- }
- // call getVolume() of each object
- // return the max volume
- double MaxVolume(vector<Shape*> shapes){
- double max_volume = 0;
- for (int i = 0; i < shapes.size(); i++) {
- double vol = shapes[i]->getVolume();
- if (vol > max_volume) max_volume = vol;
- }
- return max_volume;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement