Unit 4
Unit 4
• Function Overloading:
When there are multiple functions with same name but different number of arguments.
Based on how many parameters are passed during function call determines which
function is to be called, this is why it is considered as an example of polymorphism
because in different conditions the output is different.
Since, the call is determined during compile time that is why it is called compile time
polymorphism.
Operator Overloading
• Operator overloading is the technique using which a different meaning is given to the existing operators in
C++.
• Most of the operators in C++ are overloaded or given special meaning so that they can work on user-
defined data types. Note that while overloading, the basic operation of operators is not altered.
Overloading just gives the operator an additional meaning by keeping their basic semantic same.
• Though most of the operators can be overloaded in C++, there are some operators which cannot be
overloaded:
• Scope resolution operator(::)
• Sizeof
• member selector(.)
• member pointer selector(*)
• ternary operator(?:)
Defining Operator Overloading
Overloading Unary Operators (using member function)
++ increment the value of basic data type but if we overload this, it can increment each data member of an object.
Overloading Unary Operators (using friend function)
Rules for overloading operators
• Only existing operators can be overloaded
• The basic meaning of operator can’t be changed
• Overloaded operators must follow the syntax of original operator. For example: for binary operator
overloading: operand1 op operand 2 (a+b)
• Unary operator overloaded through a member function takes no explicit arguments and return no explicit
values, but those overloaded through a friend function takes one explicit argument, take one reference
argument (object of the relevant class)
• Binary arithmetic operators (+,-,*,/) must explicitly return a value
• Binary operators overloaded through member function take one explicit argument and those overloaded
through a friend function takes two explicit arguments
• When using binary operators overloaded through a member function, the left-hand operand must be an
object of the relevant class
• There are some operators which can not be overloaded (::,.,?:,*)
Overloading Binary operator using member function
Overloading Binary operator
using friend function
• Friend function can also be used in place of member functions for
overloading a binary operator, the only difference being that a friend
function requires two arguments to be explicitly passed to it, while a
member function requires only one.
• In the complex number program discussed in the previous section,
the statement:
t3=t1+t2;
is equivalent to:
t3= operator+(t1,t2);
Overloading Assignment operator
• The assignment operator must be overloaded as a member function
Overloading Relational Operator (<)
In stream/ out stream operator
overloading
• We can input and output the built-in data types using << and >>
operators. The stream insertion and stream extraction operators can
be overloaded to perform input and output for objects also.
• In this case, it is important to make operator overloading function a
friend of the class as it would be called without creating an object and
two parameters compulsorily needs to be passed to it.
In stream/ out stream operator overloading
Run Time Polymorphism
• Consider a situation where function name and prototype is same in both base class and derived classes. For example, consider the following
class definitions:
Pointers to objects
• A pointer can point to an object created by a class. Consider the following statement:
item x;
• Similarly, we can define a pointer ptr of type item as follows:
item *ptr;
• Object pointers are useful in creating objects at run time. We can also use an object pointer to access public members of
an object. Consider a class item defined as follows:
Pointers to objects
Abstract Class and Pure Virtual Function
Pointers to Derived classes
Virtual Functions
Real world example of virtual
function
Function display() can be used in all the classes to display the class contents
Declare display() as virtual in base class: media
Rules for virtual functions
• When virtual functions are created for implementing late binding, following rules
must be satisfied:
Pure Virtual Functions
Real-Life Example to Understand the Implementation
of Virtual Function
Syntax:
class subclass_name : access_mode base_class1, access_mode
base_class2, ....
{
//body of subclass
};
3. Multilevel Inheritance: In this type of inheritance, a derived class is
created from another derived class.
4. Hierarchical Inheritance: In this type of inheritance, more than one sub
class is inherited from a single base class. i.e. more than one derived class
is created from a single base class.
5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by
combining more than one type of inheritance. For example: Combining
Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritance: