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.
---------------------------shape.h-----------------------------
templateclass Shape { public: Shape(); virtual T getSide(); virtual bool setSide(T x); virtual T getArea(); virtual T getPerimeter(); void display(); ~Shape(); protected: private: T side; // T_sides[numSides]; };
------------------------shape.cpp--------------------------
#include "Shape.h" #includeusing namespace std; template Shape ::Shape() { } template void Shape ::display() { cout << "side: " << getSide() << endl; cout << " area: " << getArea() << endl; cout << "perimeter: " << getPerimeter() << endl; } template bool Shape ::setSide(T x) { if (x > 0) { side = x; return true; } else return false; } template T Shape ::getSide() { return side; } template T Shape ::getArea() { return side * side; } template T Shape ::getPerimeter() { return side * 4; } template Shape ::~Shape() { //dtor } int main() { int num; Shape s1; cout << "Enter side of shape(int): "; cin >> num; if (!s1.setSide(num)) { cout << "Please enter a valid side." << endl; } s1.display(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
