Saturday, 27 December 2014

How to Addition of two Numbers using Function Template (C++)?



The below program is more important in lab as well as interview. This program will explain you how to add two numbers using function in C++. Try to write a program and find out the answer. I have given answer for this program here.

Addition of two Numbers using Function Template


Program:

#include<iostream.h>

#include<conio.h>

template<class T>

T add(T m,T n)

{

return(m+n);

}

void main()

{

int a,b;

float x,y;

cout<<"Enter the two integer values";

cin>>a>>b;

cout<<"Enter two float values";

cin>>x>>y;

cout<<"The Addition of integer numbers:"<<add(a,b);

cout<<"The Addition of float numbers:"<<add(x,y);

getch();

}



Output:

Enter  the two integer values

45   55

Enter  two float values

24.5   25.5

The Addition of integer numbers:100

The Addition of float numbers: 50


All the best!!!