HackerzZ
Presentations
&
Productions
What we Learn in this Presentation
• Templates
• Function Templates
• Class Templates
Reference :- Object-Oriented Programming using C++ by IT Series
Book Page No :- 490 - 495
CHAPTER NO.17 TEMPLATES
What is template ?
It is a keyword which allows functions or classes to operate
without actually knowing what datatype will be handled by their
operations.
This is what is known as Generic
Programming
>> Advantages of Templates <<
 Allows to generate classes or functions
to handle different data types.
 Reduce the repetition of code.
 Make the program development easier
and more manageable.
Types of Templates
 Function Templates
 Class Templates
FunctionTemplates
FunctionTemplates are used to
define functions which are not related
to specific data types and can work
with different data types.
Syntax of Template Function
template <class T>
return_type function_name (T parameter(s))
{
Function Body
}
Class : Used to define the name of general data type.
T : Any letter/word can be used as a name of general data type.
Return_Type : Used to return value.
Parameter(s) : Parameters passed to a function. The type of parameters is a
general data type T that is defined in a template.
Program 1
Write a template to display the Maximum Number
#include<iostream>
#include<conio.h>
using namespace std;
template <class M>
void Max(M a, M b)
{ if(a>b) cout<<a;
else cout<<b; }
int main()
{ Max(8,20);
cout<<“n”;
Max(4.5,1.7);
getch(); }
Program 2
Write a template to display the Minimum Number between INT and FLOAT
#include<iostream>
#include<conio.h>
using namespace std;
template <class X, class Y>
void Min(X a, Y b)
{ if(a<b) cout<<a;
else cout<<b; }
int main()
{ Min(50,20.77);
cout<<“n”;
Min(4.5,10);
getch(); }
ClassTemplates
ClassTemplates are used to
define classes which are not related to
specific data types and can work with
different data types.
It consists of statements which are
independent of particular data types.
Syntax of Template Class
template <class T>
class class_name
{
Class Body
};
Class :- Used to define the name of general data type.
T :- Any letter/word can be used as a name of general data type.
Class_Name :- It is the name of a class.
Program 3
A program to input five integers in an array and display them using pointer.
#include<iostream>
#include<conio.h>
using namespace std;
template <class A>
class Add
{
private: A x,y;
public:
void get()
{ cout<<"Enter 1st value => "; cin>>x;
cout<<"Enter 2st value => "; cin>>y; }
void add()
{ x=x+y; }
void show()
{ cout<<"Values after addition = "<<x; }
};
Program 3 (contd..)
A program to input five integers in an array and display them using pointer.
int main()
{
Add <int> ob;
ob.get();
ob.add();
ob.show();
getch();
}
<int> = <class A>
Thank You
ANY QUESTIONS ??
DANGER
Created By Muhammad Ammad Hassan
Muhammad Azhar Khalil
Presented By Muhammad Azhar Khalil
Muhammad Ammad Hassan
Participants Toheed Azmat
Nouman Cheema
Hasseb Razzaq
Muhammad Usman Ahmad
Muhammad Ismail

Template C++ OOP

  • 2.
  • 3.
    What we Learnin this Presentation • Templates • Function Templates • Class Templates Reference :- Object-Oriented Programming using C++ by IT Series Book Page No :- 490 - 495
  • 4.
    CHAPTER NO.17 TEMPLATES Whatis template ? It is a keyword which allows functions or classes to operate without actually knowing what datatype will be handled by their operations. This is what is known as Generic Programming
  • 5.
    >> Advantages ofTemplates <<  Allows to generate classes or functions to handle different data types.  Reduce the repetition of code.  Make the program development easier and more manageable.
  • 6.
    Types of Templates Function Templates  Class Templates
  • 7.
    FunctionTemplates FunctionTemplates are usedto define functions which are not related to specific data types and can work with different data types.
  • 8.
    Syntax of TemplateFunction template <class T> return_type function_name (T parameter(s)) { Function Body } Class : Used to define the name of general data type. T : Any letter/word can be used as a name of general data type. Return_Type : Used to return value. Parameter(s) : Parameters passed to a function. The type of parameters is a general data type T that is defined in a template.
  • 9.
    Program 1 Write atemplate to display the Maximum Number #include<iostream> #include<conio.h> using namespace std; template <class M> void Max(M a, M b) { if(a>b) cout<<a; else cout<<b; } int main() { Max(8,20); cout<<“n”; Max(4.5,1.7); getch(); }
  • 10.
    Program 2 Write atemplate to display the Minimum Number between INT and FLOAT #include<iostream> #include<conio.h> using namespace std; template <class X, class Y> void Min(X a, Y b) { if(a<b) cout<<a; else cout<<b; } int main() { Min(50,20.77); cout<<“n”; Min(4.5,10); getch(); }
  • 11.
    ClassTemplates ClassTemplates are usedto define classes which are not related to specific data types and can work with different data types. It consists of statements which are independent of particular data types.
  • 12.
    Syntax of TemplateClass template <class T> class class_name { Class Body }; Class :- Used to define the name of general data type. T :- Any letter/word can be used as a name of general data type. Class_Name :- It is the name of a class.
  • 13.
    Program 3 A programto input five integers in an array and display them using pointer. #include<iostream> #include<conio.h> using namespace std; template <class A> class Add { private: A x,y; public: void get() { cout<<"Enter 1st value => "; cin>>x; cout<<"Enter 2st value => "; cin>>y; } void add() { x=x+y; } void show() { cout<<"Values after addition = "<<x; } };
  • 14.
    Program 3 (contd..) Aprogram to input five integers in an array and display them using pointer. int main() { Add <int> ob; ob.get(); ob.add(); ob.show(); getch(); } <int> = <class A>
  • 15.
  • 16.
  • 18.
    Created By MuhammadAmmad Hassan Muhammad Azhar Khalil Presented By Muhammad Azhar Khalil Muhammad Ammad Hassan Participants Toheed Azmat Nouman Cheema Hasseb Razzaq Muhammad Usman Ahmad Muhammad Ismail