0% found this document useful (0 votes)
15 views8 pages

DS 03 Using Templates in C++

Using Templates in C++ ppt

Uploaded by

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

DS 03 Using Templates in C++

Using Templates in C++ ppt

Uploaded by

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

Data Structure

Video Lecture

Lecture No. 3
Using Templates in C++
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chis
hti International Islamic University H-10, Islamabad, Pakistan
C++ Class Templates
 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.
Example 1: Function Overloading
#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 1
Example 2: Function Template
#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 1
Example 3: Class Template (1/2)
#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();
};
5 1
Example 3: Class Template (2/2)
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;
}
6 2
Example 4: Using Two Templates in a Class (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( );
};

7 1
Example 4: Using Two Templates in a Class (2/2)
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;
}

8 2

You might also like