C++ Please make the necessary changes and finish the code.
C++
Please make the necessary changes and finish the code.
Objectives
• To reinforce previously learned fundamentals
• To further apply OOP principles
• To understand inheritance better
Instructions
Major steps outline:
1. Implement a BasicShape class, which will be a pure abstract base class
2. Implement a Circle class derived from the BasicShape class
3. Implement a Recentage class derived from the BasicShape class
4. Dynamically create objects of the derived classes (at least 6 objects), and store them in a
5. Iterate through the vector, and call the whatAmI, as well as the getArea method (described later) to ensure the correct areas are calculate for the given shape
Implement a BasicShape class
Write one file:
• BasicShape.h
The class is a pure abstract base class, which means all of its methods are pure virtual functions. If you don’t know what this means, please refer to the lecture video as well as the Chapter 11 and 15 material.
The pure virtual methods in the BasicShape class are:
• whatAmI
• getArea
The whatAmI function will be implemented by derived classes and return the name of its type, i.e., “Circle” or “Rectangle” as a string.
The getArea function returns a double, which will return the correct area based on the type of shape (e.g., Circle, Rectangle) on which it is called.
Implement Circle and Rectangle classes
Both classes must inherit from the BasicShape class using public inheritance.
Both classes must also implement, as a result, the pure virtual methods of the BasicShape class:
• whatAmI
• getArea
Both classes must implement getters and setters for all private data members.
Both classes must implement Constructors that take the required data (radius for Circle, length and width for Rectangle) as parameters
The Circle class should contain a double radius for its private data, provide the following methods:
• getCircumference – calculate and return the circumference of the circle
• getArea (overridden implementation of the inherited BasicShape class)
• whatAmI (also overridden; returns the string “Circle”)
The Rectangle class should contain two double variables - length and width, as its private data. It should provide implementations of the following methods:
• getPerimeter
• getArea (overridden implementation of the inherited BasicShape class)
• whatAmI (also overridden; returns the string “Rectangle”)
The main File
In the main file, you should dynamically create the instances of the Rectangles and Circles, storing pointers to the objects into the vector<BasicShape*>. When you test it out in main, loop through the vector, and print out the type of class (using the whatAmI) as well as the area, using getArea. You should get the correct values, based on the type of object at each position within the area.
Also, when done, do not forget to delete the objects as well as clear the vector.
#include <string>
#include <math.h>
using namespace std;
class BasicShape{
public:
virtual string whatAml()=0;
virtual double getArea()=0;
};
class Circle: public BasicShape{
private:
double radius;
public:
Circle(double radius){
setRadius(radius);
}
double getRadius(){
return radius;
}
void setRadius(double radius){
this->radius = radius;
}
string whatAml(){
return "Circle";
}
double getCircumference(){
return 2*M_PI*radius;
}
double getArea(){
return M_PI*pow(radius,2);
}
};
class Rectangle: public BasicShape{
private:
double length;
double width;
public:
Rectangle(double length, double width){
setLength(length);
setWidth(width);
}
double getLength(){
return length;
}
void setLength(double length){
this->length = length;
}
double getWidth(){
return width;
}
void setWidth(double width){
this->width = width;
}
string whatAml(){
return "Rectangle";
}
double getPerimeter(){
return 2*(length + width);
}
double getArea(){
return length * width;
}
};
main.cpp
#include <iostream>
#include <vector>
#include "BasicShape.h"
using namespace std;
int main(void){
vector<BasicShape*> shapes;
//randomly create 6 objects
shapes.push_back(new Circle(2.0));
shapes.push_back(new Rectangle(3.5, 4.0));
shapes.push_back(new Rectangle(8.69, 1.53));
shapes.push_back(new Circle(4.69));
shapes.push_back(new Rectangle(3, 5));
shapes.push_back(new Circle(1));
// loop through the vector and find area
for(int i=0; i<shapes.size(); ++i){
cout<<"Shape="<<shapes[i]->whatAml();
cout<<" , ";
cout<<"Area="<<shapes[i]->getArea();
cout<<endl;
}
// deleting the objects
for(int i=0; i<shapes.size(); ++i){
delete shapes[i];
}
// clear the vector
shapes.clear();
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images