SlideShare a Scribd company logo
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 1
Department of Information Technology
POINTER AND
OBJECT IN C++
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 2
Department of Information Technology
Pointer to object
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 3
Department of Information Technology
How to store address of object into pointer
and access members?
 Ans
 Pointer to object
 Two Ways to
access member
 (*ptr).member
 Ptr->member
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 4
Department of Information Technology
Pointer to object
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 5
Department of Information Technology
How to store address of object created using new into pointer
and access members?
 new Base(10)
allocate memory
at memory heap
that is to be
pointed by p1
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 6
Department of Information Technology
How to create pointer to array of object?
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 7
Department of Information Technology
How to call constructor of array of object
through new?
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 8
Department of Information Technology
What is this pointer?
 C++ use unique key word called this to represent
object that invokes member function.
 i.e. Compiler store address of invoking object in this!!!!
 Example: obj1.max(obj2)
 Address of obj1 will get stored into this pointer.
 Use of this pointer
 To access data member of invoking object within class
 To return invoking object!!!
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 9
Department of Information Technology
To access data member of invoking
object within class
30
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 10
Department of Information Technology
To return invoking object!!
30
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 11
Department of Information Technology
Pointer to derived class
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 12
Department of Information Technology
Pointer to derived class
Note: Base class pointer could point to DerivedNote: Base class pointer could point to Derived
Class object but derived class pointer can notClass object but derived class pointer can not
Point to base class pointer!!!Point to base class pointer!!!
Note: Base class pointer could point to DerivedNote: Base class pointer could point to Derived
Class object but derived class pointer can notClass object but derived class pointer can not
Point to base class pointer!!!Point to base class pointer!!!
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 13
Department of Information Technology
Pointer to derived class
Compiler generates error Because derived classCompiler generates error Because derived class
public members can not be Accessed throughpublic members can not be Accessed through
base class Pointer!!!base class Pointer!!!
Reason: Base class pointer can only points toReason: Base class pointer can only points to
Base class object within derived class.Base class object within derived class.
Compiler generates error Because derived classCompiler generates error Because derived class
public members can not be Accessed throughpublic members can not be Accessed through
base class Pointer!!!base class Pointer!!!
Reason: Base class pointer can only points toReason: Base class pointer can only points to
Base class object within derived class.Base class object within derived class.
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 14
Department of Information Technology
Pointer to derived class
Compiler generates error Because derived classCompiler generates error Because derived class
public members can not be Accessed throughpublic members can not be Accessed through
base class Pointer!!!base class Pointer!!!
Reason: Base class pointer can only points toReason: Base class pointer can only points to
Base class object within derived class.Base class object within derived class.
Compiler generates error Because derived classCompiler generates error Because derived class
public members can not be Accessed throughpublic members can not be Accessed through
base class Pointer!!!base class Pointer!!!
Reason: Base class pointer can only points toReason: Base class pointer can only points to
Base class object within derived class.Base class object within derived class.
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 15
Department of Information Technology
Method Overriding
 Method Overgrind: If same signature of a functionMethod Overgrind: If same signature of a function
in base class as well derived class.in base class as well derived class.
 Binding of Fun. Depend on type of Invoking objectBinding of Fun. Depend on type of Invoking object
 Base class-> base class member functionBase class-> base class member function
 Derived class-> Derived class member funDerived class-> Derived class member fun
 Compile time polymorphismCompile time polymorphism
 Method Overgrind: If same signature of a functionMethod Overgrind: If same signature of a function
in base class as well derived class.in base class as well derived class.
 Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.
 Base class-> base class member functionBase class-> base class member function
 Derived class-> Derived class member funDerived class-> Derived class member fun
 Compile time polymorphismCompile time polymorphism
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 16
Department of Information Technology
Method Overriding
 Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.
 p->print() call base type print function as type ofp->print() call base type print function as type of
p is Base.p is Base.
 Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.
 p->print() call base type print function as type ofp->print() call base type print function as type of
