Question #218025

Define and implement the class Square with template to enable the user using different data types for the square side. The class has side as member variable and and has setSide(Sside), getSide(), Area(), Perimeter(), Print() as member functions beside the constructers Square(), Square(Cside ), the destructor ~Square(). Determine which function is accessor and which one is mutator. Hint: Perimeter=4*side. *



Expert's answer

#include<iostream>
 using namespace std;
template <class T>
 class Square {
 private:
  T side;
 public:
  void setSide(T Sside){
   side=Sside;
 }
 T getSide(){
 return side;
 }
 T Area(){
 return (side*side);
 }
 T Perimeter (){
 return (side*4);
 }
 void print(){
 cout<<"\nArea:"<<Area();
 cout<<"Perimeter:"<<Perimeter();
 }
 Square (){
 }
 Square (T Cside){
 side=Cside;
 }
 ~Square(){
 }
 };
 int main()
 {
   Square <int> s;
   s.setSide(10);
   s.print();
   return 0;
 }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS