Problem Implement a hierarchy of Shape s to demonstrate the OOP concepts of inhe
ID: 3769117 • Letter: P
Question
Problem
Implement a hierarchy of Shapes to demonstrate the OOP concepts of inheritance and polymorphism.
Specification
Create class Shape that will serve as the base class for several types of Shapes. Declare data members in the Shape class for a description ("circle", "triangle", etc.) and an area, and include appropriate accessor (get) and mutator (set) functions. Also declare a virtual calcArea() function.
Derive classes Rectangle, Circle, and Triangle from class Shape. For each of these classes, include appropriate data members and implement thecalcArea()function.
Create an array of at least 5 Shape pointers and fill the array with the addresses of several different kinds of shapes.
Use a for loop to calculate and print the description and area of each Shape in the array.
Include a multi-line comment in your program that describes the difference between static (early) binding and dynamic (late) binding and how dynamic binding facilitates polymorphic precessing.
PLEASE USE C++;
PolyShape.h
---------------------------------
#ifndef POLYSHAPE_H
#define POLYSHAPE_H
class PolyShape
{
public:
PolyShape();
double Gettriangle();
void Settriangle(double val);
double Getcircle();
void Setcircle(double val);
double Getrectangle();
void Setrectangle(double val);
protected:
private:
double triangle;
double circle;
double rectangle;
};
#endif // POLYSHAPE_H
PolyShape.cpp
-------------------------------------
#include "polyshape.h"
PolyShape::PolyShape()
{
//ctor
}
double PolyShape::Gettriangle()
{
return triangle;
}
void PolyShape::Settriangle(double val)
{
triangle = val;
}
double PolyShape::Getcircle()
{
return circle;
}
void PolyShape::Setcircle(double val)
{
circle = val;
}
double PolyShape::Getrectangle()
{
return rectangle;
}
void PolyShape::Setrectangle(double val)
{
rectangle = val;
}
int main
----------------------
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Explanation / Answer
#include <iostream.h>
#include <conio.h>
class shape
{
protected:
double x,y;
public:
virtualvoid get_data()=0;
virtualvoid display_area()=0;
};
class triangle : public shape
{
public:
void get_data(void)
{
cout<<" Enter data for triangle ";
cout<<"Enter base and height respectively : ";
cin>>x>>y;
}
void display_area(void)
{
cout<<" The area of triangle ";
double tringlearea;
tringlearea = 0.5 * x * y;
cout<<"Area of Triangle is "<<tringlearea;
}
};
class rectangle : public shape
{
public:
void get_data(void)
{
cout<<" Enter data for rectangle ";
cout<<"Enter length of two sides : ";
cin>>x>>y;
}
void display_area(void)
{
cout<<" The area for rectangle ";
double rectanglearea;
rectanglearea= x * y;
cout<<"Area of Rectangle is "<<rectanglearea;
}
};
void main()
{
clrscr();
triangle tri;
rectangle rect;
shape *list[2];
list[0]=&tri;
list[1]=▭
int option;
while(1)
{
clrscr();
cout<<" Different Shapes ";
cout<<" Select your option ";
cout<<"1) Area of Triangle ";
cout<<"2) Area of Rectangle ";
cout<<"3) Exit ";
cout<<"Enter your option:-";
cin>>option;
switch(option)
{
case 1 : list[0]->get_data();
list[0]->display_area();
getch();
break;
case 2 : list[1]->get_data();
list[1]->display_area();
getch();
break;
case 3 : goto end;
default: cout<<" Invalid option Try again ";
getch();
}
}
end:
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.