SlideShare a Scribd company logo
1 of 20
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

C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP ImplementationFridz Felisco
 
Object Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part IObject Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part IAjit Nayak
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloadingHaresh Jaiswal
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
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
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++Ankur Pandey
 
Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stalMichael Stal
 
C++ questions and answers
C++ questions and answersC++ questions and answers
C++ questions and answersDeepak Singh
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpMichael Stal
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1ReKruiTIn.com
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 

What's hot (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP Implementation
 
03. oop concepts
03. oop concepts03. oop concepts
03. oop concepts
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
Object Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part IObject Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part I
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
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
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
 
Oop2011 actor presentation_stal
Oop2011 actor presentation_stalOop2011 actor presentation_stal
Oop2011 actor presentation_stal
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
C++ questions and answers
C++ questions and answersC++ questions and answers
C++ questions and answers
 
C++ interview question
C++ interview questionC++ interview question
C++ interview question
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Overloading
OverloadingOverloading
Overloading
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 

Similar to Pointer and Object in C++

Virtual function
Virtual functionVirtual function
Virtual functionsdrhr
 
OpenCon2014 - Sumatra as an Open Science tool
OpenCon2014 - Sumatra as an Open Science toolOpenCon2014 - Sumatra as an Open Science tool
OpenCon2014 - Sumatra as an Open Science toolFelix Z. Hoffmann
 
Salesforce meetup | Lightning Web Component
Salesforce meetup | Lightning Web ComponentSalesforce meetup | Lightning Web Component
Salesforce meetup | Lightning Web ComponentAccenture Hungary
 
Serverless survival kit
Serverless survival kitServerless survival kit
Serverless survival kitSteve Houël
 
Beware of Pointers
Beware of PointersBeware of Pointers
Beware of Pointersppd1961
 
High quality ap is with api platform
High quality ap is with api platformHigh quality ap is with api platform
High quality ap is with api platformNelson Kopliku
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET FrameworkKamlesh Makvana
 
Build and Deploy Provider-hosted SharePoint Add-ins
Build and Deploy Provider-hosted SharePoint Add-insBuild and Deploy Provider-hosted SharePoint Add-ins
Build and Deploy Provider-hosted SharePoint Add-insDanny Jessee
 
C Types - Extending Python
C Types - Extending PythonC Types - Extending Python
C Types - Extending PythonPriyank Kapadia
 
Teaching and Learning in Virtual Worlds
Teaching and Learning in Virtual WorldsTeaching and Learning in Virtual Worlds
Teaching and Learning in Virtual WorldsRinda Montgomery
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]Rome468
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented ProgrammingGamindu Udayanga
 
Venice boats classification
Venice boats classificationVenice boats classification
Venice boats classificationRoberto Falconi
 
PythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programmingPythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programmingKhadijaKhadijaAouadi
 

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

C++ Training
C++ TrainingC++ Training
C++ Training
 
Java basics
Java basicsJava basics
Java basics
 
Virtual function
Virtual functionVirtual function
Virtual function
 
OpenCon2014 - Sumatra as an Open Science tool
OpenCon2014 - Sumatra as an Open Science toolOpenCon2014 - Sumatra as an Open Science tool
OpenCon2014 - Sumatra as an Open Science tool
 
Salesforce meetup | Lightning Web Component
Salesforce meetup | Lightning Web ComponentSalesforce meetup | Lightning Web Component
Salesforce meetup | Lightning Web Component
 
Serverless survival kit
Serverless survival kitServerless survival kit
Serverless survival kit
 
C++ Course module
C++ Course moduleC++ Course module
C++ Course module
 
Beware of Pointers
Beware of PointersBeware of Pointers
Beware of Pointers
 
High quality ap is with api platform
High quality ap is with api platformHigh quality ap is with api platform
High quality ap is with api platform
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
 
Build and Deploy Provider-hosted SharePoint Add-ins
Build and Deploy Provider-hosted SharePoint Add-insBuild and Deploy Provider-hosted SharePoint Add-ins
Build and Deploy Provider-hosted SharePoint Add-ins
 
C Types - Extending Python
C Types - Extending PythonC Types - Extending Python
C Types - Extending Python
 
Teaching and Learning in Virtual Worlds
Teaching and Learning in Virtual WorldsTeaching and Learning in Virtual Worlds
Teaching and Learning in Virtual Worlds
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
Venice boats classification
Venice boats classificationVenice boats classification
Venice boats classification
 
PythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programmingPythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programming
 
Malware analysis
Malware analysisMalware analysis
Malware analysis
 
An approach for knowledge-driven product, process and resource mappings for a...
An approach for knowledge-driven product, process and resource mappings for a...An approach for knowledge-driven product, process and resource mappings for a...
An approach for knowledge-driven product, process and resource mappings for a...
 
Bhushan Rathi
Bhushan RathiBhushan Rathi
Bhushan Rathi
 

Recently uploaded

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Recently uploaded (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

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