Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Statement In this project, you will write a program to represent geometr

ID: 3692827 • Letter: P

Question

Problem Statement In this project, you will write a program to represent geometric shapes and some operations that can be performed on them. For each shape, store fundamental data about its size, and provide member functions to access and modify the data. In addition, provide appropriate functions to compute each shape's area (note that for 3D shape, the area is the surface area) and volume when it applies (for 3D shape only). In your design, consider how shapes are related and thus where inheritance can be implemented. There are various ways to use inheritance to relate these shapes but please follow the inheritance described in the table shown on the next page The abstract base class Shape is declared as follows class Shape public: Shape() th virtual double computeArea()=0; virtual void expand (int factor)-0; virtual void display ) 0; virtual -Shape() 1) //other member functions if you want to add; Your program will consist of the following classes: Shape, Circle, Rectangle, Cuboid, Sphere and Cylinder. Your classes may only have the class variables specified in the table below. Each 3D shape contains a member function computeVolume () to calculate the volume of the shape. You will implement the member function display() for each shape and return the information about the dimension(s), the area and/or volume(for 3D shapes only). Any attempts to set dimensions to be a negative or zero value should display the warning message. Almost all shapes are Expandable, meaning all dimensions can be increased by a (multiplicative)

Explanation / Answer

Circle.cpp


#include "Circle.h"
#include <iostream>
using namespace std;

//Circle constructor with one parameter
Circle::Circle(double r){
    radius = r;
}

//Return the area of a circle
double Circle::computeArea(){

    return PI * (radius * radius);
}

//Expand radius by factor
void Circle::expand(int factor){
    radius = radius*factor;
}

//Print out circle radius and circle area
void Circle::display(){
    cout<<"Circle: (radius = "<<radius<<")"<<endl;
}

Circle.h


#ifndef CIRCLE_H_
#define CIRCLE_H_

#include "Shape.h"

//The Circle is derived from Shape class. It
//Provide a method to compute area of a Circle
//and to expand the dimension with
//given radius
class Circle : public Shape{

   public:
       Circle(double r);
                double computeArea();
     
  
                void expand(int factor);
               void display();
  
   private:
       double radius;
  
        //Used to calculate the area of a circle
               static double const PI = 3.1416;

};
#endif/*CIRCLE_H_*/


Rectangle.cpp

#include "Rectangle.h"
#include <iostream>
using namespace std;

//Create a rectangle with width w and length l
Rectangle::Rectangle(double l, double w){
  
    width = w;
    length = l;
}

//Return the area of a Rectangle
double Rectangle::computeArea(){
    return width * length;
}

//Expand the width and length of a
//rectangle by factor
void Rectangle::expand(int factor){
    width = width * factor;
    length = length * factor;

}

//print out rectangle length and width
//print out the area of a rectangle;
void Rectangle::display(){
    cout<<"Rectangle: (length = "<<length<<", width = "<<width<<")"<<endl;
}


Rectangle.h


#ifndef RECTANGLE_H_
#define RECTANGLE_H_

#include "Shape.h"

//The Rectangle is derived from Shape class. It
//Provide a method to compute area of a Rectangle
//and to expand dimensions with
//given width and length;
class Rectangle : public Shape{

   public:
       Rectangle(double w, double l);
        double computeArea();
        void expand(int factor);
        void display();
  
  
   private:
       double width;
       double length;

};
#endif /*RECTANGLE_H_*/


Shape.h

#ifndef SHAPE_H_
#define SHAPE_H_

//This is the abstract base class.
class Shape{

   public:
       Shape(){};
                virtual double computeArea()=0;
                virtual void expand(int factor)=0;
                virtual void display()=0;
                virtual ~Shape() {};
};

#endif /* SHAPE_H_*/


Square.cpp

#include "Square.h"

#include <iostream>
using namespace std;

//Create a square with side s
Square::Square(double s) : Rectangle(s, s){
    side = s;
}


//Expand the width and length of a
//rectangle by factor
void Square::expand(int factor){
    side = side * factor;

}


//Return the area of a Rectangle
double Square::computeArea(){
    return side * side;
}


//Print the square side and Square area
void Square::display(){
    cout<<"Square: (side = "<<side<<")"<<endl;
  
}


Square.h

#ifndef SQUARE_H_
#define SQUARE_H_

#include "Rectangle.h"


//The Square is derived from Rectangle class. It
//Provide a method to compute area of a Square
//and to expand dimensions with
//given side.
class Square : public Rectangle{

   public:
       Square(double s);
       double computeArea();      
       void expand(int);  
            void display();
  
   private:
       double side;

};

#endif


main.cpp


#include <iostream>
#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"
using namespace std;

int main(int argc, const char * argv[]) {
  
    Circle c1(2.45);
    c1.display();
  
    Square s1(50);
    s1.display();
  
    Rectangle rect(17.53, 6.8);
    rect.display();
  
    Circle c2(9.8);
    c2.display();
  
    Rectangle rect1(88.2, 53);
    rect1.display();
  
    Circle c3(50);
    c3.display();
  
    cout<<"Please input a positive integer as the factor to expand"<<endl;
    int expandFactor=0;
    cin>>expandFactor;
    cout <<"ExpandFactor = "<<expandFactor<<endl;
  
  
    c1.expand(expandFactor);
    c1.display();
  
    s1.expand(expandFactor);
    s1.display();
  
    rect.expand(expandFactor);
    rect.display();
  
    c2.expand(expandFactor);
    c2.display();
  
    rect1.expand(expandFactor);
    rect1.display();
  
    c3.expand(expandFactor);
    c3.display();
  
    return 0;
}

sample output

Circle: (radius = 2.45)                                                                                                                                     
The area is : 18.8575                                                                                                                                       
Square: (side = 50)                                                                                                                                         
Rectangle: (length = 17.53, width = 6.8)                                                                                                                    
Circle: (radius = 9.8)                                                                                                                                      
The area is : 301.719                                                                                                                                       
Rectangle: (length = 88.2, width = 53)                                                                                                                      
Circle: (radius = 50)                                                                                                                                       
The area is : 7854                                                                                                                                          
Please input a positive integer as the factor to expand                                                                                                     
10                                                                                                                                                          
ExpandFactor = 10                                                                                                                                           
Circle: (radius = 24.5)                                                                                                                                     
The area is : 1885.75                                                                                                                                       
Square: (side = 500)                                                                                                                                        
Rectangle: (length = 175.3, width = 68)                                                                                                                     
Circle: (radius = 98)                                                                                                                                       
The area is : 30171.9                                                                                                                                       
Rectangle: (length = 882, width = 530)                                                                                                                      
Circle: (radius = 500)                                                                                                                                      
The area is : 785400                                                                                                                                        

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote