Data types In Template in C++


The code defines a function template Large that takes two parameters of the same type T and returns the larger of the two values.

The main() function creates six variables int1, int2, float1, float2, char1, and char2 of types int, int, float, float, char, and char, respectively. It then calls the Large function three times, passing in pairs of these variables, and prints the result on the console.

The first call to Large passes in the integers int1 and int2, and the larger of the two values is returned and printed on the console. The second call to Large passes in the floats float1 and float2, and the larger of the two values is returned and printed on the console. The third call to Large passes in the characters char1 and char2, and the character with the larger ASCII value is returned and printed on the console.

Source Code

#include<iostream>
using namespace std;
template<class T>
T Large(T num1,T num2)
{
  return (num1>num2)?num1:num2;
}
int main()
{
  int int1,int2;
  float float1,float2;
  char char1,char2;
  cout<<"Enter two integers:\n";
  cin>>int1>>int2;
  cout<<Large(int1, int2)<<" is larger."<<endl;
  cout<<"\nEnter two floating-point numbers:\n";
  cin>>float1>>float2;
  cout<<Large(float1, float2)<<" is larger."<<endl;
  cout<<"\nEnter two characters:\n";
  cin>>char1>>char2;
  cout<<Large(char1, char2)<<" has larger ASCII value.";
  return 0;
}
To download raw file Click Here

Output

Enter two integers:
12
23
23 is larger.

Enter two floating-point numbers:
23.2
45.4
45.4 is larger.

Enter two characters:
c
a
c has larger 
ASCII value. 98
98

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples