2. Module No.
Content
Teaching Hours
I
Fundamentals of Object Oriented Programming: Procedure Oriented Programming vs. Object Oriented Programming
(OOP).
Object Oriented Programming Concepts: Classes, Objects, Encapsulation, Inheritance, Polymorphism, Dynamic Binding,
Message Passing and Abstraction; Benefits and Applications of Object Oriented Programming.
Introduction to C++: What is C++, A Simple C++ Program, Structure of C++ Program, Dynamic Initialization of
Variables, Reference Variables, Scope Resolution Operators, and Manipulators.
Functions in C++: Call by Value & Reference, Inline Function, Default Arguments, Function Overloading.
Classes and Objects: Creation of Class, Accessing Class Members, Private vs Public Objects, Member Functions, Method
Definition, Constant Member Functions, Overloading Member Functions, Array within a Class, Memory Allocation for
Objects, Static Data Members and Static Member Functions, Arrays of Objects, Objects as Function Arguments, Friend
Functions, and Returning Objects.
Constructor and Destructor: Introduction to Constructors Parameterized Constructors, Multiple Constructors in a Class,
Constructor with Default Arguments, Copy Constructors.
20
II
Operator Overloading: Defining Operator Overloading, Overloading Unary Operator, Overloading Binary Operators,
Overloading Binary Operators Using Friend, Rules for Overloading Operators.
Inheritance: Introduction, Defining Derived Classes, Types of Inheritance- Single, Multiple, Multilevel, Hierarchical,
Hybrid Inheritance, Virtual Base Classes, Abstract Classes, and Constructors to Derived Classes.
Pointers, Virtual Functions: Introduction, Pointers to Objects, Virtual Function Pure Virtual Functions, Object Slicing.
Templates Function & Class Templates, Class Templates with Multiple Parameters.
Exception Handing: Introduction, Basics of Exception Handling, Exception Handling Mechanism, Throwing Mechanism,
Catching Mechanism, Rethrowing an Exception
20
4/3/2020 2
4. Operator Overloading
• 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/3/2020 4
5. General Format
5
returnType operator*(parameters);
any type
keyword
operator symbol
Return type may be whatever the operator returns
Including a reference to the object of the operand
Operator symbol may be any overloadable operator from
the list.
6. Can we overload all operators?
4/3/2020 6
Almost all operators can be overloaded except few.
Following is the list of operators that cannot be overloaded.
. (dot)
::
?:
sizeof
7. Rules For Overloading Operators
Only existing operators can be overloaded. New operators cannot be
created.
The overloaded operator must have at least one operand that is of user-
defined type.
We cannot change the basic meaning of an operator.
Overloaded operators follow the syntax rules of the original operators.
7
8. Rules For Overloading Operators
The following operators that cannot be overloaded:
Size of Size of operator
. Membership operator
.* Pointer-to-member operator
: : Scope resolution operator
? ; Conditional operator
continue…
8
9. Rules For Overloading Operators continue…
28
The following operators can be over loaded with the use of member functions and
not by the use of friend functions:
Assignment operator =
Function call operator( )
Subscripting operator [ ]
Class member access operator ->
Unary operators, overloaded by means of a member function, take no explicit arguments
and return no explicit values, but, those overloaded by means of a Bfyr:-iGeounradvKfouttanwcartion, take one
reference argument.
10. Rules For Overloading Operators
Binary operators overloaded through a member function take one
explicit argument and those which are overloaded through a friend
function take two explicit arguments.
When using binary operators overloaded through a member function,
the left hand operand must be an object of the relevant class.
Binary arithmetic operators such as +, -, * and / must explicitly return a
value. They must not attempt to change their own arguments.
continue…
1
0
19. Important points about operator
overloading
1) For operator overloading to work, at least one of the operands
must be a user defined class object.
2) Assignment Operator: Compiler automatically creates a default
assignment operator with every class.
The default assignment operator does assign all members of right
side to the left side and works fine most of the
cases (this behavior is same as copy constructor).
4/3/2020 19
20. 3) Conversion Operator: We can also write conversion operators that can be used
to convert one type to another type.
#include <iostream>
using namespace std;
class Fraction
{
int num, den;
public:
Fraction(int n, int d) { num = n; den = d; }
// conversion operator: return float value of fraction
operator float()
const {
return float(num) / float(den);
} }; 4/3/2020 20
int main() {
Fraction f(2, 5);
float val = f;
cout << val;
return 0;
}