p is Base.p is Base.
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 17
Department of Information Technology
How to call derived class method using
base class pointer??
 Solution
 Run time polymorphism
 What it mean?
 Binding will done at run time Instead of compile time.
 What happen in Runtime binding
 Based on content of the object compiler will bind the
function definition with specific functional call!!!
 How to instruct to the compiler to do binding at
runtime instead of compile time
 By making base class function virtual. Generally referred as
virtual Function!!!!
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 18
Department of Information Technology
Method Overriding
 When compiler find virtual keyword in baseWhen compiler find virtual keyword in base
class member function, Compiler perform runtimeclass member function, Compiler perform runtime
binding instead of compile time binding.binding instead of compile time binding.
Content of p is address of derived class so it callsContent of p is address of derived class so it calls
print() method of derived class!!!!print() method of derived class!!!!
 When compiler find virtual keyword in baseWhen compiler find virtual keyword in base
class member function, Compiler perform runtimeclass member function, Compiler perform runtime
binding instead of compile time binding.binding instead of compile time binding.
Content of p is address of derived class so it callsontent of p is address of derived class so it calls
rint() method of derived class!!!!rint() method of derived class!!!!
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 19
Department of Information Technology
Definition: Virtual Function
 A virtual function is a member function which is
declared within base class and is re-
defined (Overridden) by derived class. When you refer
to a derived class object using a pointer or a reference
to the base class, you can call a virtual function for
that object and execute the derived class's version of
the function.
Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 20
Department of Information Technology

More Related Content

What's hot (20)

PPT
Polymorphism
Nilesh Dalvi
 
PPT
C++ OOP Implementation
Fridz Felisco
 
PDF
03. oop concepts
Haresh Jaiswal
 
PPT
Operator overloading
Northeastern University
 
PDF
Object Oriented Programming using C++ Part I
Ajit Nayak
 
PPT
C++ Interview Questions
Kaushik Raghupathi
 
PDF
06. operator overloading
Haresh Jaiswal
 
PPTX
Presentation 1st
Connex
 
PPT
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 
PDF
Top C Language Interview Questions and Answer
Vineet Kumar Saini
 
PDF
Object oriented programming c++
Ankur Pandey
 
PPTX
Oop2011 actor presentation_stal
Michael Stal
 
PPSX
SRAVANByCPP
aptechsravan
 
PPT
C++ questions and answers
Deepak Singh
 
PDF
C++ interview question
Durgesh Tripathi
 
PPT
Operator overloading
piyush Kumar Sharma
 
PPT
Overloading
poonamchopra7975
 
PPTX
Qcon2011 functions rockpresentation_f_sharp
Michael Stal
 
PPT
C, C++ Interview Questions Part - 1
ReKruiTIn.com
 
PPT
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Polymorphism
Nilesh Dalvi
 
C++ OOP Implementation
Fridz Felisco
 
03. oop concepts
Haresh Jaiswal
 
Operator overloading
Northeastern University
 
Object Oriented Programming using C++ Part I
Ajit Nayak
 
C++ Interview Questions
Kaushik Raghupathi
 
06. operator overloading
Haresh Jaiswal
 
Presentation 1st
Connex
 
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 
Top C Language Interview Questions and Answer
Vineet Kumar Saini
 
Object oriented programming c++
Ankur Pandey
 
Oop2011 actor presentation_stal
Michael Stal
 
SRAVANByCPP
aptechsravan
 
C++ questions and answers
Deepak Singh
 
C++ interview question
Durgesh Tripathi
 
Operator overloading
piyush Kumar Sharma
 
Overloading
poonamchopra7975
 
Qcon2011 functions rockpresentation_f_sharp
Michael Stal
 
C, C++ Interview Questions Part - 1
ReKruiTIn.com
 
08 c++ Operator Overloading.ppt
Tareq Hasan
 

Similar to Pointer and Object in C++ (20)

PPTX
7. Pointers and Virtual functions final -3.pptx
Abhishekkumarsingh630054
 
PPTX
Inheritance, Polymorphism, and Virtual Functions (Ch_15).pptx
nasiruni6428
 
PPTX
OOPS & C++(UNIT 4)
Dr. SURBHI SAROHA
 
PPTX
6. Virtual base class.pptx and virtual function
archana22486y
 
PPTX
Inheritance
sourav verma
 
PPT
VIRTUAL FUNCTIONS c++ -POLYMORPHISM.ppt
blindlove32538210
 
PPTX
Intro To C++ - Class #22: Inheritance, Part 1
Blue Elephant Consulting
 
PDF
polymorpisum-140106223024-phpapp01.pdf
BapanKar2
 
PPTX
Inheritance, Polymorphism, and Virtual Functions.pptx
MrNikhilMohanShinde
 
PPTX
Virtual function
zindadili
 
PPTX
Object oriented programming slides for presentation
abdullahkhann3534
 
PDF
Chapter 6 and inheritance OOP C++ tu ioe
EZERR1
 
PPT
Polymorphism in C++ for beginners reference
21pd23
 
PPT
Virtual Function
Lovely Professional University
 
PDF
Object Oriented Programming (OOP) using C++ - Lecture 4
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
PPTX
OOPS Basics With Example
Thooyavan Venkatachalam
 
PPT
OOP
karan saini
 
PPT
Lecturespecial
karan saini
 
PDF
2 BytesC++ course_2014_c11_ inheritance
kinan keshkeh
 
7. Pointers and Virtual functions final -3.pptx
Abhishekkumarsingh630054
 
Inheritance, Polymorphism, and Virtual Functions (Ch_15).pptx
nasiruni6428
 
OOPS & C++(UNIT 4)
Dr. SURBHI SAROHA
 
6. Virtual base class.pptx and virtual function
archana22486y
 
Inheritance
sourav verma
 
VIRTUAL FUNCTIONS c++ -POLYMORPHISM.ppt
blindlove32538210
 
Intro To C++ - Class #22: Inheritance, Part 1
Blue Elephant Consulting
 
polymorpisum-140106223024-phpapp01.pdf
BapanKar2
 
Inheritance, Polymorphism, and Virtual Functions.pptx
MrNikhilMohanShinde
 
Virtual function
zindadili
 
Object oriented programming slides for presentation
abdullahkhann3534
 
Chapter 6 and inheritance OOP C++ tu ioe
EZERR1
 
Polymorphism in C++ for beginners reference
21pd23
 
Object Oriented Programming (OOP) using C++ - Lecture 4
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
OOPS Basics With Example
Thooyavan Venkatachalam
 
Lecturespecial
karan saini
 
2 BytesC++ course_2014_c11_ inheritance
kinan keshkeh
 
Ad

Recently uploaded (20)

PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
July Patch Tuesday
Ivanti
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
Ad

