Question: Write a new version of the area calculation program from Assessment 3 that makes use of inheritance in C++. Add a new Shape base class

Write a new version of the area calculation program from Assessment 3 that makes use of inheritance in C++.

Add a new Shape base class to the area calculation program that includes data members common to all shapes (such as a shape ID, a shape type, and a unit of measure). The Shape base class should also include a virtual getArea() member function.

Revise the C++ code for Circle and Square classes so that they inherit from the Shape base class.

Add a third shape to the program that also inherits from the Shape class. The finished program should ask the user to enter the relevant information for each shape and then print the area and other information for each shape.

Organize the code correctly into header (.h) and implementation (.cpp) files. Your code should include meaningful comments and be correctly formatted.

Here's the code:

main.cpp

#include //Need this header file to support the C++ I/O system #include "circle.hpp"// including the header and implementation files #include "square.hpp" #include "circle.cpp" #include "square.cpp" using namespace std;//Telling the compiler to use name space where C++ library is declared

int main() { double radius,length;//declare variables for circumference of the circle and length of square Circle circle;//default values used to display radius value circle.Display();//radius value for the circle Circle circ; cout << "Enter the radius value :" << endl;//user to enter value cin >> radius; circ.setRadius(radius); circ.Display();

Square square;//display the square square.Display(); Square squ;//get value for length cout << "Enter the length value :" << endl;//user to enter value cin >> length; squ.setLength(length);//base class squ.Display(); return 0; }

circle.hpp

#ifndef CIRCLE_H #define CIRCLE_H

// circle structure class struct Circle { //declare circle access specifier public: Circle(); Circle(double rad);//parameterized constructor //set radius value void setRadius(double r); //retrieve radius value double getRadius(); //calculate area double Area(); //display data void Display(); //declare value for the radius access specifiers private: double Radius; //access specifier };

#endif

circle.cpp

#include //need this header file to support the C++ I/O system #include "circle.hpp"// include the header file using namespace std;//telling the compiler to use names pace where C++ library is declared

const double PI = 3.14159;//declare, define and initialize global values Circle::Circle()// declare circle method { Radius = 0;//value of the radius initialized }

Circle::Circle(double rad) {//Parameterized constructor Radius=rad; }

void Circle::setRadius(double r) { Radius = r <= 0 ? 1 : r;//verify the input value } double Circle::getRadius() { return Radius;//retrieve and store value } double Circle::Area() { return Radius * Radius * PI;//return value of the area } void Circle::Display() { cout << " Circle Properties";// display the data cout << " Radius = " << getRadius(); cout << " Area = " << Area() << " "; }

square.hpp

#ifndef SQUARE_H #define SQUARE_H

// square structure class struct Square { //declare square access specifier public : Square();

Square(double len);//parameterized constructor //set length void setLength(double l); //retrieve length double getLength(); //retrieve area double Area(); //display data void Display(); // length value declaration access specifier //access specifiers private: double Length; }; #endif

square.cpp

#include //need this header file to support the C++ I/O system #include "square.hpp"// including the header file using namespace std;//telling the compiler to use name space where C++ library is declared

Square::Square()//declare method used for square shape { Length = 0;//initialize length value }

Square::Square(double len) {//Parameterized constructor Length=len; } void Square::setLength(double l) { Length = l <= 0 ? 1 : l;//verify data } double Square::getLength() { return Length;//returning length } double Square::Area() { return Length*Length;// returning the area } void Square::Display(){ cout << " Square Properties";// display output cout << " Length = " << getLength(); cout << " Area = " << Area() << " "; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!