Problem Develop a menu driven program to input the sides and calculate the perim
ID: 3885190 • Letter: P
Question
Problem
Develop a menu driven program to input the sides and calculate the perimeter and area of a triangle, square, rectangle, pentagon, and polygon. The application should close on selecting the Exit option.
Q1.
Define a C++ abstract class named shape. The class will have attributes for the sides of the shape. The class will also need the accessor method getSide that returns the sides of the shape. The class also needs one mutator method called setSides which will set the sides of the shape. The setSides method should return a Boolean value indicating if the set was successful or not and update the sides accordingly. (You cannot have a 0 or negative side.)
You will need to do the class declaration in a header(.h) file and the class implementation in the .cpp file.
Also add two more methods in the class namely getArea and getPerimeter to calculate the area and perimeter of the shape. Define a method Display() for this class to print the sides of the shape on the screen.
Q2.
Make this class generic so that the sides of the shape may be integer or float.
Q3.
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.
Q4.
Define a new class ThreeD extended from ThreeD class. The constructor of ThreeD class should call constructor of TwoD class, and also the print method should call the Display() of the super class. Override the functions getArea and getPerimeter to work with the new formula. The ThreeD class will represent instances of ThreeD class as a cube and a box.
Extend the driver program to display a menu to take user input for the figure user is selecting and display all details about that figure.
Explanation / Answer
// ====== Shape.h ======
#pragma once
#include <iostream>
#include <string>
template <typename T>
class Shape
{
private:
// Maximum number of sides.
static const int _MAX_SIDES{10};
// Number of sides.
int _num_sides;
// Sides magnitudes.
T _sides[_MAX_SIDES];
public:
// Constructor.
Shape();
// gets a side magnitude.
virtual T get_side(int index) = 0;
// set sides.
virtual bool set_sides(int sides) = 0;
// get number of sides.
int get_num_sides;
// area
virtual double get_area() = 0;
// perimeter
virtual double get_perimeter() = 0;
// Display all sides.
void display();
// Destructor.
~Shape();
};
// ====== Shape.cpp ======
#include "stdafx.h"
#include "Shape.h"
template <typename T>
Shape<T>::Shape()
{
}
/**
* Displays all sides as a string on console.
*/
template <typename T>
void Shape<T>::display()
{
std::string out = "Sides: ";
for (int i = 0; i < this->_num_sides-1; ++i)
{
out += (std::to_string(_sides[i])+", ");
}
out += _sides[_num_sides - 1];
std::cout << out << std::endl;
}
/**
* Get number of sides.
*/
template <typename T>
void Shape<T>::get_num_sides()
{
return this->_num_sides;
}
template <typename T>
Shape<T>::~Shape()
{
}
// ====== TwoD.h ======
#pragma once
#include "Shape.h"
template <typename T>
class TwoD :
public Shape<T>
{
public:
TwoD();
~TwoD();
T get_side(int index);
bool set_sides(int sides);
double get_area();
double get_perimeter();
};
// ====== TwoD.cpp ======
#include "stdafx.h"
#include "TwoD.h"
template<typename T>
TwoD<T>::TwoD()
{
}
template<typename T>
TwoD<T>::~TwoD()
{
}
template<typename T>
T TwoD<T>::get_side(int index)
{
return this->_sides[index];
}
template<typename T>
bool TwoD<T>::set_sides(int sides)
{
this._num_sides = sides;
for (int i = 0; i < sides; ++i)
{
int side_mag{0};
std::cout << "Enter side " << i + 1 << std::end;
std::cin >> side_mag;
if (side_mag == 0)
{
return false;
}
else {
this._sides[i] = side_mag;
}
}
// if all goes well.
return true;
}
template<typename T>
double TwoD<T>::get_area()
{
double area{};
switch (this->get_num_sides()) {
// triangle, all we need is base and height.
case 3:
area = this->_sides[0] * _sides[1] / 2;
// square or rectangle.
case 4:
area = this->_sides[0] * _sides[1];
}
}
template<typename T>
double TwoD<T>::get_perimeter()
{
double perimeter{};
switch (this->get_num_sides()) {
// triangle, all we need is base and height.
case 3:
perimeter = this->_sides[0] * _sides[1] / 2;
// square or rectangle.
case 4:
perimeter = this->_sides[0] * _sides[1];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.