C++ Please make the necessary changes and finish the code.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 vector<BasicShape*>
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;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Data members
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education