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

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
+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
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
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
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 

Recently uploaded (20)

Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
+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...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
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 🔝✔️✔️
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.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