Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

6. In the following class Shape, the data members are integers and some function

ID: 644019 • Letter: 6

Question

6.   In the following class Shape, the data members are integers and some function members return an integer value.

rewrite the template class below:

class Shape

{     private:

            int numOfSides;

            int lenOfShortestSide;

            int circumference;

      public;

      Shape::Shape()

            void setData(int num, int length)

            {     numOfSides = num;

                  lenOfShortestSide = length;

            }

            int getCircumference()

            {     int circumf;

                  cin >> circumf;

                  return circumf;

            }

};

a. Rewrite the class as a template that accepts any numeric type for these members:

b. Write two statements to instantiate two objects of different data types in the implementation file:

c. Write two function calls to the template class, from the implementation file, one to setData(), theother to getCircumference() for the two different objects instantiated above:

Explanation / Answer

// a. Rewrite the class as a template that accepts any numeric type for these members:
template<typename dataType>
class Shape
{     private:
            dataType numOfSides;
            dataType lenOfShortestSide;
            dataType circumference;
      public:
      Shape(){}
            void setData(dataType num, dataType length)
            {     numOfSides = num;
                  lenOfShortestSide = length;
            }
            dataType getCircumference()
            {     dataType circumf;
                  cin >> circumf;
                  return circumf;
            }
};
//b. Write two statements to instantiate two objects of different data types in the implementation file:
Shape<int> S_int;
Shape<double> S_double;
// c. Write two function calls to the template class, from the implementation file, one to setData(),
// theother to getCircumference() for the two different objects instantiated above

S_int.setData(4,5);
S_int.getCircumference();
S_double.setData(2,5);
S_double.getCircumference();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote