0% found this document useful (0 votes)
12 views18 pages

Chapter 8: Templates: Bibha Sthapit Asst. Professor Ioe, Pulchowk Campus

Chapter 8 discusses templates in programming, focusing on their role in generic programming for creating reusable software components. It covers function templates, class templates, and their syntax, along with examples of overloading and derived class templates. The chapter emphasizes the flexibility of templates in handling different data types and their application in both functions and classes.

Uploaded by

ishangautam099
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

Chapter 8: Templates: Bibha Sthapit Asst. Professor Ioe, Pulchowk Campus

Chapter 8 discusses templates in programming, focusing on their role in generic programming for creating reusable software components. It covers function templates, class templates, and their syntax, along with examples of overloading and derived class templates. The chapter emphasizes the flexibility of templates in handling different data types and their application in both functions and classes.

Uploaded by

ishangautam099
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Chapter 8: Templates

BIBHA STHAPIT
ASST. PROFESSOR
IOE, PULCHOWK CAMPUS
Introduction
• Template supports generic programming, which allows
developing reusable software components such as functions,
classes supporting different data types in a single frame work.
– For example , class vector can be used for int, float or double vector
type. Function ‘ addition’ can be used for addition of any type of data
• When object or variable of specific type is defined for actual
use, the template definition for class or function is substituted
with required data type.
• Types of templates :
– Function template
– Class template
Function template
• A function template specifies how an individual function
can be constructed.
• Syntax :
template <class / typename typeT >
ret-type func_ name ( param List of typeT )
{
/ / function body
}
• Where, typeT is a placeholder
template <class T> int a,b;
void swaping (T & x, T & y) cout<<"Enter two integers:"<<endl;
{ T z; cin>>a>>b;
z=x; swaping(a,b);
x=y; cout<<a<<ends<<b<<endl;
y=z; Output:
} float p,q; Enter two characters:
cout<<"Enter two real rt
main( ) numbers:"<<endl; tr
{ cin>>p>>q; Enter two integers:
char ch1,ch2; swaping(p,q); 79
cout<<"Enter two characters:"<<endl; cout<<p<<ends<<q<<endl; 97
cin>>ch1>>ch2; } Enter two real numbers:
swaping(ch1,ch2); 5.6 8.2
cout<<ch1<<ends<<ch2<<endl; 8.2 5.6
Function template with multiple parameters

template <class T1, class T2>


