0% found this document useful (0 votes)
368 views

Lab Answers

The document discusses templates in C++ programming. It provides two examples - one that uses a function template to find the larger of two numbers of different data types, and another that uses a function template to swap values of different data types. Templates allow functions and classes to work with multiple data types without needing separate code for each one. The document defines a function template to find the larger of two values, and it works for int, float, and char data when called. Similarly, a template function is defined to swap two values, and it can swap integers, floats, and characters. Templates generate type-specific versions at compile-time for better code reuse and flexibility.

Uploaded by

svp62
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
368 views

Lab Answers

The document discusses templates in C++ programming. It provides two examples - one that uses a function template to find the larger of two numbers of different data types, and another that uses a function template to swap values of different data types. Templates allow functions and classes to work with multiple data types without needing separate code for each one. The document defines a function template to find the larger of two values, and it works for int, float, and char data when called. Similarly, a template function is defined to swap two values, and it can swap integers, floats, and characters. Templates generate type-specific versions at compile-time for better code reuse and flexibility.

Uploaded by

svp62
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

program to generate Fibonacci series using constructor and destructor

#include<iostream.h>
#include<conio.h>
class fib
{
private:
int a,b,c,ca,n;
public:
fib()
{
cout<<"the fab series is ";
}
fib(int a1,int b1 )
{
a=a1;
b=b1;
}
~ fib()
{
Cout<<destructor called;
}
void increment()
{
ca=2;a=0;b=1;
cout<<"enter the number of values ";
cin>>n;
cout<<a<<" "<<b;
while(ca<n)
{
c=a+b;
cout<<" "<<c;
a=b;
b=c;
ca++;
}
}
};
void main()
{ clrscr();
class fib k;
k.increment();
class fib z(0,1);
getch();
clrscr();
z.increment();
getch();
}
TEMPLATES:
Templates in C++ programming allows function or class to work on more than one data type at
once without writing different codes for different data types. Templates are often used in larger
programs for the purpose of code reusability and flexibility of program. The concept of templetes
can be used in two different ways:
Function Templates
Class Templates
Function Templates
A function templates work in similar manner as function but with one key difference. A single
function template can work on different types at once but, different functions are needed to
perform identical task on different data types. If you need to perform identical operations on two
or more types of data then, you can use function overloading. But better approach would be to
use function templates because you can perform this task by writing less code and code is easier
to maintain.
How to define function template?
A function template starts with keyword template followed by template parameter/s inside < >
which is followed by function declaration.
template <class T>
T some_function(T arg)
{
.... ... ....
}
In above code, T is a template argument and class is a keyword. You can use keyword typename
instead of class in above example. When, an argument is passed to some_function( ), compiler
generates new version of some_function() to work on argument of that type.
Example of Function Template

/* C++ program to display larger number among two numbers using function
templates. */
/* If two characters are passed to function template, character with larger
ASCII value is displayed. */

#include <iostream>
using namespace std;
template <class T>
T Large(T n1, T n2)
{
return (n1>n2) ? n1:n2;
}
int main()
{
int i1, i2;
float f1, f2;
char c1, c2;
cout<<"Enter two integers: ";
cin>>i1>>i2;
cout<<Large(i1, i2)<<" is larger.";
cout<<"\n\nEnter two floating-point numbers: ";
cin>>f1>>f2;
cout<<Large(f1, f2)<<" is larger.";
cout<<"\n\nEnter two characters: ";
cin>>c1>>c2;
cout<<Large(c1, c2)<<" has larger ASCII value.";
return 0;
}
Explanation
In this program, data of three different types: int, float and char is passed to function template
and this template returns the larger of two data passed. In function template data type is
represented by name: T in above example. During run-time, when integer data is passed to
template function then, compiler knows the type to use is int. Similarly, when floating-point data
and char data is passed, it knows the type to use is float and char respectively. After knowing the
information of a type, it generates the specific version of Large( ) to work for that type.
Example to Swap Datas Using Concept of Templates

/* C++ program to swap datas entered by user. */

#include <iostream>
using namespace std;
template <typename T>
void Swap(T &n1, T &n2)
{
T temp;
temp = n1;
n1 = n2;
n2 = temp;
}

int main()
{
int i1=1, i2=2;
float f1=1.1, f2=2.2;
char c1='a', c2='b';
cout<<"Before passing data to function template.\n";
cout<<"i1 = "<<i1<<"\ni2="<<i2;
cout<<"\nf1 = "<<f1<<"\nf2="<<f2;
cout<<"\nc1 = "<<c1<<"\nc2="<<c2;

Swap(i1, i2);
Swap(f1, f2);
Swap(c1, c2);

cout<<"\n\nAfter passing data to function template.\n";
cout<<"i1 = "<<i1<<"\ni2="<<i2;
cout<<"\nf1 = "<<f1<<"\nf2="<<f2;
cout<<"\nc1 = "<<c1<<"\nc2="<<c2;
return 0;
}
Output
Before passing data to function template.
i1 = 1
i2 = 2
f1 = 1.1
f2 = 2.2
c1 = a
c2 = b

After passing data to function template.
i1 = 2
i2 = 1
f1 = 2.2
f2 = 1.1
c1 = b
c2 = a

You might also like