SlideShare a Scribd company logo
1 of 38
Download to read offline
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;
}
Output : :
The value of i is 3
The value of *j is 3
* j
Introduction - Cont.
 How the *j gets the value of i ?
int i , *j ;
Variable
Names
i j
Value
Memory
Address
65524 65522
Introduction - Cont.
 How the *j gets the value of i ?
int i , *j ;
i = 3 ;
Variable
Names
i j
Value
Memory
Address
65524 65522
3
Introduction - Cont.
 How the *j gets the value of i ?
int i , *j ;
i = 3 ;
j = & i ;
Variable
Names
i j
Value 3
Memory
Address
65524 65522
65524
*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
2. Pointer to a Constant ::
data type const * pointer
3. const data type * const pointer
INTRODUCTION - CONT.
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
Public:
a , b
Private /
Protected:
c , d
Derived Class
Public / Private /
Protected :
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
Polymorphism
Run-time
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

What's hot

Virtual function complete By Abdul Wahab (moon sheikh)
Virtual function complete By Abdul Wahab (moon sheikh)Virtual function complete By Abdul Wahab (moon sheikh)
Virtual function complete By Abdul Wahab (moon sheikh)MoonSheikh1
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++Rabin BK
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Anil Bapat
 
polymorphism and virtual function
polymorphism and virtual functionpolymorphism and virtual function
polymorphism and virtual functionBhanuprataparya
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++PRINCE KUMAR
 
Object-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism UnleashedObject-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism UnleashedNaresh Chintalcheru
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented ProgrammingGamindu Udayanga
 
Polymorphism
PolymorphismPolymorphism
PolymorphismKumar
 
OOP - Polymorphism
OOP - PolymorphismOOP - Polymorphism
OOP - PolymorphismMudasir Qazi
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerVineet Kumar Saini
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA PolymorphismMahi Mca
 
2nd puc computer science chapter 8 function overloading
 2nd puc computer science chapter 8   function overloading 2nd puc computer science chapter 8   function overloading
2nd puc computer science chapter 8 function overloadingAahwini Esware gowda
 

What's hot (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Virtual function complete By Abdul Wahab (moon sheikh)
Virtual function complete By Abdul Wahab (moon sheikh)Virtual function complete By Abdul Wahab (moon sheikh)
Virtual function complete By Abdul Wahab (moon sheikh)
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
polymorphism and virtual function
polymorphism and virtual functionpolymorphism and virtual function
polymorphism and virtual function
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
 
Object-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism UnleashedObject-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism Unleashed
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism in oop
Polymorphism in oopPolymorphism in oop
Polymorphism in oop
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
OOP - Polymorphism
OOP - PolymorphismOOP - Polymorphism
OOP - Polymorphism
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
2nd puc computer science chapter 8 function overloading
 2nd puc computer science chapter 8   function overloading 2nd puc computer science chapter 8   function overloading
2nd puc computer science chapter 8 function overloading
 

Similar to POINTERS AND POLYMORPHISM

pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismrattaj
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphismramya marichamy
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
C questions
C questionsC questions
C questionsparm112
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptishan743441
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)rashmita_mishra
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cppgourav kottawar
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Abu Saleh
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpprajshreemuthiah
 
C++ Class & object pointer in c++ programming language
C++ Class & object pointer in c++ programming languageC++ Class & object pointer in c++ programming language
C++ Class & object pointer in c++ programming languageHariTharshiniBscIT1
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdfPowerfullBoy1
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesUmesh Nikam
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdfstudy material
 

Similar to POINTERS AND POLYMORPHISM (20)

pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
C questions
C questionsC questions
C questions
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
C++ Class & object pointer in c++ programming language
C++ Class & object pointer in c++ programming languageC++ Class & object pointer in c++ programming language
C++ Class & object pointer in c++ programming language
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdf
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Oop concepts
Oop conceptsOop concepts
Oop concepts
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

POINTERS 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; } Output : : The value of i is 3 The value of *j is 3 * j
  • 6. Introduction - Cont.  How the *j gets the value of i ? int i , *j ; Variable Names i j Value Memory Address 65524 65522
  • 7. Introduction - Cont.  How the *j gets the value of i ? int i , *j ; i = 3 ; Variable Names i j Value Memory Address 65524 65522 3
  • 8. Introduction - Cont.  How the *j gets the value of i ? int i , *j ; i = 3 ; j = & i ; Variable Names i j Value 3 Memory Address 65524 65522 65524 *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. 2. Pointer to a Constant :: data type const * pointer 3. const data type * const pointer INTRODUCTION - CONT.
  • 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 Public: a , b Private / Protected: c , d Derived Class Public / Private / Protected : 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!
  • 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.