SlideShare a Scribd company logo
Dr. BABASAHEB AMBEDKAR TECHNOLOGICAL UNIVERSITY
                Presentation on


Pointers , Virtual Functions and Polymorphism.
                       By ,
                                Ruturaj Nalawade
                            Sanjay Bidkar
                            Swapnil Sarwade
                      Under the Guidance of ,
                            Mrs . Ladda
POINTERS
INTRODUCTION
Pointers are the variables which
 holds the addresses of other
 variables.

Pointer variables are denoted by
  „ * ptr ‟

Pointers are the derived data types.
INTRODUCTION – CONT.
E.g. :=
{
          int i , *j;
          i= 3;
          j = &i;
          cout<<“The value of i is t”<<i<<endl;
          cout<<“The value of *j is t”<<*j;
}

Output : :

The value of i is      3
The value of *j is     3
INTRODUCTION – CONT.
E.g. :=
{
          int i , *j;
          i= 3;
          j = &i;
          cout<<“The value of i is t”<<i<<endl;
          cout<<“The value of *j is t”<<*j; * j
}

Output : :

The value of i is      3
The value of *j is     3
Introduction - Cont.
 How the *j gets the value of i ?

int i , *j ;




Variable                      i        j
Names
Value

Memory                      65524    65522
Address
Introduction - Cont.
 How the *j gets the value of i ?

int i , *j ;
i=3;




Variable                      i        j
Names
Value                         3

Memory                      65524    65522
Address
Introduction - Cont.
 How the *j gets the value of i ?

int i , *j ;
i=3;
j=&i;


Variable                      i            j
Names
Value                         3          65524

Memory                      65524        65522
Address




  *j refers to the value at address j.
INTRODUCTION – CONT.

Pointers are used for memory management and
  achieving polymorphism.

C++  adds the concept of
CONSTANT POINTER
 & POINTER TO A CONSTANT . :=
INTRODUCTION - CONT.



1. Constant Pointer      ::

Declaration   =

              data type * const pointer
INTRODUCTION - CONT.
2.   Pointer to a Constant ::

              data type const * pointer



3.   const data type * const pointer
POINTERS       TO   OBJECTS -

Pointers can point to an object created by
 class .

   Declaration : classname object;
                  classname * pointer;

  Definition    : pointer = & object;
POINTERS TO OBJECTS - CONT.

Object pointers are useful in creating
 objects at run time.



We can also use an object pointer to access
 the public members & member function of
 an object , by using „->‟ operator and the
 object pointer .
POINTERS       TO   OBJECTS –CONT.

E.g.
       pointer -> getdata( );

We can also use

       ( * pointer ) . function( );
THIS POINTER

C++ uses keyword „ this ‟ to represent an
 object that invokes a member function.

E.g. The function call A. max( ) will set the
 pointer this to the address of the object A.

E.g. To access private variables inside a
 member function
           a=123;                       or
           this -> a = 123;
THIS POINTER - APPLICATIONS
In operator overloading using member
 function we use implicitly 'this‟ pointer.



The another important application of the
 pointer 'this' is to return the object it points
 to .
POINTERS TO DERIVED CLASS
Pointers to objects of a base class are type
 compatible with pointers to objects of a
 derived class.

A single pointer variable can be made to
 point to objects belonging to different
 classes.
POINTERS TO DERIVED CLASS – CONT.

e.g.
        B *cptr;
        B b;
        D d;
        cptr = & b;
we can also make cptr to point to the object d
 as follows:
    cptr = & d
POINTERS TO DERIVED CLASS – CONT.

  Base Class
                          Derived Class
Public:
a,b
                        Public / Private /
Private /               Protected :
Protected:
c,d                     e,f,g,h

 If cptr = & d;

        cptr      a,b
POINTERS TO DERIVED CLASS – CONT.


This shows that , although a base pointer
  can be made to point to any number of
  derived objects, it can not directly access
  the members defined by a derived class.


To access the members defined by a
  derived class , cast base pointer to the
  derived class type.