void sum (T1 x,T2 y)
{ Output:
cout<<"Sum="<<x+y<<endl; Sum=5
} Sum=15.8
Sum=7.5
main( )
{
sum(2, 3);
sum(9.8, 6);
sum(3, 4.5);
}
Overloading function template
• Function template can also be overloaded
– 1. Overloading with function
– 2. Overloading with function template
1. Overloading with function
main()
template <class T> {
void display(const T & a)
char c = 'a';
{
display(c);
cout << a << ends;
cout<<endl;
}
display(100,3);
void display(int & a, int n) //overloaded display() cout<<endl;
{ display(10.85);
int ctr; }
for(ctr=0;ctr<n;ctr++)
cout << a << ends;
}
2. Overloading with function template
template <class T> main()
void print( T a) {
{ cout<<a<<endl; print(1);
} print(3.4);
print(455,5);
template <class T> print("hello",3);
void print( T a, int n) }
{
int i; Output:
cout<<endl; 1
for (i=0; i<n; i ++) 3.4
cout<<a<<ends; 455 455 455 455 45
} hello hello hello
Class template
• similar to functions, classes can also be declared to operate on different
data types. Such classes are class templates.
• These classes model a generic class which support similar operations
for different data types.
• Syntax :
template <class T>
class classnm
{ T member1;
T member2;
public:
T fun(); };
• Objects for class template is created like:
classnm <datatype> obj;
obj.memberfun();
Member function as template function
• If the member functions are defined within the template class body,
then they are defined as normal functions
• If the member functions are defined outside the template class body,
they should always be defined with the full template definition.
• Syntax:
template <class typeT >
ret-type classname< typeT> :: func_ name ( param List)
{
/ / function body
}
template <class T> main()
class Add {
{ Add <int> ob1;
T a, b; Add <float> ob2;
public: cout<<"For integer type"<<endl;
void getdata() ob1.getdata( );
{ ob1.display( );
cout<<"Enter 2 nos : "; cout<<"For float type"<<endl;
cin>>a>>b; ob2.getdata( );
} ob2.display( );
void display(); }
}; Output:
For integer type
template <class T> Enter 2 nos : 5 7
void Add <T>::display( ) sum=12
{ For float type
cout<<"sum="<<a+b<<endl; Enter 2 nos : 7.4 2.1
} sum=9.5
Class template with multiple parameter
template <class Type1, class Type2>
main()
class myclass
{
{
myclass<int, double> ob1(10, 0.23);
Type1 i;
myclass<char, char *> ob2('A', "Electrical Engineering");
Type2 j;
ob1.show(); // show int, double
public:
ob2.show(); // show char, char *
myclass(Type1 a, Type2 b)
}
{
i = a; j = b;
Output:
}
10 0.23
void show()
A Electrical Engineering
{
cout << i << ' ' << j << '\n';
}
};
Non- template type argument
main()
Output:
{ Array <float ,5> ob1;
template <class T, int N> For float type
Array <int ,10> ob2;
class Array 2.5
cout<<"For float type"<<endl;
{ For int type
ob1.setdata( 1.5, 1);
T a[N]; 34
ob1.setdata( 2.5, 2);
public: 46
ob1.setdata( 3.5, 3);
void setdata(T value, int i) 346
cout<<ob1.display(2)<<endl;
{ 67
cout<<"For int type"<<endl;
a[ i ]=value; 78
int num;
} 90
for(int i=0; i<10; i++)
T display(int i) 06
{ cin>>num;
{ 32
ob2.setdata(num, i );
return a[i]; 56
}
} 89
for(int i=0; i<10; i++)
};
cout<<ob2.display( i)<<ends; 34 46 346 67 78 90 6 32 56 89

}
Default argument with class template
template <class T=float, int n=5>
class Array main()
{ T a[n]; template <class T, int n>
{
public: void Array<T, n>::display() cout<<"For float type"<<endl;
void setdata() { Array < > ob1;
T sum=0; ob1.setdata( );
{ for (int i=0; i<n; i++)
for (int i=0; i<n; i++) ob1.display();
cin>>a[i];
sum+=a[i];
}
cout<<"For int type"<<endl;
void display(); cout<<"Sum="<<sum<<endl;
Array <int,3> ob2;
}; }
ob2.setdata();
ob2.display();
}
Derived class template
• Three cases of derived class templates:
– 1. Deriving template from a template
– 2. Deriving non-template from template
– 3. Deriving template from non template
1. Deriving template from a template

template <class T,class T1> main()


template <class T>
class derived: public base<T> {
class base
{ T1 b; derived<int,float> d1(20, 30.5);
{
public: d1.display();
T a;
derived(T x, T1 y): base<T>(x), }
public:
base(T x):a(x){} b(y){ }
void display() void display()
{ {
cout<<"a="<<a; base<T>::display();
} cout<<"b="<<b;
}; }
};
2. Deriving non-template from template

template <class T> class derived: public base<int>


main()
class base { {
{ int b; derived d1(20, 30);
T a; public: d1.display();
public: derived(int x, int y): base<int>(x), }
base(T x):a(x){} b(y){ }
void display() void display()
{ {
cout<<"a="<<a; base<int>::display();
} cout<<"b="<<b;
}; }
};
3. Deriving template from non template

class base template <class T> main()


{ class derived: public base {
int a; { T b; derived<int> d1(20, 30);
public: public: d1.display();
base(int x):a(x){} derived(int x, T y): base(x), }
void display() b(y){ }
{ void display()
cout<<"a="<<a; {
} base::display();
}; cout<<"b="<<b;
}
};

You might also like