Question: C++ Use the abstract class shape to define TwoD shape and add all the functionalities to the methods. Define the instances triangle, square and rectangle
C++
Use the abstract class shape to define TwoD shape and add all the functionalities to the methods.
Define the instances triangle, square and rectangle in the driver class to test the functionality of the TwoD class you have extended from the shape class.
CLARIFICATION:
TwoD is to be inherited from Shape class. And TwoD class methods can be changed or we can add new methods to differentiate between rectangle and a triangle based on the number of sides.
#include
using namespace std;
template
// shape is square
class shape
{
T side;
public: int getSide()
{
return side;
}
bool setSides(T s)
{
if(s > 0)
{
side = s;
return true;
}
else
return false;
}
T getArea()
{
return side*side;
}
T getPerimeter()
{
return side*4;
}
void display()
{
cout<<"Side: "<
cout<<"Area: "<
cout<<"Perimeter: "<
}
};
int main()
{
shape S1;
int num1;
cout<<"Enter side(integer): ";
cin>>num1;
if(!S1.setSides(num1))
cout<<"Entered side is not valid ";
S1.display();
shape S2;
float num2;
cout<<"Enter side(float): ";
cin>>num2;
if(!S2.setSides(num2))
cout<<"Entered side is not valid ";
S2.display();
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
