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

use c++ A right-angle triangle with integer sides a, b, c, such as 3, 4, 5,...
use c++ A right-angle triangle with integer sides a, b, c, such as 3, 4, 5, is called a Pythagorean triple. The condition is that hypotenuse to power of 2 is equal to adding of square of the 2 other sides. In this example 25 = 16 + 9. Use brute force approach to find all the triples such that no side is bigger than 50. For this you should first implement the following Triangle class and its methods. Then...
/* A right-angle triangle with integer sides a, b, c, such as 3, 4, 5, is...
/* A right-angle triangle with integer sides a, b, c, such as 3, 4, 5, is called a Pythagorean triple. The condition is that hypotenuse to power of 2 is equal to adding of square of the 2 other sides. In this example 25 = 16 + 9. Use brute force approach to find all the triples such that no side is bigger than 50. For this you should first implement the following Triangle class and its methods. Then use...
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
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...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
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...
C# Language Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and...
C# Language Create a class called evaluateValue that declares 3 integer class variables: zeroValue, positiveValue and negativeValue. These should be declared as public and you should not use automatic properties to declare them. Your class should have a constructor that takes one integer argument. In the constructor you will code if statements to set one of the three class variables (indicators) to 1 if the number sent to the constructor is either equal to zero, negative, or positive. In the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT