SlideShare a Scribd company logo
1 of 36
Download to read offline
Kinan keshkeh IT Engineering-Damascus University 3rd year 
Summer course- 2014 
2 bytes team
Hi All !! How do you do ?
Templates !
Templates 
•Templates allows you to : 
•define classes and functions that are much more general . 
•define functions and classes that have parameters for type names.
Function Templates
Function Templates 
•Function swapValues swap between variables1 and variables2 : 
•With Template … MORE GENERAL !!
Function Templates 
•Function swapValues swap between variables1 and variables2 : 
•With Template … MORE GENERAL !! 
Template prefix
Function Templates 
•Function swapValues swap between variables1 and variables2 : 
•With Template … MORE GENERAL !! 
Template < class T , Z …..>
Function Templates 
•Function swapValues swap between variables1 and variables2 : 
•With Template … MORE GENERAL !! 
Template works only if the definition be in the same file where we calling
Function Templates 
•Main .cpp :
Function Templates 
•Main .cpp : 
Template works only if the definition be in the same file where we calling
Function Templates 
•Main .cpp :
Class Templates
Class Templates 
#include <iostream> 
using std::cout; 
using std::endl; 
template<class T> 
class Pair 
{ 
public: 
Pair( ); 
Pair(T firstValue, T secondValue); 
void setFirst(T newValue); 
void setSecond(T newValue); 
T getFirst( ) const; 
T getSecond( ) const; 
private: 
T first; 
T second; 
}; 
int main(){ 
cout<<"Object representing pair of int values :n"; 
cout<<" Object name: scoren"; 
Pair<int> score(3,0); 
Cout <<" object value: {" << score.getFirst() 
<<", "<< score.getSecond() << "}n"; 
cout<<"object representing pair of char values :n"; 
cout<<" Object name: seatsn"; 
Pair<char> seats; 
seats.setFirst(65);//A 
seats.setSecond(90);//Z 
cout <<" object value: {" << seats.getFirst() 
<< ", " << seats.getSecond() << "}n"; 
}
Class Templates 
#include <iostream> using std::cout; using std::endl; template<class T> class Pair { public: Pair( ); Pair(T firstValue, T secondValue); void setFirst(T newValue); void setSecond(T newValue); T getFirst( ) const; T getSecond( ) const; private: T first; T second; }; int main(){ cout<<"Object representing pair of int values :n"; cout<<" Object name: scoren"; Pair<int> score(3,0); Cout <<" object value: {" << score.getFirst() <<", "<< score.getSecond() << "}n"; cout<<"object representing pair of char values :n"; cout<<" Object name: seatsn"; Pair<char> seats; seats.setFirst(65);//A seats.setSecond(90);//Z cout <<" object value: {" << seats.getFirst() << ", " << seats.getSecond() << "}n"; } 
T in members ( functions + variables )
Class Templates 
#include <iostream> using std::cout; using std::endl; template<class T> class Pair { public: Pair( ); Pair(T firstValue, T secondValue); void setFirst(T newValue); void setSecond(T newValue); T getFirst( ) const; T getSecond( ) const; private: T first; T second; }; int main(){ cout<<"Object representing pair of int values :n"; cout<<" Object name: scoren"; Pair<int> score(3,0); Cout <<" object value: {" << score.getFirst() <<", "<< score.getSecond() << "}n"; cout<<"object representing pair of char values :n"; cout<<" Object name: seatsn"; Pair<char> seats; seats.setFirst(65);//A seats.setSecond(90);//Z cout <<" object value: {" << seats.getFirst() << ", " << seats.getSecond() << "}n"; } 
When you define an Object from a class template 
put the Type between “ <“ “>”
Class Templates 
template<class T> 
Pair<T>::Pair() 
{ 
first ; 
second ; 
} 
template<class T> 
Pair<T>::Pair(T firstValue, T secondValue) 
{ 
first = firstValue; 
second = secondValue; 
} 
template<class T> 
void Pair<T>::setFirst(T newValue) 
{ 
first = newValue; 
} 
template<class T> 
T Pair<T>::getFirst( ) const 
{ 
return first; 
} 
template<class T> 
T Pair<T>::getSecond( ) const 
{ 
return second; 
} 
template<class T> 
void Pair<T>::setSecond(T newValue) 
{ 
second = newValue; 
}
Class Templates 
template<class T> Pair<T>::Pair() { first ; second ; } template<class T> Pair<T>::Pair(T firstValue, T secondValue) { first = firstValue; second = secondValue; } template<class T> void Pair<T>::setFirst(T newValue) { first = newValue; } template<class T> T Pair<T>::getFirst( ) const { return first; } template<class T> T Pair<T>::getSecond( ) const { return second; } template<class T> void Pair<T>::setSecond(T newValue) { second = newValue; } 
You have to prefix the 
template<class T> 
Before the function class Template members
Class Templates 
template<class T> 
int addUp(const Pair<T>& thePair); 
•When you define the function member in class template : 
• the function gets a classTemplate object parameter
Code discussion 4
Code discussion 4 
• PFArray. h :
Code discussion 4
Code discussion 4 
• PFArray.cpp:
Code discussion 4
Code discussion 4
Code discussion 4 
• main.cpp:
Code discussion 4
Code discussion 5 important !!
Code discussion 5 
• PFArraybak.h:
Code discussion 5 
• PFArraybak.cpp:
Code discussion 5
Code discussion 5
Code discussion 5 
•Main.cpp:
Code discussion 5
That’s for today 
Enjoy and play !! 
Bye Bye !
2 bytes team 
Group : group link Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 
2 bytes team

More Related Content

What's hot

The Ring programming language version 1.5.1 book - Part 174 of 180
The Ring programming language version 1.5.1 book - Part 174 of 180 The Ring programming language version 1.5.1 book - Part 174 of 180
The Ring programming language version 1.5.1 book - Part 174 of 180 Mahmoud Samir Fayed
 
Pavel kravchenko obj c runtime
Pavel kravchenko obj c runtimePavel kravchenko obj c runtime
Pavel kravchenko obj c runtimeDneprCiklumEvents
 
The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210Mahmoud Samir Fayed
 
Templates presentation
Templates presentationTemplates presentation
Templates presentationmalaybpramanik
 
The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.9 book - Part 39 of 210The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.9 book - Part 39 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 41 of 210
The Ring programming language version 1.9 book - Part 41 of 210The Ring programming language version 1.9 book - Part 41 of 210
The Ring programming language version 1.9 book - Part 41 of 210Mahmoud Samir Fayed
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywordsmanish kumar
 

What's hot (20)

Templates
TemplatesTemplates
Templates
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Lecture 7 arrays
Lecture   7 arraysLecture   7 arrays
Lecture 7 arrays
 
The Ring programming language version 1.5.1 book - Part 174 of 180
The Ring programming language version 1.5.1 book - Part 174 of 180 The Ring programming language version 1.5.1 book - Part 174 of 180
The Ring programming language version 1.5.1 book - Part 174 of 180
 
Templates2
Templates2Templates2
Templates2
 
Pavel kravchenko obj c runtime
Pavel kravchenko obj c runtimePavel kravchenko obj c runtime
Pavel kravchenko obj c runtime
 
The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210
 
Algorithm and Programming (Array)
Algorithm and Programming (Array)Algorithm and Programming (Array)
Algorithm and Programming (Array)
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185
 
The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196The Ring programming language version 1.7 book - Part 34 of 196
The Ring programming language version 1.7 book - Part 34 of 196
 
oop objects_classes
oop objects_classesoop objects_classes
oop objects_classes
 
The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.9 book - Part 39 of 210The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.9 book - Part 39 of 210
 
The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88
 
The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181
 
.NET F# Class constructor
.NET F# Class constructor.NET F# Class constructor
.NET F# Class constructor
 
The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196
 
The Ring programming language version 1.9 book - Part 41 of 210
The Ring programming language version 1.9 book - Part 41 of 210The Ring programming language version 1.9 book - Part 41 of 210
The Ring programming language version 1.9 book - Part 41 of 210
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 

Viewers also liked

10 Little Tricks to Get Your Class’s Attention (and Hold It)
10 Little Tricks to Get Your  Class’s Attention (and Hold It)10 Little Tricks to Get Your  Class’s Attention (and Hold It)
10 Little Tricks to Get Your Class’s Attention (and Hold It)kinan keshkeh
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graphkinan keshkeh
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays kinan keshkeh
 
Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714Alaa Bar Avi
 
أخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائيأخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائيAlaa Bar Avi
 
تصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسطتصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسطAlaa Bar Avi
 
النشرالإلكتروني
النشرالإلكترونيالنشرالإلكتروني
النشرالإلكترونيAlaa Bar Avi
 
مبادئ التحرير الإعلامي
مبادئ التحرير الإعلاميمبادئ التحرير الإعلامي
مبادئ التحرير الإعلاميAlaa Bar Avi
 
التحرير الإخباري
التحرير الإخباريالتحرير الإخباري
التحرير الإخباريAlaa Bar Avi
 

Viewers also liked (9)

10 Little Tricks to Get Your Class’s Attention (and Hold It)
10 Little Tricks to Get Your  Class’s Attention (and Hold It)10 Little Tricks to Get Your  Class’s Attention (and Hold It)
10 Little Tricks to Get Your Class’s Attention (and Hold It)
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
 
Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714
 
أخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائيأخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائي
 
تصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسطتصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسط
 
النشرالإلكتروني
النشرالإلكترونيالنشرالإلكتروني
النشرالإلكتروني
 
مبادئ التحرير الإعلامي
مبادئ التحرير الإعلاميمبادئ التحرير الإعلامي
مبادئ التحرير الإعلامي
 
التحرير الإخباري
التحرير الإخباريالتحرير الإخباري
التحرير الإخباري
 

Similar to 2 BytesC++ course_2014_c13_ templates

Similar to 2 BytesC++ course_2014_c13_ templates (20)

The Future of C++
The Future of C++The Future of C++
The Future of C++
 
C++0x Variadic Type List
C++0x Variadic Type ListC++0x Variadic Type List
C++0x Variadic Type List
 
Types, classes and concepts
Types, classes and conceptsTypes, classes and concepts
Types, classes and concepts
 
C++ Templates. SFINAE
C++ Templates. SFINAEC++ Templates. SFINAE
C++ Templates. SFINAE
 
Object Oriented Programming using C++ - Part 5
Object Oriented Programming using C++ - Part 5Object Oriented Programming using C++ - Part 5
Object Oriented Programming using C++ - Part 5
 
C# Generics
C# GenericsC# Generics
C# Generics
 
C++ extension methods
C++ extension methodsC++ extension methods
C++ extension methods
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
OOP - Templates
OOP - TemplatesOOP - Templates
OOP - Templates
 
Developing a new Epsilon EMC driver
Developing a new Epsilon EMC driverDeveloping a new Epsilon EMC driver
Developing a new Epsilon EMC driver
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
Bw14
Bw14Bw14
Bw14
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
For Beginners - C#
For Beginners - C#For Beginners - C#
For Beginners - C#
 
Savitch Ch 17
Savitch Ch 17Savitch Ch 17
Savitch Ch 17
 
麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)
 
.NET 2015: Будущее рядом
.NET 2015: Будущее рядом.NET 2015: Будущее рядом
.NET 2015: Будущее рядом
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
 

More from kinan keshkeh

Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods kinan keshkeh
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm  GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm kinan keshkeh
 
Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...kinan keshkeh
 
2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_unitskinan keshkeh
 
2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_listskinan keshkeh
 
2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked listkinan keshkeh
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointerskinan keshkeh
 
2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfiles2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfileskinan keshkeh
 
2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfileskinan keshkeh
 
2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_records2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_recordskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphismkinan keshkeh
 
2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritancekinan keshkeh
 
2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces 2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces kinan keshkeh
 
2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings 2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings kinan keshkeh
 

More from kinan keshkeh (20)

Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm  GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm
 
Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...
 
2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units
 
2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists
 
2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers
 
2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfiles2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfiles
 
2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles
 
2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_records2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_records
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism
 
2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance
 
2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces 2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces
 
2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings 2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings
 

Recently uploaded

WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 

Recently uploaded (20)

WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 