POINTERS TO DERIVED CLASS – CONT.


 E.g   . Casting

        dptr -> show ( ) ;

        ( ( DC * ) bptr ) -> show ( ) ;
VIRTUAL FUNCTIONS
VIRTUAL FUNCTION

The application of polymorphism is the
 ability to refer the objects without any regard
 to their classes.



This necessitates the use of a single
 pointer variable to refer to the objects of
 different classes.
VIRTUAL FUNCTION – CONT.
By making the function 'virtual' in base
 class C++ determines which function to use
 at run time based on the type of object
 pointed to by the base pointer , rather than
 the type of the pointer.

Runtime polymorphism is achieved only
 when a virtual function is accessed through
 a pointer to the base class.
VIRTUAL FUNCTIONS - RULES

1. The virtual functions must be members of
     some class.
2.   They cannot be static members.
3.   They are accessed by using object
     pointers.
4.   A virtual function can be friend of other
     function.
5.   A virtual function in a base class must be
     defined , even though it may not be used.
RULES –CONT.
6. We cannot have a virtual constructors, but
  we can have virtual destructors.



7. While a base pointer can point to any type
  of derived object, the reverse is not true.
RULES –CONT.
8. The prototypes of the base class version
   of a virtual function and all the derived
   class versions must be identical.



9. When a base pointer points to a derived
   class , incrementing or decrementing it will
   not make it to point to the next object of
   the derived class.
RULES –CONT.



10.If a virtual function is define in the base
   class ,it need not be necessarily redefined
   in the derived class.
POLYMORPHISM
POLYMORPHISM

 Polymorphism is crucial feature of Object
   Oriented Programming.



 Polymorphism simply means one name
   having multiple forms.
POLYMORPHISM - CONT.

 “Polymorphism   is the genie in OOP who
 takes instruction from clients and properly
 interprets their wishes.”

–   Ira Pohl, “Object Oriented Programming
 using C++”.
POLYMORPHISM – CONT.

Definition:

     Polymorphism is the ability to create a
 variable, a function or an object that has
 more than one form.
EXAMPLE:

For example:

 The + (plus) operator in C++:
    4+5         <-- Integer addition
    3.14 + 2.0 <-- Floating point addition
    s1 + "bar" <-- String concatenation!
TYPES OF POLYMORPHISM



     Compile-time     Run-time
     Polymorphism   Polymorphism
TYPES OF POLYMORPHISM
 In
   compile time polymorphism, compiler is
 able to select the appropriate function a
 particular call at the compile time.

 In
   run time polymorphism, an appropriate
 member function is selected while the
 program is running.
BENEFITS OF POLYMORPHISM

 Simplicity:
This makes your code easier for you to write
 and easier for others to understand.

 Extensibility:

Polymorphism design and implements system
 that are more extensible.
REFERENCES :

Let us C++
             – by Yeshwant Kanetkar.

Object Oriented Programming with C++
             –by E . BALAGURUSAMY.

Internet.
THANK YOU !!

More Related Content

PPTX
Pointer in c
PPTX
Functions in C
PPTX
Functions in c++
PDF
Set methods in python
PPTX
Union in C programming
PPT
Operator Overloading
PDF
Function overloading ppt
PDF
Operators in python
Pointer in c
Functions in C
Functions in c++
Set methods in python
Union in C programming
Operator Overloading
Function overloading ppt
Operators in python

What's hot (20)

PPTX
Nested loops
PPTX
07. Virtual Functions
PPTX
Java swing
PPTX
STACKS IN DATASTRUCTURE
PPT
structure and union
PPTX
Functions in c
PDF
C++ OOPS Concept
PPT
Stacks
PPTX
classes and objects in C++
PPT
Pointers C programming
PDF
Control statements
PDF
Introduction to Python
PPTX
PPTX
Inheritance in java
PPTX
Regular expressions in Python
PPTX
INLINE FUNCTION IN C++
PPTX
Pointers in C Programming
PPTX
Strings in Python
PPT
Linked list
PPTX
Call by value or call by reference in C++
Nested loops
07. Virtual Functions
Java swing
STACKS IN DATASTRUCTURE
structure and union
Functions in c
C++ OOPS Concept
Stacks
classes and objects in C++
Pointers C programming
Control statements
Introduction to Python
Inheritance in java
Regular expressions in Python
INLINE FUNCTION IN C++
Pointers in C Programming
Strings in Python
Linked list
Call by value or call by reference in C++
Ad

