Question

In: Computer Science

C++ Make this class generic so that the sides of the shape may be integer or...

C++

Make this class generic so that the sides of the shape may be integer or float.

---------------shape.h------------

class Shape
{
public:
Shape();
virtual int getSide();
virtual bool setSide(int x);
virtual int getArea();
virtual int getPerimeter();
void display();
~Shape();


protected:

private:
int side;

// T_sides[numSides];

};

----------------shape.cpp----------

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

using namespace std;

Shape::Shape()
{
}

void Shape::display()
{
cout<<"side: "<<getSide()<<endl;
cout<<" area: "<<getArea()<<endl;
cout<<"perimeter: "<<getPerimeter()<<endl;
}

bool Shape::setSide(int x)
{
if(x>0){
side = x;
return true;
}else
return false;
}


int Shape::getSide()
{
return side;
}

int Shape::getArea()
{
return side*side;

}

int Shape::getPerimeter()
{
return side*4;

}


Shape::~Shape()
{
//dtor
}

int main()
{
int num;
Shape s1;

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

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

s1.display();
return 0;
}

Solutions

Expert Solution

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

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

Related Solutions

Suppose that A is a triangle with integer sides and integer area. Prove that the semiperimeter...
Suppose that A is a triangle with integer sides and integer area. Prove that the semiperimeter of A cannot be a prime number. (Hint: Suppose that a natrual number x is a perfect square, and suppose that p is a prime number that divides x. Explain why it must be the case that p divides x an even number of times)
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square text file: circle   3.5   square   3   rectangle   38   36 circle   23  ...
Make the Measurable interface from Chapter 10 into a generic class. Provide a static method that...
Make the Measurable interface from Chapter 10 into a generic class. Provide a static method that returns the largest element of an ArrayList, provided that the elements are instances of Measurable. Be sure to return a value of type T. Measurable interface: public interface Measurable { double getMeasure(); } Using JAVA please
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Generic types A class or interface that declares one or more generic variables is called a...
Generic types A class or interface that declares one or more generic variables is called a generic type. In this portion of the activity, you will make a class generic. Open GenericsC.java in jGRASP then compile it. At this point you should be familiar with the two type-safety warnings given by the compiler. You should be able to understand the source of the error: the use of the raw types List and Collection. Since the List being declared (al) is...
1. Make the flow diagram and the C ++ program that receives three integer values ​​R,...
1. Make the flow diagram and the C ++ program that receives three integer values ​​R, T and Q, determine if they satisfy the expression below, and if so, show the corresponding values ​​of R, T and Q. R'- T '+ 4 * Q? <820 2. Construct a flow chart and the corresponding program in C which, when receiving Y as data, calculates the result of the following function and prints the values ​​of X and Y.
BackgroundConsider a trianglewith sides of lengths a, b, and c where c is the...
BackgroundConsider a triangle with sides of lengths a, b, and c where c is the longest side. We can describe the side and angle properties of the triangle using these lengths.The side properties include:scalene if all three sides have a different lengthisosceles if two of the sides have the same lengthequilateral if all three sides have the same lengthThe Pythagorean Theorem determines the angle properties which include:acute if all angles are acute (<90 2="" or="">c**2right if one angle is right...
C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators...
C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators + (addition), - (subtraction), * (multiplication), / (division) and pairs of parentheses, properly nested. Use the following two-stack algorithm (by E. W. Dijkstra): If the next token in the expression is an integer, push the integer onto the value stack. If the next token in the expression is an operator, If the operator stack is empty or the priority of the operator is greater...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
A peanut can has the usual “can shape” of a circular cylinder. Its sides are made...
A peanut can has the usual “can shape” of a circular cylinder. Its sides are made of cardboard, its bottom is made of thick plastic-coated aluminum foil, and its top lid is made of clear plastic. It must have a volume of 480 cubic centimeters. The cardboard material used for the sides costs 0.1 cents per square centimeter, the foil material used for the bottom costs 0.3 cents per square centimeter, and the plastic material used for the top lid...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT