Answer to Question #218025 in C++ for Shema

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. *



1
Expert's answer
2021-07-17T01:31:02-0400
#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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS