Monday, 19 March 2018

Object Oriented Programming: No. 10


 No: 10


1. Experiment vision: To write a program on the concept of Template function.
2. OBJECTIVE: Program to implement Template function.
3.1 THEORY:
  • Template
The template in C++ allows us to operate on any data type for which the internal implementation is appropriate. Template can be defined for both classes and functions therefore there are two types of template in C++.
1)Function Template
2)Class Template
  • Function Template:- Function Template could be used to create a family of functions with different argument types.
  • Class Template:- Class Template are generally used for data storage classes.
3.2 PROGRAM:
1) Following is an example which makes use of Function template.
#include <iostream>
using namespace std;
template <class T>
void swap(T &x,T &y)
{
T temp=x;
x=y;
y=temp;
}
void fun(int m,int n,float a,float b)
{
cout<<"m and n before swap:"<<m<<" "<<n<<"\n";
swap(m,n);
cout<<"m and n after swap:"<<m<<" "<<n<<"\n";
cout<<"a and b before swap:"<<a<<" "<<b<<"\n";
swap(a,b);
cout<<"a and b after swap:"<<a<<" "<<b<<"\n";
}
int main()
{
fun(100,200,11.22,33.44);
return 0;
}
3.3 OUTPUT:
m and n before swap: 100 200
m and n after swap: 200 100
a and b before swap: 11.22 33.44
a and b after swap: 33.44 11.22
4.1 PROGRAM:
2) Following is an example which makes use of Class template.
#include <iostream>
using namespace std;
template<class t1,class t2>
class test
{
t1 a;
t2 b;
public:
test(t1 x,t2 y)
{
a=x;
b=y;
}
void show()
{
cout<<a<<"and"<<b<<"\n";
}
};
int main()
{
test<float,int>test1(1.23,123);
test<int,char>test2(100,'w');
test1.show();
test2.show();
return 0;
}
4.2 OUTPUT:
5. conclusion:
Thus C++ programs by using template function have been executed successfully.
6. PRECAUTIONS:
1. Avoid wrong keywords.

2. Check whether all brackets are closed properly or not.

3. Take desired output.

4. In case of abnormal results, Contact the teacher/instructor, repeat the
experiment, and check the algorithm and program.
7. REMARK:
A template can be considered as a macro. When an object of a specific type is defined for actual use, the template definition for that class is substituted with the required data type.
8. Discussion Questions:
1) Explain Template in detail.
____________________________________________________________________________________________________________________________________________________________
2) What are the two types of template?
____________________________________________________________________________________________________________________________________________________________
3) What is syntax for writing class template?
____________________________________________________________________________________________________________________________________________________________
4) What is syntax for writing function template?
____________________________________________________________________________________________________________________________________________________________
5) Explain class template with multiple argument.



No comments:

Post a Comment

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.