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
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
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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started