Loading....
Coupon Accepted Successfully!

 

More Programs

17.19 Write a program to overload[] bracket. Use template variable for displaying float numbers.
#include<iostream.h>
#include<conio.h>
template<class T>
class display
{
public:
void operator[] ( T j)
{
cout<<“ ”<<j;
}
};
int main()
{
clrscr();
display <float> start;
for (float k=0.5;k<10;k++)
start[k];
return 0;
}
OUTPUT
0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5

Explanation:
The first statement defines the template class T. In the class display, the operator[] is overloaded. The variable j is an object of the class T. In main(), the for loop is executed ten times, and the statement start[] is executed 10 times. The output of the program is as given above.
 
17.20 Write a program to declare pointer to template class. Display the data of different data types.
#include<iostream.h>
#include<conio.h>
template<class T>
class data
{
public:
data (T *c) {cout<<“\n”<< “ c=”<<*c;}
};
int main()
{
clrscr();
char x=‘A’;
int y=100;
float z=68.2;
data <char> h(&x) ;
data <int> i(&y);
data <float> j(&z);
return 0;
}
OUTPUT
c = A
c = 100
c = 68.199997


​Explanation:
In the above program, c is a template pointer. In the function main(), x, y, and z are variables of char, int, and float data types that are declared and initialized. The address passed is received by the pointer c, and the values are displayed.

 

17.21 Write a program to access array elements of different types of arrays.
#include<iostream.h>
#include<conio.h>
template<class T>
class data
{
public:
data (T *c);
};
template<class T>
data <T> ::data (T *c)
{
for (int k=0;k<3;k++)
{
cout.precision(2);
cout<<*c <<“,”;
c++;
}
cout<<“\n”;
}
int main()
{
clrscr();
char x[3]=”abc”;
int y[3]={100,200,300};
float z[3]={68.22,55.25,45.85};
data <char> h(x) ;
data <int> i(y);
data <float> j(z);
return 0;
}
OUTPUT
a,b,c,
100,200,300,
68.22,55.25,45.85,


Explanation:
In the above program, variable c is a template pointer. In the function main(), x, y, and z are the arrays of char, int, and float data type. The base address of these arrays is passed to the template. Thus, by an increment operation on the template pointer c,successive elements of the arrays are displayed.
 
17.22 Write a program to define template max() and find the maximum value out of two variables passed to template.
#include<iostream.h>
#include<conio.h>
template<class T>
T max ( T x, T y) {return (x>y) ? x: y;}
void main()
{
clrscr();
int q=78, r=87;
cout<<endl<<“max (int)=”<<max(q,r);
float i=5.2, o=7.4;
cout<<endl<<“max (float)=”<<max(i,o);
char mh=‘B’,ms=‘b’;
cout<<endl<<“max (char)=”<<max(mh,ms);
}
OUTPUT
max (int) =87
max (float) =7.4
max (char) =b

 

Explanation: In the above program, max() is a template function that can be used to determine the maximum value of different data types. In this program, sets of values of int, float, and char are passed to the function and template, and the function template returns the maximum value.

 

17.23 Write a program to exchange values of two variable using templates.
#include<iostream.h>
#include<conio.h>
template<class T>
T exchange (T &x, T &y)
{
T z;
z=x;
x=y;
y=z;
return 0;
}
void main()
{
clrscr();
int j=10, k=20;
exchange (j,k);
cout<<endl<<“j=”<<j<<“\t”<<“k=”<<k;
char c=‘Y’, h=‘B’;
exchange(h,h);
cout<<endl<<“c=”<<c<<“\t”<<“h=”<<h;
}
OUTPUT
j=20 k=10
c=Y h=B

 

Explanation: In the above program, the template function exchange() is defined. Different types of values are passed to the exchange() function. The function exchange() exchanges the values between two variables. The cout() statements display the contents of variables.

 

17.24 Write a program to pass multiple values to the function template.
#include<iostream.h>
#include<conio.h>
template <class A, class B, class C>
void mult(A x, B y, C z)
{cout<<“ j=”<<x <<“ f=”<<y <<“ str=”<<z;}
void main()
{
clrscr();
int j=45;
float f=7.8;
char *str=”string”;
mult(j,f,str);
}
OUTPUT
j= 45 f=7.8 str = string

 

Explanation: In the above program, the template function mult() has three template arguments. In the function main(), variables of int, float, and char are declared and initialized. These variables are passed to the template function mult(). The function mult()displays contents of the variables on the screen.

 

17.25 Write a program to overload a template function and display the float, integer, and string for a given number of times.
#include<iostream.h>
#include<conio.h>
template <class O>
void display (O d) {cout<<d<<“\n”;}
template <class O>
void display (O d, int t)
{
int k=0;
while (k<t)
cout<<d<<“\n”,
k++;
}
void main()
{
clrscr(),
display(2.14),
display(101,3),
display (“Virus !”,4);
}
OUTPUT
2.14
101
101
101
Virus !
Virus !
Virus !
Virus !

 

Explanation: In the above program, the template function display() is overloaded with one and two parameters. When the template function with one argument is called, it displays the given number or character for a single time. When called with two arguments, the first argument is displayed for a number of times that is equal to the second argument. In the output, float value 2.14 and the integer number 101 are displayed thrice. The string “Virus!” is displayed four times.





Test Your Skills Now!
Take a Quiz now
Reviewer Name