Viewers also liked (9)

PDF
08 subprograms
PPTX
9 subprograms
PPTX
Object Oriented Programming - Abstraction & Encapsulation
PPT
Java multi threading
PPTX
PPT
Presentation on generation of languages
PPTX
Unit1 principle of programming language
PPTX
Pointers, virtual function and polymorphism
PPT
Unit 3 principles of programming language
08 subprograms
9 subprograms
Object Oriented Programming - Abstraction & Encapsulation
Java multi threading
Presentation on generation of languages
Unit1 principle of programming language
Pointers, virtual function and polymorphism
Unit 3 principles of programming language
Ad

Similar to pointers,virtual functions and polymorphism (20)

PDF
Polymorphism
PPTX
Pointers,virtual functions and polymorphism cpp
PPTX
pointer, virtual function and polymorphism
PDF
polymorphism in c++ with Full Explanation.
PPTX
OOPS & C++(UNIT 4)
PPTX
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
PPTX
Pointer and polymorphism
PPTX
Presentations_PPT_ Unit-6_OOP.pptx
PPTX
Object oriented programming slides for presentation
PDF
Object Oriented Programming using C++ - Part 4
PDF
Object Oriented Programming (OOP) using C++ - Lecture 4
PPTX
Pointers in c++
PDF
CS225_Prelecture_Notes 2nd
PPTX
C++ Object Oriented Programming
PPT
Polymorphism
PPT
Polymorphism in C++ for beginners reference
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
PPTX
Polymorphism
PPT
C++ Programming Course
PPT
Ccourse 140618093931-phpapp02
Polymorphism
Pointers,virtual functions and polymorphism cpp
pointer, virtual function and polymorphism
polymorphism in c++ with Full Explanation.
OOPS & C++(UNIT 4)
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
Pointer and polymorphism
Presentations_PPT_ Unit-6_OOP.pptx
Object oriented programming slides for presentation
Object Oriented Programming using C++ - Part 4
Object Oriented Programming (OOP) using C++ - Lecture 4
Pointers in c++
CS225_Prelecture_Notes 2nd
C++ Object Oriented Programming
Polymorphism
Polymorphism in C++ for beginners reference
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
Polymorphism
C++ Programming Course
Ccourse 140618093931-phpapp02

Recently uploaded (20)

