Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Write a new version of the area calculation program that makes use of.docx
1. Write a new version of the area calculation program that makes
use of inheritance in C++. Add a new Shape base class to the
area calculation program of the circle and square that includes
data members common to all shapes (such as a shape ID: a
shape type: and a unit of measure). 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 get Area() member function. Revise the 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. Be sure to organize the
code correctly into header (.h) and implementation (cpp) files.
Your code should include meaningful comments and be
correctly formatted.
Solution
Shapes.h
#include <iostream>
#include<string.h>
using namespace std;
class Shape
{
private:
2. int Shape_ID;
string Shape_type;
string unit;
public:
virtual void SetUnit(int)=0; //virtual functions
virtual int getArea()=0;
};
class Circle:public Shape
{
private:
int radius;
public:
void SetUnit(int);
int getArea();
};
class Square:public Shape
{
private:
int side;
public:
void SetUnit(int);
int getArea();
};
Shapes.cpp
#include"Shapes.h"
4. int main() {
Shape* c=new Circle; base class pointer is used to create
derived class objects
Shape* s=new Square;
c->SetUnit(4);
cout<<"Area of circle"<<c->getArea()<<endl;
s->SetUnit(12);
cout<<"Area of Square"<<s->getArea()<<endl;
return 0;
}