KEMBAR78
Object Oriented Programming using C++: C++ Templates.pptx
1
 Templates are powerful features of C++ which allows us to write generic
programs. There are two ways we can implement templates:
 Function Templates
 Class Templates
 Similar to function templates, we can use class
templates to create a single class to work with
different data types.
 Class templates come in handy as they can make our code shorter and more
manageable.
C++ Class Templates
Templates
Function
Templates
Class
Templates
#include <iostream>
#include <stdlib.h>
using namespace std;
int add(int a, int b){
return a + b;
}
double add(double a, double b){
return a + b;
}
int main(){
int iA = 3, iB = 4;
double dA = 3.4, dB = 4.3;
cout << iA << " + "<< iB << " = " << add(iA, iB) << endl;
cout << dA << " + " << dB << " = " << add(dA, dB) << endl;
system("PAUSE");
return 0;
}
3
Example 1: Function Overloading
1
#include <iostream>
#include <stdlib.h>
using namespace std;
template <typename T>
T add(T a, T b){
return a + b;
}
int main(){
int iA = 3, iB = 4;
double dA = 3.4, dB = 4.3;
cout << iA <<" + "<< iB << " = "<< add(iA, iB) << endl;
cout << dA <<" + " << dB << " = " << add(dA, dB) << endl;
system("PAUSE");
return 0;
}
4
Example 2: One Argument Function Template
1
#include <iostream>
#include <stdlib.h>
using namespace std;
template <typename T>
inline T const& Max(T const& a, T const& b){
return a < b ? b:a;
}
int main(){
int i = 39, j = 20;
cout << "Max("<<i<<","<< j<<") = " << Max(i, j) << endl;
double d1 = 13.5, d2 = 20.7;
cout << "Max("<<d1<<","<< d2<<") = " << Max(d1, d2) << endl;
system("PAUSE");
return 0;
}
5
Example 3: Two Arguments Function Template
1
#include <iostream>
#include <stdlib.h>
using namespace std;
template <typename T> class Calculator {
private:
T num1, num2;
public:
Calculator(T n1, T n2) {
num1 = n1; num2 = n2;
}
void Result(){
cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
cout << num1 << " + "<< num2 <<" = "<< add() << endl;
cout << num1 << " / "<< num2 <<" = "<< divide() << endl;
}
T add();
T divide();
};
6
Example 4: Calculator using Class Template (1/2)
1
template <typename T> T Calculator<T> :: add() {
return num1 + num2;
}
template <typename T> T Calculator<T> :: divide() {
return num1 / num2;
}
int main() {
Calculator <int> iCalc(5, 2);
Calculator <double> dCalc(2.6, 1.2);
cout << "Int Result:" << endl;
iCalc.Result();
cout << endl << "Double Result:" << endl;
dCalc.Result();
system("PAUSE");
return 0;
}
7 2
Example 4: Calculator using Class Template (2/2)
#include <iostream>
using namespace std;
const int SIZE = 3; // stack size
template <typename T> class Stack {
private:
T stack_array[SIZE]; int top;
public:
Stack() { top = -1; }
bool Is_Empty() { return top <= -1; }
bool Is_Full () { return top >= SIZE-1; }
void Push ( T data );
void Pop ( );
T Top();
};
template<typename T> void Stack<T> :: Push(T data) {
if (Is_Full()) // if stack full,
cout << "Error: Stack is full"<< endl;
else
stack_array[++top] = data;
}
8 2
Example 5: Stack Using Class Template (1/2)
template<typename T> void Stack<T> :: Pop() {
if (!Is_Empty()) top--;
}
template<typename T> T Stack<T> :: Top() {
if (!Is_Empty())
return stack_array[top];
return NULL;
}
int main() {
Stack<int> st;
int i = 1;
while(!st.Is_Full()){
st.Push(i++*11);
}
while(!st.Is_Empty()){
cout << st.Top() << endl;
st.Pop();
}
system("PAUSE"); return 0;
}
9 2
Example 5: Stack Using Class Template (1/2)
#include <iostream>
#include <stdlib.h>
using namespace std;
template<class T1, class T2> class Test_Class {
T1 a;
T2 b;
public:
Test_Class (T1 a1, T2 b2){
a = a1; b = b2;
}
void Test(T1 x, T2 y);
void Show( );
};
10
Example 6: Using Two Templates in a Class (1/2)
1
template<class T1, class T2> void Test_Class <T1,T2> :: Test(T1 x, T2 y) {
a = x;
b = y;
}
template<class T1, class T2> void Test_Class <T1,T2> :: Show() {
cout << a << " and " << b << endl;
}
int main() {
Test_Class <double,int> test1 (1.23, 123);
Test_Class <int,char> test2 ( 200, 'Z');
test1.Show();
test2.Show();
system("PAUSE");
return 0;
}
11 2
Example 6: Using Two Templates in a Class (2/2)