PPTX
PPT on SardarPatel and Popular Media.pptx
PDF
424926802-1987-Constitution-as-Basis-of-Environmental-Laws.pdf
PPTX
Precised New Precis and Composition 2025.pptx
PDF
Regional Media Representation of Kuki-Meitei Conflict - An Analysis of Peace ...
PPTX
opher bryers alert -How Opher Bryer’s Impro.ai Became the Center of Israel’s ...
PPTX
OBG. ABNORAMLTIES OF THE PUERPERIUM, BSC
PDF
Theories of federalism showcasing india .pdf
PDF
The Blogs_ Hamas’s Deflection Playbook _ Andy Blumenthal _ The Times of Israe...
PDF
KAL 007 Manual: The Russian Shootdoown of Civilian Plane on 09/01/1983
PDF
Mathura Sridharan's Appointment as Ohio Solicitor General Sparks Racist Backl...
PDF
05082025_First India Newspaper Jaipur.pdf
PDF
Conflict, Narrative and Media -An Analysis of News on Israel-Palestine Confli...
PPTX
The-Evolution-of-Public-Human-Resource-Management (1).pptx
DOCX
Breaking Now – Latest Live News Updates from GTV News HD
PPTX
ASEANOPOL: The Multinational Police Force
PDF
Executive an important link between the legislative and people
PDF
Role of federalism in the indian society
PDF
The Most Dynamic Lawyer to Watch 2025.pdf
PPTX
Indian ancient knowledge system, ancient geopolitics
PDF
2025-07-24_CED-HWB_WIPP_ACO000000001.pdf
PPT on SardarPatel and Popular Media.pptx
424926802-1987-Constitution-as-Basis-of-Environmental-Laws.pdf
Precised New Precis and Composition 2025.pptx
Regional Media Representation of Kuki-Meitei Conflict - An Analysis of Peace ...
opher bryers alert -How Opher Bryer’s Impro.ai Became the Center of Israel’s ...
OBG. ABNORAMLTIES OF THE PUERPERIUM, BSC
Theories of federalism showcasing india .pdf
The Blogs_ Hamas’s Deflection Playbook _ Andy Blumenthal _ The Times of Israe...
KAL 007 Manual: The Russian Shootdoown of Civilian Plane on 09/01/1983
Mathura Sridharan's Appointment as Ohio Solicitor General Sparks Racist Backl...
05082025_First India Newspaper Jaipur.pdf
Conflict, Narrative and Media -An Analysis of News on Israel-Palestine Confli...
The-Evolution-of-Public-Human-Resource-Management (1).pptx
Breaking Now – Latest Live News Updates from GTV News HD
ASEANOPOL: The Multinational Police Force
Executive an important link between the legislative and people
Role of federalism in the indian society
The Most Dynamic Lawyer to Watch 2025.pdf
Indian ancient knowledge system, ancient geopolitics
2025-07-24_CED-HWB_WIPP_ACO000000001.pdf

pointers,virtual functions and polymorphism

  • 1. Dr. BABASAHEB AMBEDKAR TECHNOLOGICAL UNIVERSITY Presentation on Pointers , Virtual Functions and Polymorphism. By , Ruturaj Nalawade Sanjay Bidkar Swapnil Sarwade Under the Guidance of , Mrs . Ladda
  • 3. INTRODUCTION Pointers are the variables which holds the addresses of other variables. Pointer variables are denoted by „ * ptr ‟ Pointers are the derived data types.
  • 4. INTRODUCTION – CONT. E.g. := { int i , *j; i= 3; j = &i; cout<<“The value of i is t”<<i<<endl; cout<<“The value of *j is t”<<*j; } Output : : The value of i is 3 The value of *j is 3
  • 5. INTRODUCTION – CONT. E.g. := { int i , *j; i= 3; j = &i; cout<<“The value of i is t”<<i<<endl; cout<<“The value of *j is t”<<*j; * j } Output : : The value of i is 3 The value of *j is 3
  • 6. Introduction - Cont.  How the *j gets the value of i ? int i , *j ; Variable i j Names Value Memory 65524 65522 Address
  • 7. Introduction - Cont.  How the *j gets the value of i ? int i , *j ; i=3; Variable i j Names Value 3 Memory 65524 65522 Address
  • 8. Introduction - Cont.  How the *j gets the value of i ? int i , *j ; i=3; j=&i; Variable i j Names Value 3 65524 Memory 65524 65522 Address *j refers to the value at address j.
  • 9. INTRODUCTION – CONT. Pointers are used for memory management and achieving polymorphism. C++ adds the concept of CONSTANT POINTER & POINTER TO A CONSTANT . :=
  • 10. INTRODUCTION - CONT. 1. Constant Pointer :: Declaration = data type * const pointer
  • 11. INTRODUCTION - CONT. 2. Pointer to a Constant :: data type const * pointer 3. const data type * const pointer
  • 12. POINTERS TO OBJECTS - Pointers can point to an object created by class . Declaration : classname object; classname * pointer; Definition : pointer = & object;
  • 13. POINTERS TO OBJECTS - CONT. Object pointers are useful in creating objects at run time. We can also use an object pointer to access the public members & member function of an object , by using „->‟ operator and the object pointer .
  • 14. POINTERS TO OBJECTS –CONT. E.g. pointer -> getdata( ); We can also use ( * pointer ) . function( );
  • 15. THIS POINTER C++ uses keyword „ this ‟ to represent an object that invokes a member function. E.g. The function call A. max( ) will set the pointer this to the address of the object A. E.g. To access private variables inside a member function a=123; or this -> a = 123;
  • 16. THIS POINTER - APPLICATIONS In operator overloading using member function we use implicitly 'this‟ pointer. The another important application of the pointer 'this' is to return the object it points to .
  • 17. POINTERS TO DERIVED CLASS Pointers to objects of a base class are type compatible with pointers to objects of a derived class. A single pointer variable can be made to point to objects belonging to different classes.
  • 18. POINTERS TO DERIVED CLASS – CONT. e.g. B *cptr; B b; D d; cptr = & b; we can also make cptr to point to the object d as follows: cptr = & d
  • 19. POINTERS TO DERIVED CLASS – CONT. Base Class Derived Class Public: a,b Public / Private / Private / Protected : Protected: c,d e,f,g,h If cptr = & d; cptr a,b
  • 20. POINTERS TO DERIVED CLASS – CONT. This shows that , although a base pointer can be made to point to any number of derived objects, it can not directly access the members defined by a derived class. To access the members defined by a derived class , cast base pointer to the derived class type.
  • 21. POINTERS TO DERIVED CLASS – CONT.  E.g . Casting dptr -> show ( ) ; ( ( DC * ) bptr ) -> show ( ) ;
  • 23. VIRTUAL FUNCTION The application of polymorphism is the ability to refer the objects without any regard to their classes. This necessitates the use of a single pointer variable to refer to the objects of different classes.
  • 24. VIRTUAL FUNCTION – CONT. By making the function 'virtual' in base class C++ determines which function to use at run time based on the type of object pointed to by the base pointer , rather than the type of the pointer. Runtime polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.
  • 25. VIRTUAL FUNCTIONS - RULES 1. The virtual functions must be members of some class. 2. They cannot be static members. 3. They are accessed by using object pointers. 4. A virtual function can be friend of other function. 5. A virtual function in a base class must be defined , even though it may not be used.
  • 26. RULES –CONT. 6. We cannot have a virtual constructors, but we can have virtual destructors. 7. While a base pointer can point to any type of derived object, the reverse is not true.
  • 27. RULES –CONT. 8. The prototypes of the base class version of a virtual function and all the derived class versions must be identical. 9. When a base pointer points to a derived class , incrementing or decrementing it will not make it to point to the next object of the derived class.
  • 28. RULES –CONT. 10.If a virtual function is define in the base class ,it need not be necessarily redefined in the derived class.
  • 30. POLYMORPHISM  Polymorphism is crucial feature of Object Oriented Programming.  Polymorphism simply means one name having multiple forms.
  • 31. POLYMORPHISM - CONT.  “Polymorphism is the genie in OOP who takes instruction from clients and properly interprets their wishes.” – Ira Pohl, “Object Oriented Programming using C++”.
  • 32. POLYMORPHISM – CONT. Definition: Polymorphism is the ability to create a variable, a function or an object that has more than one form.
  • 33. EXAMPLE: For example: The + (plus) operator in C++: 4+5 <-- Integer addition 3.14 + 2.0 <-- Floating point addition s1 + "bar" <-- String concatenation!
  • 34. TYPES OF POLYMORPHISM Compile-time Run-time Polymorphism Polymorphism
  • 35. TYPES OF POLYMORPHISM  In compile time polymorphism, compiler is able to select the appropriate function a particular call at the compile time.  In run time polymorphism, an appropriate member function is selected while the program is running.
  • 36. BENEFITS OF POLYMORPHISM  Simplicity: This makes your code easier for you to write and easier for others to understand.  Extensibility: Polymorphism design and implements system that are more extensible.
  • 37. REFERENCES : Let us C++ – by Yeshwant Kanetkar. Object Oriented Programming with C++ –by E . BALAGURUSAMY. Internet.