1. Engineering
  2. Computer Science
  3. c use the abstract class shape to define twod shape...

Question: c use the abstract class shape to define twod shape...

Question details


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.


template <class T>
class Shape {
    virtual T getSide();
    virtual bool setSide(T x);
    virtual T getArea();
    virtual T getPerimeter();
    void display();

    T side;
// T_sides[numSides];


#include "Shape.h"
#include <iostream>

using namespace std;

template <class T>
Shape<T>::Shape() {

template <class T>
void Shape<T>::display() {
    cout << "side: " << getSide() << endl;
    cout << " area: " << getArea() << endl;
    cout << "perimeter: " << getPerimeter() << endl;

template <class T>
bool Shape<T>::setSide(T x) {
    if (x > 0) {
        side = x;
        return true;
    } else
        return false;

template <class T>
T Shape<T>::getSide() {
    return side;

template <class T>
T Shape<T>::getArea() {
    return side * side;


template <class T>
T Shape<T>::getPerimeter() {
    return side * 4;


template <class T>
Shape<T>::~Shape() {

int main() {
    int num;
    Shape<int> s1;

    cout << "Enter side of shape(int): ";
    cin >> num;

    if (!s1.setSide(num)) {
        cout << "Please enter a valid side." << endl;

    return 0;
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution