Q:

C++ Program to build Simple calculator using Class template

belongs to collection: C++ Template Solved Programs

0

Write a C++ Program to build Simple calculator using Class template. Here’s a Simple C++ Program to build Simple calculator using Class template in C++ Programming Language.


What are Templates in C++ ?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

 
 

A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>..


Function Template :

The general form of a template function definition is shown here:

 

template <class type> ret-type func-name(parameter list)

{
// body of function
}


Class Template :

Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here:

template <class type> class class-name

{
.
.
.
}


Below is the source code for C++ Program to build Simple calculator using Class template which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/* C++ Program to build Simple calculator using Class template  */

#include <iostream>
using namespace std;

template <class T>
class Calculator
{
private:
        T num1, num2;
        
public:
        Calculator(T n1, T n2)
        {
                num1 = n1;
                num2 = n2;
        }
        
        void displayResult()
        {
                cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
                cout << "Addition is: " << add() << endl;
                cout << "Subtraction is: " << subtract() << endl;
                cout << "Product is: " << multiply() << endl;
                cout << "Division is: " << divide() << endl;
        }
        
        T add() { return num1 + num2; }
        
        T subtract() { return num1 - num2; }
        
        T multiply() { return num1 * num2; }
        
        T divide() { return num1 / num2; }
};

int main()
{
        Calculator<int> intCalc(2, 1);
        Calculator<float> floatCalc(2.4, 1.2);
        
        cout << "Int results:" << endl;
        intCalc.displayResult();
        
        cout << endl << "Float results:" << endl;
        floatCalc.displayResult();
        
        return 0;
}

OUTPUT : :


/* C++ Program to build Simple calculator using Class template  */

Int results:
Numbers are: 2 and 1.
Addition is: 3
Subtraction is: 1
Product is: 2
Division is: 2

Float results:
Numbers are: 2.4 and 1.2.
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2
 
Exit code: 0 (normal program termination)

Above is the source code and output for C++ Program to build Simple calculator using Class template which is successfully compiled and run on Windows System to produce desired output.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to demonstrate an Example of Template ... >>
<< C++ Program to Swap data using function template...