Help C programming How to convert into template classes Test
Help!!! C++ programming How to convert into template classes
Test each with Implicit int, float, double, long int.
Test each with explicit int, float, double, long int.
Header.h
#ifndef Header_h
#define Header_h
class Rectangle // Defination of Class Rectangle
{
public: //public variables and functions
void set_values(int x, int y); // public function with variables x and y, they are ints
int area(); //public function area
~Rectangle()
{
cout << \"This is a deconstructor.\" << endl; //This defines the deconstructor of the class.
}
private:
int width, height;
};
#endif /* Header_h */
Source.cpp
#include <iostream>
#include <string>
using namespace std;
#include \"Header.h\"
void Rectangle::set_values(int x, int y)// Implementation of function Rectangle with input variable x and y
{
width = x; // parameter transmission, transmite formal parameter to pratical parameter width
height = y; // parameter transmission, transmite formal parameter to pratical parameter height
}
int Rectangle::area() // Implementation of function Rectangle
{
int answer; // Set parameter
answer = width * height;// Calculate the area of the rectangle
return answer; // return the result of calculation
}
Main.cpp
#include <iostream>
#include <string>
using namespace std;
#include \"Header.h\"
int main() // Testing program
{
Rectangle rect1; // DECLARATION - USE the new datatype / class \'Rectangle\' in a Declaration statement to create \'rect1\'
rect1.set_values(5, 6); // Use set_values function to set values
cout << \"area: \" << rect1.area() << endl; // Output the result
Rectangle rect2; // DECLARATION
rect2.set_values(3, 4); // Use set_values function to set values
cout << \"area: \" << rect2.area() << endl; // Output the result
// system(\"pause\"); // Mac not support
return 0;
}
Solution
#ifndef Header_h
 #define Header_h
 #include<iostream>
 using namespace std;
 //template class of type ,T
 template<class T>
 class Rectangle // Defination of Class Rectangle
 {
 public: //public variables and functions
     void set_values(T x, T y); // public function with variables x and y, they are ints
     T area(); //public function area
    ~Rectangle(){}
 private:
     T width;
    T height;
 };
//template defintion of class method set_values
 template <class T>
 void Rectangle<T>::set_values(T x, T y)
 {
     width = x; // parameter transmission, transmite formal parameter to pratical parameter width
     height = y; // parameter transmission, transmite formal parameter to pratical parameter height
 }
//template defintion of class method area
 // Implementation of function Rectangle
 template <class T>
 T Rectangle<T>::area()
 {
    T answer; // Set parameter
     answer = width * height;// Calculate the area of the rectangle
     return answer; // return the result of calculation
 }
#endif Header_h/* Header_h */
------------------------------------------------------------------------------------------------------------
//Main.cpp
#include <iostream>
 #include <string>
 //include Rectangle.h
 #include \"Rectangle.h\"
 using namespace std;
 int main() // Testing program
 {
   //Creat an objct of type Rectangle of input type integer
    Rectangle<int> rect1;
       
     rect1.set_values(5, 6); // Use set_values function to set values
     cout << \"area: \" << rect1.area() << endl; // Output the result
   //Creat an objct of type Rectangle of input type float
     Rectangle<float> rect2; // DECLARATION
     rect2.set_values(3.0, 4.0); // Use set_values function to set values
     cout << \"area: \" << rect2.area() << endl; // Output the result
system(\"pause\"); // Mac not support
return 0;
}
------------------------------------------------------------------------------------------------------------
Sample output:
area: 30
 area: 12