Object Oriented Programming using C++: C++ Templates.pptx

  • 1.
  • 2.
     Templates arepowerful features of C++ which allows us to write generic programs. There are two ways we can implement templates:  Function Templates  Class Templates  Similar to function templates, we can use class templates to create a single class to work with different data types.  Class templates come in handy as they can make our code shorter and more manageable. C++ Class Templates Templates Function Templates Class Templates
  • 3.
    #include <iostream> #include <stdlib.h> usingnamespace std; int add(int a, int b){ return a + b; } double add(double a, double b){ return a + b; } int main(){ int iA = 3, iB = 4; double dA = 3.4, dB = 4.3; cout << iA << " + "<< iB << " = " << add(iA, iB) << endl; cout << dA << " + " << dB << " = " << add(dA, dB) << endl; system("PAUSE"); return 0; } 3 Example 1: Function Overloading 1
  • 4.
    #include <iostream> #include <stdlib.h> usingnamespace std; template <typename T> T add(T a, T b){ return a + b; } int main(){ int iA = 3, iB = 4; double dA = 3.4, dB = 4.3; cout << iA <<" + "<< iB << " = "<< add(iA, iB) << endl; cout << dA <<" + " << dB << " = " << add(dA, dB) << endl; system("PAUSE"); return 0; } 4 Example 2: One Argument Function Template 1
  • 5.
    #include <iostream> #include <stdlib.h> usingnamespace std; template <typename T> inline T const& Max(T const& a, T const& b){ return a < b ? b:a; } int main(){ int i = 39, j = 20; cout << "Max("<<i<<","<< j<<") = " << Max(i, j) << endl; double d1 = 13.5, d2 = 20.7; cout << "Max("<<d1<<","<< d2<<") = " << Max(d1, d2) << endl; system("PAUSE"); return 0; } 5 Example 3: Two Arguments Function Template 1
  • 6.
    #include <iostream> #include <stdlib.h> usingnamespace std; template <typename T> class Calculator { private: T num1, num2; public: Calculator(T n1, T n2) { num1 = n1; num2 = n2; } void Result(){ cout << "Numbers are: " << num1 << " and " << num2 << "." << endl; cout << num1 << " + "<< num2 <<" = "<< add() << endl; cout << num1 << " / "<< num2 <<" = "<< divide() << endl; } T add(); T divide(); }; 6 Example 4: Calculator using Class Template (1/2) 1
  • 7.
    template <typename T>T Calculator<T> :: add() { return num1 + num2; } template <typename T> T Calculator<T> :: divide() { return num1 / num2; } int main() { Calculator <int> iCalc(5, 2); Calculator <double> dCalc(2.6, 1.2); cout << "Int Result:" << endl; iCalc.Result(); cout << endl << "Double Result:" << endl; dCalc.Result(); system("PAUSE"); return 0; } 7 2 Example 4: Calculator using Class Template (2/2)
  • 8.
    #include <iostream> using namespacestd; const int SIZE = 3; // stack size template <typename T> class Stack { private: T stack_array[SIZE]; int top; public: Stack() { top = -1; } bool Is_Empty() { return top <= -1; } bool Is_Full () { return top >= SIZE-1; } void Push ( T data ); void Pop ( ); T Top(); }; template<typename T> void Stack<T> :: Push(T data) { if (Is_Full()) // if stack full, cout << "Error: Stack is full"<< endl; else stack_array[++top] = data; } 8 2 Example 5: Stack Using Class Template (1/2)
  • 9.
    template<typename T> voidStack<T> :: Pop() { if (!Is_Empty()) top--; } template<typename T> T Stack<T> :: Top() { if (!Is_Empty()) return stack_array[top]; return NULL; } int main() { Stack<int> st; int i = 1; while(!st.Is_Full()){ st.Push(i++*11); } while(!st.Is_Empty()){ cout << st.Top() << endl; st.Pop(); } system("PAUSE"); return 0; } 9 2 Example 5: Stack Using Class Template (1/2)
  • 10.
    #include <iostream> #include <stdlib.h> usingnamespace std; template<class T1, class T2> class Test_Class { T1 a; T2 b; public: Test_Class (T1 a1, T2 b2){ a = a1; b = b2; } void Test(T1 x, T2 y); void Show( ); }; 10 Example 6: Using Two Templates in a Class (1/2) 1
  • 11.
    template<class T1, classT2> void Test_Class <T1,T2> :: Test(T1 x, T2 y) { a = x; b = y; } template<class T1, class T2> void Test_Class <T1,T2> :: Show() { cout << a << " and " << b << endl; } int main() { Test_Class <double,int> test1 (1.23, 123); Test_Class <int,char> test2 ( 200, 'Z'); test1.Show(); test2.Show(); system("PAUSE"); return 0; } 11 2 Example 6: Using Two Templates in a Class (2/2)