2 BytesC++ course_2014_c13_ templates

  • 1. Kinan keshkeh IT Engineering-Damascus University 3rd year Summer course- 2014 2 bytes team
  • 2. Hi All !! How do you do ?
  • 4. Templates •Templates allows you to : •define classes and functions that are much more general . •define functions and classes that have parameters for type names.
  • 6. Function Templates •Function swapValues swap between variables1 and variables2 : •With Template … MORE GENERAL !!
  • 7. Function Templates •Function swapValues swap between variables1 and variables2 : •With Template … MORE GENERAL !! Template prefix
  • 8. Function Templates •Function swapValues swap between variables1 and variables2 : •With Template … MORE GENERAL !! Template < class T , Z …..>
  • 9. Function Templates •Function swapValues swap between variables1 and variables2 : •With Template … MORE GENERAL !! Template works only if the definition be in the same file where we calling
  • 11. Function Templates •Main .cpp : Template works only if the definition be in the same file where we calling
  • 14. Class Templates #include <iostream> using std::cout; using std::endl; template<class T> class Pair { public: Pair( ); Pair(T firstValue, T secondValue); void setFirst(T newValue); void setSecond(T newValue); T getFirst( ) const; T getSecond( ) const; private: T first; T second; }; int main(){ cout<<"Object representing pair of int values :n"; cout<<" Object name: scoren"; Pair<int> score(3,0); Cout <<" object value: {" << score.getFirst() <<", "<< score.getSecond() << "}n"; cout<<"object representing pair of char values :n"; cout<<" Object name: seatsn"; Pair<char> seats; seats.setFirst(65);//A seats.setSecond(90);//Z cout <<" object value: {" << seats.getFirst() << ", " << seats.getSecond() << "}n"; }
  • 15. Class Templates #include <iostream> using std::cout; using std::endl; template<class T> class Pair { public: Pair( ); Pair(T firstValue, T secondValue); void setFirst(T newValue); void setSecond(T newValue); T getFirst( ) const; T getSecond( ) const; private: T first; T second; }; int main(){ cout<<"Object representing pair of int values :n"; cout<<" Object name: scoren"; Pair<int> score(3,0); Cout <<" object value: {" << score.getFirst() <<", "<< score.getSecond() << "}n"; cout<<"object representing pair of char values :n"; cout<<" Object name: seatsn"; Pair<char> seats; seats.setFirst(65);//A seats.setSecond(90);//Z cout <<" object value: {" << seats.getFirst() << ", " << seats.getSecond() << "}n"; } T in members ( functions + variables )
  • 16. Class Templates #include <iostream> using std::cout; using std::endl; template<class T> class Pair { public: Pair( ); Pair(T firstValue, T secondValue); void setFirst(T newValue); void setSecond(T newValue); T getFirst( ) const; T getSecond( ) const; private: T first; T second; }; int main(){ cout<<"Object representing pair of int values :n"; cout<<" Object name: scoren"; Pair<int> score(3,0); Cout <<" object value: {" << score.getFirst() <<", "<< score.getSecond() << "}n"; cout<<"object representing pair of char values :n"; cout<<" Object name: seatsn"; Pair<char> seats; seats.setFirst(65);//A seats.setSecond(90);//Z cout <<" object value: {" << seats.getFirst() << ", " << seats.getSecond() << "}n"; } When you define an Object from a class template put the Type between “ <“ “>”
  • 17. Class Templates template<class T> Pair<T>::Pair() { first ; second ; } template<class T> Pair<T>::Pair(T firstValue, T secondValue) { first = firstValue; second = secondValue; } template<class T> void Pair<T>::setFirst(T newValue) { first = newValue; } template<class T> T Pair<T>::getFirst( ) const { return first; } template<class T> T Pair<T>::getSecond( ) const { return second; } template<class T> void Pair<T>::setSecond(T newValue) { second = newValue; }
  • 18. Class Templates template<class T> Pair<T>::Pair() { first ; second ; } template<class T> Pair<T>::Pair(T firstValue, T secondValue) { first = firstValue; second = secondValue; } template<class T> void Pair<T>::setFirst(T newValue) { first = newValue; } template<class T> T Pair<T>::getFirst( ) const { return first; } template<class T> T Pair<T>::getSecond( ) const { return second; } template<class T> void Pair<T>::setSecond(T newValue) { second = newValue; } You have to prefix the template<class T> Before the function class Template members
  • 19. Class Templates template<class T> int addUp(const Pair<T>& thePair); •When you define the function member in class template : • the function gets a classTemplate object parameter
  • 21. Code discussion 4 • PFArray. h :
  • 23. Code discussion 4 • PFArray.cpp:
  • 26. Code discussion 4 • main.cpp:
  • 28. Code discussion 5 important !!
  • 29. Code discussion 5 • PFArraybak.h:
  • 30. Code discussion 5 • PFArraybak.cpp:
  • 33. Code discussion 5 •Main.cpp:
  • 35. That’s for today Enjoy and play !! Bye Bye !
  • 36. 2 bytes team Group : group link Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 2 bytes team