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

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-----------------------------

template <class T>
class 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"
#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() {
//dtor
}

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;
    }

    s1.display();
    return 0;
}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution