Tuesday, 18 May 2021

CPP-Slip22a

 Write a C++ program to define two function templates for calculating the square and  cube of  give n numbers with different data types.

 #include<iostream.h>

#include<conio.h>

#include<stdlib.h>

 template <class T>

class Calculator

{

private:

T num1;

 public:

Calculator(T n1)

{

num1 = n1;

}

 void displayResult()

{

cout << "Numbers are: " <<num1<<endl;

cout << "Square is: " <<square()<<endl;

cout << "Cube is: " <<cube()<<endl;

}

 T square() { return num1*num1; }

T cube() { return num1*num1*num1; }

};

 int main()

{

Calculator<int> intCalc(2);

Calculator<float> floatCalc(3.1);

 cout << "Int results:" << endl;

intCalc.displayResult();

 cout << endl << "Float results:" << endl;

floatCalc.displayResult();

return 0;

}