Polymorphism
Polymorphism
Written by
Unit Outcome 1 : Create C++
programs to perform the given
arithmetic operations using
pointers
Written by
Learning Outcome 1 : student will be
able to use pointers.
Written by
What we will learn today
Pointer
Declaration
Working Of
Pointer Pointer
Initialization
Pointer
Memory
Allocation Operation
on Pointer
► Every variable is a memory location and every memory location has its address defined
► Address can be accessed using ampersand (&) operator which denotes an address
► C++ tasks are performed more easily with pointers
► C++ tasks, such as dynamic memory allocation, cannot be performed without them
► Pointer is a variable in C++ that holds the address of another variable.
► Pointers have data type just like variables.
► Pointers are variables which display value at the memory address.
► Like any variable or constant, you must declare a pointer before you can work with it
7
► Declaring Pointers
► Syntax:
data_type *pointer_name;
int *ptr; //declaration
8
ptr=&a; //initialization
Pointer Operator
#include<iostream.h>
void main()
{ ptr a
int a=10;
int *ptr; Value of ptr 1024 10
ptr=&a; 1000 1024
cout<<"\n Value of a:"<<a; //a=10 Value of a
► Operation
► Increment operator (++)
► Decrement operator (–)
► Addition (+)
► Subtraction (-)
► Valid statements
► p2-p1;
► ptr1+1;
► ptr=ptr+2;
► Invalid statements
► p1+p2;
► p2 * p1;
► p2/p1;
Page 11 Maharashtra State Board of Technical Education 4 July 2020
Pointer Arithmetic
Program code
Output
Pointer to
Object Pointee derived
class
► C++ uses a unique keyword called ‘this’ to represent an object that invokes a
member function.
► This unique pointer is automatically passed to a member function when it is invoked.
► ‘this’ is a pointer that always point to the object for which the member function was
called.
► The ‘this’ pointer can be treated like any other pointer to an object.
► For example, the function call
20
A.max () will set the pointer ‘this’ to the address of the object A.
Next time suppose we call B.max(), the pointer ‘this’ will store address of object B.
If we try to write
x=x;
It will give garbage Value
Output :
Page 22
to D. Maharashtra State Board of Technical Education 4 July 2020
Pointers to derived class
► Example:
B *cptr; // pointer to B class
B b; //Base object
D d; // Derived object
cptr=&b; //ptr points to object b
Output :
Written by
Learning Outcome 1 : student will be
able to use different types of
Polymorphism
Written by
What we will learn today
MANY FORMS
31
Business
man
Salesman
Family
head
Employee
Polymorphism
32
Customer
POLYMORPHISM
FUNCTION OPERATOR 33
VIRTUAL FUNCTION
OVERLOADING OVERLOADING
class base
► Runtime polymorphism or Dynamic Method
{
Dispatch is a process in which a call to an public:
void show ()
overridden method is resolved at runtime rather { cout<< "show base class" ;}
};
than compile-time.
► Function to be called is unknown until ► Functions to be called are known well before.
appropriate selection is made.
► This does not require use of pointers to object. ► This requires use of pointers to object
► Function calls execution are faster ► Function calls execution are slower
► Also called as early binding, Static binding ► Also called as late binding, dynamic binding.
► It is implemented with operator overloading or ► It is implemented with virtual function.
function overloading
Written by
What we will learn today
class
► What is overloading?
► In C++ two functions can share the same name as long as their parameter declarations are different.
► In this situation, the functions that share the same name are said to be overloaded, and the process
is referred to as function overloading.
► The secret to function overloading is that each function should use either :
❑ Different types of parameters such as int, float, etc.
❑ Different number of parameters such as function1 contains 2 parameters and function2 contains
4 parameters.
42
► In this example the function’s basic execution is to perform addition of two numbers.
► The name of both the functions is same but the datatype of the parameter of both the functions are
different.
► So while compiling if the data passed to the function is of integer type then the compiler will execute
the definition of that function.
44
► In this example the function’s basic execution is to calculate the area of different shapes.
► The name is same of both function, but the number of parameters are different.
► This helps compiler to help which function to call during the compilation process.
Output Of Program
Operator
(+)
Addition Concatenation
(2+3) (“ABC”+”DEF”=“ABCDEF”)
► What is Overloading?
❑ Function Overloading
Compile time
❑ Operator overloading
50
► In C++ we can specify new meaning to an existing operator which is called as Operator overloading.
► In overloading, the operators are implemented as functions using the operator keyword.
► We can redefine or overload some of the built-in operators by giving user defined meaning to it.
► For example, we can overload an operator ‘+’ in a class for String so that we can concatenate two
strings by just using ‘+’.
► Example
The input/output operators << and >> are good examples of operator overloading
cout<<“HELLO WORLD!”;
Overloaded insertion
operator
The built-in definition of the << operator is for shifting of bits.
It is also used for displaying the values of various data types.
► Almost all operators in C++ can be overloaded except the given below:
.
2. Scope resolution operator( :: )
3. Sizeof operator(sizeof)
sizeof() Operator ::
4. Conditional oprator( ?: )
54
?:
SuperClass
Extends
Subclass
► To achieve Run time polymorphism, C++ supports a mechanism known as virtual function.
► A virtual function in a base class must be defined, even though it may not be used
Program Output
► A pure virtual function is a function declared in a base class that has no definition.
► The compiler requires each derived class to either define the function or redeclare it as a pure
virtual function.
► Such functions are also called as ’do-nothing’ functions.
► Ex:-
class ABC
{
public:
virtual void display( )=0; 67
};
Program Output
► https://msbte.org.in/
► PPT: Jaishree Sudhakar Anerao (Lecturer, Department of Computer Engineering[NBA Accredited],Vivekanand
Education Society’s Polytechnic, Mumbai)