Programming Using C++ Session 11
Programming Using C++ Session 11
Objectives
In this lesson, you will learn to:
Define polymorphism
Overload functions
Identify overloading functions as an implementation of
static polymorphism
Understand the need for overloading operators
Overload the following unary operators:
Simple prefix unary operators
Pre and post increment and decrement operators
Overload a binary operator
©NIIT Programming Using C++/Session 11/Slide 1 of 27
Polymorphism
Static Polymorphism
Refers to an entity existing in different physical forms
simultaneously
Function Overloading
Is the process of using the same name for two or
more functions
Requires each redefinition of a function to use a
different function signature that is:
different types of parameters,
or sequence of parameters,
or number of parameters
Is used so that a programmer does not have to
remember multiple function names
Constructor Overloading
Is commonly used in C++
Example:
#include <iostream>
class Calculator
{
int number1, number2, tot;
public:
Calculator();//Default Constructor
Calculator(int,int);//Two-Argument
//Constructor
};
Classification of Operators
Unary operators
Simple prefix unary operators
Pre and post increment and decrement operators
Binary operators
Simple operators
Comparison operators
The assignment operator
The insertion and extraction operator
Special operators
return temp;
}
Just a Minute…
Modify the existing employee class such that when the
following statements are given in the main() function,
the program successfully compares the basic salary of
the employees and displays the given message.
#include <iostream>
void main()
{
Employee eObj1, eObj2;
eObj1.getdata(); //Accepts data
eObj2.getdata();
if(eObj1<eObj2)
cout<< “Employee 1 draws less salary
than Employee 2”;
©NIIT Programming Using C++/Session 11/Slide 20 of 27
Polymorphism
Just a Minute…(Contd.)
else
cout<< “Employee 2 draws less salary
than Employee 1”;
}
Summary
In this lesson, you learned that:
The term polymorphism has been derived form the
Greek words ‘poly’ and ‘morphos’, which means
‘many’ and ‘forms’, respectively
Function overloading is the process of using the same
name for two or more functions
The number, type, or sequence of parameters for a
function is called the function signature
Static polymorphism is exhibited by a function when it
exists in different forms
Summary (Contd.)
Operator overloading refers to providing additional
meaning to the normal C++ operators when they are
applied to user-defined data types
Operator overloading improves the clarity of user-
defined data types
The predefined C++ operators can be overloaded by
using the operator keyword
Operators may be considered as functions internal to
the compiler
Operators may be classified into two types: Unary and
Binary
Unary operators work with one operand
Summary (Contd.)
Unary operators can be classified as:
Simple prefix unary operators, for example, ! and -
Pre and post increment and decrement operators
A prefix unary operator may be defined by a member
function taking no parameter or a non-member
function taking one parameter
In order to avoid confusion between pre and post-fix
operators, all postfix unary operators take in a dummy
integer
Binary operators work with two operands
Summary (Contd.)
Overloading a binary operator is similar to overloading
a unary operator except that a binary operator
requires an additional parameter
In order to understand their overloading better, binary
operators may be classified as follows:
Simple operators, like + - * / % += -=
Comparison operators, like < > <= >= !=
==
The assignment operator =
The insertion and extraction operator << >>