Pointer and Object in C++

  • 1. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 1 Department of Information Technology POINTER AND OBJECT IN C++
  • 2. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 2 Department of Information Technology Pointer to object
  • 3. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 3 Department of Information Technology How to store address of object into pointer and access members?  Ans  Pointer to object  Two Ways to access member  (*ptr).member  Ptr->member
  • 4. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 4 Department of Information Technology Pointer to object
  • 5. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 5 Department of Information Technology How to store address of object created using new into pointer and access members?  new Base(10) allocate memory at memory heap that is to be pointed by p1
  • 6. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 6 Department of Information Technology How to create pointer to array of object?
  • 7. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 7 Department of Information Technology How to call constructor of array of object through new?
  • 8. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 8 Department of Information Technology What is this pointer?  C++ use unique key word called this to represent object that invokes member function.  i.e. Compiler store address of invoking object in this!!!!  Example: obj1.max(obj2)  Address of obj1 will get stored into this pointer.  Use of this pointer  To access data member of invoking object within class  To return invoking object!!!
  • 9. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 9 Department of Information Technology To access data member of invoking object within class 30
  • 10. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 10 Department of Information Technology To return invoking object!! 30
  • 11. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 11 Department of Information Technology Pointer to derived class
  • 12. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 12 Department of Information Technology Pointer to derived class Note: Base class pointer could point to DerivedNote: Base class pointer could point to Derived Class object but derived class pointer can notClass object but derived class pointer can not Point to base class pointer!!!Point to base class pointer!!! Note: Base class pointer could point to DerivedNote: Base class pointer could point to Derived Class object but derived class pointer can notClass object but derived class pointer can not Point to base class pointer!!!Point to base class pointer!!!
  • 13. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 13 Department of Information Technology Pointer to derived class Compiler generates error Because derived classCompiler generates error Because derived class public members can not be Accessed throughpublic members can not be Accessed through base class Pointer!!!base class Pointer!!! Reason: Base class pointer can only points toReason: Base class pointer can only points to Base class object within derived class.Base class object within derived class. Compiler generates error Because derived classCompiler generates error Because derived class public members can not be Accessed throughpublic members can not be Accessed through base class Pointer!!!base class Pointer!!! Reason: Base class pointer can only points toReason: Base class pointer can only points to Base class object within derived class.Base class object within derived class.
  • 14. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 14 Department of Information Technology Pointer to derived class Compiler generates error Because derived classCompiler generates error Because derived class public members can not be Accessed throughpublic members can not be Accessed through base class Pointer!!!base class Pointer!!! Reason: Base class pointer can only points toReason: Base class pointer can only points to Base class object within derived class.Base class object within derived class. Compiler generates error Because derived classCompiler generates error Because derived class public members can not be Accessed throughpublic members can not be Accessed through base class Pointer!!!base class Pointer!!! Reason: Base class pointer can only points toReason: Base class pointer can only points to Base class object within derived class.Base class object within derived class.
  • 15. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 15 Department of Information Technology Method Overriding  Method Overgrind: If same signature of a functionMethod Overgrind: If same signature of a function in base class as well derived class.in base class as well derived class.  Binding of Fun. Depend on type of Invoking objectBinding of Fun. Depend on type of Invoking object  Base class-> base class member functionBase class-> base class member function  Derived class-> Derived class member funDerived class-> Derived class member fun  Compile time polymorphismCompile time polymorphism  Method Overgrind: If same signature of a functionMethod Overgrind: If same signature of a function in base class as well derived class.in base class as well derived class.  Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.  Base class-> base class member functionBase class-> base class member function  Derived class-> Derived class member funDerived class-> Derived class member fun  Compile time polymorphismCompile time polymorphism
  • 16. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 16 Department of Information Technology Method Overriding  Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.  p->print() call base type print function as type ofp->print() call base type print function as type of p is Base.p is Base.  Binding of Fun. Depend on type of Invoking object.Binding of Fun. Depend on type of Invoking object.  p->print() call base type print function as type ofp->print() call base type print function as type of p is Base.p is Base.
  • 17. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 17 Department of Information Technology How to call derived class method using base class pointer??  Solution  Run time polymorphism  What it mean?  Binding will done at run time Instead of compile time.  What happen in Runtime binding  Based on content of the object compiler will bind the function definition with specific functional call!!!  How to instruct to the compiler to do binding at runtime instead of compile time  By making base class function virtual. Generally referred as virtual Function!!!!
  • 18. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 18 Department of Information Technology Method Overriding  When compiler find virtual keyword in baseWhen compiler find virtual keyword in base class member function, Compiler perform runtimeclass member function, Compiler perform runtime binding instead of compile time binding.binding instead of compile time binding. Content of p is address of derived class so it callsContent of p is address of derived class so it calls print() method of derived class!!!!print() method of derived class!!!!  When compiler find virtual keyword in baseWhen compiler find virtual keyword in base class member function, Compiler perform runtimeclass member function, Compiler perform runtime binding instead of compile time binding.binding instead of compile time binding. Content of p is address of derived class so it callsontent of p is address of derived class so it calls rint() method of derived class!!!!rint() method of derived class!!!!
  • 19. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 19 Department of Information Technology Definition: Virtual Function  A virtual function is a member function which is declared within base class and is re- defined (Overridden) by derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.
  • 20. Classified e-Material ©Copyrights Charotar Institute of Technology-CHARUSAT, Changa 20 Department of Information Technology