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. *
#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;
}
Comments
Leave a comment