DSA Lab Manual
DSA Lab Manual
DSA Lab Manual
Islamabad
LAB MANUAL
Lab Designers
Tehreem Shabbir
Teaching Fellow
Faculty of Computing
Table of Contents
Contents
Lab 4: Introduction to Operator Overloading & Templates................................................................. 5
1. Introduction .............................................................................................................................. 5
2. Activity Time boxing................................................................................................................... 5
3. Lab Manual Lecture [Expected time = 30 minutes] ........................................................ 5
4. Objective ................................................................................................................................... 5
5. Concept Map ............................................................................................................................. 5
5.1 The Structure of an Operator ............................................................................................. 5
5.2 Syntax ............................................................................................................................ 6
6. Templates.................................................................................................................................. 9
6.1 Function templates .................................................................................................................. 9
6.2 Class templates ...................................................................................................................... 10
7. Evaluation criteria.................................................................................................................... 13
8. Further Reading ....................................................................................................................... 13
8.1 Books ............................................................................................................................... 13
9.1 Out comes ............................................................................................................................. 13
––
Lab Manual for Programming Fundamentals
Lab 4: Introduction to Operator Overloading &
Templates
Lab 4: Introduction to Operator Overloading &
Templates
1. Introduction
In C++, we can make operators to work for user defined classes. This means C++ has the ability to provide
the operators with a special meaning for a data type, this ability is known as operator overloading.
For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings
by just using +.
Other example classes where arithmetic operators may be overloaded are Complex Number, Fractional
Number, Big Integer, etc.
4. Objective
1. Learn Operator Overloading
2. Learn Templates
5. Concept Map
Operator Overloading • Operator overloading is the process of providing object specific functionality for
the base operators:
1. +
2. -
3. /
4. *
You can overload these to allow you to, for example, divide objects by each other or multiply them
together.
1. Operator is +
2. Operands and 4 and 5
3. Returns the sum of the two operands – 9
5.2 Syntax
An Unary operator is an operator that operates on a single operand and returns a new value.
Some of the unary operators are,
++ (Increment Operator)
- - (Decrement operator)
– (Unary Minus operator)
! (NOT Operator)
#include<iostream>
/*
#include <iostream>
class Test
{
private:
int count;
public:
Test(): count(5){}
count = count+1;
};
int main()
Test t;
++t;
t.Display();
return 0;
The binary operators take two arguments and following are the examples of Binary operators. You use
binary operators very frequently like addition (+) operator, subtraction (-) operator and division (/)
operator.
#include <iostream>
class Box
{
public:
int length;
int breadth;
public:
Box()
length = 9;
breadth = 12;
Box box;
cout<<"box.breadth "<<box.breadth<<endl;
cout<<"This.breadth "<<this->breadth<<endl;
return box;
};
int main()
Box Box1;
Box Box2;
Box Box3;
Box2.breadth = 11;
// Box1.breadth=5;
int a = Box3.breadth;
return 0;
6. Templates
The format for declaring function templates with type parameters is:
// function template
#include <iostream>
T GetMax (T a, T b)
T max;
if(a>b)
{ max = a; }
else
max=b;
return max;
int main () {
k=GetMax(i,j);
n=GetMax(l,m);
return 0;
The class that we have just defined serves to store two elements of any valid type. For example, if we
wanted to declare an object of this class to store two integer values of type int with the values 115 and
36 we would write:
this same class would also be used to create an object to store any other type:
// class templates
#include <iostream>
class mypair {
T a, b;
public:
{a=first; b=second;}
T getmax ();
};
T mypair<T>::getmax ()
T retval;
retval = a>b? a : b;
return retval;
}
int main () {
return 0;
6. Practice Tasks
Task 1: [Time Required: 30 minutes]
Define a class IncrementDecrement
1. Declare a variable inside it .
2. Class must have at least one constructor i.e. Default constructor for initializing var to some default
value.
3. Write a function Display to show the value of var.
4. Write a function operator ++( ) inside which you have to increment the value of var, and return it as
well.
5. Inside main function, create an object of the class , say obj1.
6. Call Display() to show the initial value of i
7. Apply increment to obj1
8. Save the value of obj1 in some other object, say obj2
9. Call Display for Obj2 to display the increment
10. Also overload Decrement operator.
8. Further Reading
8.1 Books
9. The slides and reading material can be accessed from the folder of the class instructor available at
vle.