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  ...
c++ You will implement a template class with the following members: An array of our generic...
c++ You will implement a template class with the following members: An array of our generic type. The size of this array should be determined by an integer argument to the constructor. An integer for storing the array size. A constructor capable of accepting one integer argument. The constructor should initialize the array and set the array size. A method, find Max, that returns the maximum value in the array. A method, find Min, that returns the minimum value in...
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
In C++ the largest int value is 2147483647. So, an integer larger than this cannot be...
In C++ the largest int value is 2147483647. So, an integer larger than this cannot be stored and processed as an integer. Similarly if the sum or product of two positive integers is greater than 2147483647, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an array. In this assignment, you will design a class named LargeInteger such that an object of this class can store...
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...
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