SlideShare a Scribd company logo
1 of 65
Object Oriented
Programming with C++
Gamindu Udayanga
Software Engineer
Metatechno Lanka Company (Pvt) Ltd.
Web : www.metalanka.com
Basic Concepts of OOP
 Abstraction
 Polymorphism
 Inheritance
 Encapsulation
Access Specifies in C++
 Public
-Any code can access public
Data Members
Member Functions
Protected
-Any member function of the class
-Member function of the derived class
-Can access protected class
Private
Only member function of the class can access private class members
Friend functions & Classes in C++
 If a function is defined as a friend function then private & protected data of
a class can be accessed using the function
 The compiler knows a given function is a friend function by the use of the
keyword friend
 The declaration of a friend function should be made inside the body of the
class(Anywhere inside class either in private or public section starting with
keyword friend
Friend Class
 Like a friend function , a class can also be a friend of another class. A friend
class can access all protected & private variables of a class.
Which Object Oriented Concept violates
friend method & friend class?
Difference Between Class & Struct in C++
 Members of a class are private by default and members of struct are public
by default
Different kinds of data members in C++
 Static
static member is associated with class instead of an object
Accessing static members within a class methods
same as regular data members
Accessing static data members outside the class
int c = Class_Name::variable;
Static Methods
They do not have this pointer
Can access non static data members private and protected data members
Const
 Const variables cannot be changed(Except mutable & const cast)
 We can declare const methods.const members give a guarantee that method
will not change the members of that class
Const object
 A const object’s values cannot be changed
 Non const object can call both const & non const methods
 Const object can call only const methods
Mutable Modifier
Mutable data member allow user to modify a particular data member
Default Constructor
 Zero argument constructor or constructor added by compiler
 Print Bingo ten times without using loop, recursion or without writing ten
times
Constructor Initializer List C++
 Initializer List is used to initialize data members of a class. The list of
members to be initialized is indicated with constructor as a comma separated
list followed by a colon
When to use initializer list
Copy Constructor
 Default Copy Constructor perform shallow copy .We can write our copy
constructor to perform deep copy(Deep copy & Shallow copy will be discuses
in a future session)
 For More Information refer below link
 Write Bug Free Efficient Code in C++ with Dynamic Memory Allocation
C++ inheritance
 C++ supports 3 types of inheritance
-public
-private
-protected
Public Inheritance
 Protected Inheritance
Private Inheritance
Prevent inheritance in C++
C++ supports both single inheritance &
Multiple inheritance
 Single Inheritance Multiple Inheritance
Method Overriding in C++
 Method overriding add or replace existing functions
 In C++ Methods declared as virtual in the base class can be properly
overridden by Derived class
 We don’t need to repeat virtual keyword in front of the method definition in
base classes
 It is recommended to add the override keyword to the end of the overriding
method(C++ 11)
 By adding final keyword at the end of the methods, we can prevent
overriding(C++ 11)
 A pointer or reference type of a class can refer to an object of that class or
its derived classes
 Here Comes to the C++ polymorphism
 C++ supports different types of polymorphism
1)Ad hoc polymorphism
2)Parametric polymorphism
3)Subtyping
Static polymorphism & Dynamic Polymorphism
 Static polymorphism – Compile Time
 Dynamic Polymorphism –Run time Polymorphism
In C++ Methods declared as virtual in the base class
can be properly overridden by Derived class
Call Base class Method in derived Class
Virtual Functions in C++
 A virtual function a member function which is declared within base class and
is re-defined (Overriden) 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.
Function Pointer
 it’s also possible to declare a non-constant pointer to a function
How virtual Functions resolves
 C++ compiler creates a hidden class member called
virtual-pointer or in short vfptr when there are one or
more virtual functions. This vfptr is a pointer that points
to a table of function pointers. This table is also created
by compiler and called virtual function table or vftable.
Each row of the vftable is a function pointer pointing to a
corresponding virtual function.
Super *sPtr = new Sub();
Virtual Destructor
Making base class destructor virtual guarantees that the
object of derived class is destructed properly
 If derived class allocates dynamic memory ,open files, database connections
if base class destructor is not virtual then only base destructor may call and
oboe resources will not be released;
Pure virtual Functions & Abstract Classes
 A pure virtual function (or abstract function) in C++ is a virtual function for
which we don’t have implementation, we only declare it. A pure virtual
function is declared by assigning 0 in declaration
 When a class has one or more than pure virtual functions. That class becomes
an abstract class & we cannot create an object out of it. But we can create
pointers or reference types in abstract classes
 If we inherit from abstract class we have to add implementation to all pure
virtual functions ,Otherwise it will also become abstract
 C++ does not have interfaces, Only abstract classes
C++ allows multiple inheritance
Diamond Problem
The solution to this problem is ‘virtual’ keyword. We make the classes
‘Faculty’ and ‘Student’ as virtual base classes to avoid two copies of
‘Person’ in ‘TA’ class. For example, consider the following program.
Namespaces in C++
Dynamic Casting
Shallow copy vs Deep Copy
Shallow Copy
int *p = new int;
*p =100;
Int *q = p;
Deep Copy
int *p = new int ;
Int *q = new int ;
*p =*q;
*q = 99;
Add Copy Constructor

More Related Content

What's hot

What's hot (20)

Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Inline function
Inline functionInline function
Inline function
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Method Overloading in Java
Method Overloading in JavaMethod Overloading in Java
Method Overloading in Java
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Modular programming
Modular programmingModular programming
Modular programming
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 

Similar to C++ Object Oriented Programming

Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfismartshanker1
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
Introduction to C++ Programming
Introduction to C++ ProgrammingIntroduction to C++ Programming
Introduction to C++ ProgrammingPreeti Kashyap
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 
C questions
C questionsC questions
C questionsparm112
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceEng Teong Cheah
 
14_inheritance.ppt
14_inheritance.ppt14_inheritance.ppt
14_inheritance.pptsundas65
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++M Hussnain Ali
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismJawad Khan
 

Similar to C++ Object Oriented Programming (20)

Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
 
My c++
My c++My c++
My c++
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
Introduction to C++ Programming
Introduction to C++ ProgrammingIntroduction to C++ Programming
Introduction to C++ Programming
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
C++ Version 2
C++  Version 2C++  Version 2
C++ Version 2
 
C questions
C questionsC questions
C questions
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
14_inheritance.ppt
14_inheritance.ppt14_inheritance.ppt
14_inheritance.ppt
 
Classes2
Classes2Classes2
Classes2
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
C# interview
C# interviewC# interview
C# interview
 
Class and object
Class and objectClass and object
Class and object
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 

Recently uploaded

Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 

Recently uploaded (20)

Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 

C++ Object Oriented Programming

  • 1. Object Oriented Programming with C++ Gamindu Udayanga Software Engineer Metatechno Lanka Company (Pvt) Ltd. Web : www.metalanka.com
  • 2. Basic Concepts of OOP  Abstraction  Polymorphism  Inheritance  Encapsulation
  • 3. Access Specifies in C++  Public -Any code can access public Data Members Member Functions Protected -Any member function of the class -Member function of the derived class -Can access protected class Private Only member function of the class can access private class members
  • 4. Friend functions & Classes in C++  If a function is defined as a friend function then private & protected data of a class can be accessed using the function  The compiler knows a given function is a friend function by the use of the keyword friend  The declaration of a friend function should be made inside the body of the class(Anywhere inside class either in private or public section starting with keyword friend
  • 5.
  • 6.
  • 7. Friend Class  Like a friend function , a class can also be a friend of another class. A friend class can access all protected & private variables of a class.
  • 8. Which Object Oriented Concept violates friend method & friend class?
  • 9. Difference Between Class & Struct in C++  Members of a class are private by default and members of struct are public by default
  • 10. Different kinds of data members in C++  Static static member is associated with class instead of an object Accessing static members within a class methods same as regular data members Accessing static data members outside the class int c = Class_Name::variable; Static Methods They do not have this pointer Can access non static data members private and protected data members
  • 11. Const  Const variables cannot be changed(Except mutable & const cast)  We can declare const methods.const members give a guarantee that method will not change the members of that class
  • 12. Const object  A const object’s values cannot be changed  Non const object can call both const & non const methods  Const object can call only const methods Mutable Modifier Mutable data member allow user to modify a particular data member
  • 13. Default Constructor  Zero argument constructor or constructor added by compiler  Print Bingo ten times without using loop, recursion or without writing ten times
  • 14.
  • 15. Constructor Initializer List C++  Initializer List is used to initialize data members of a class. The list of members to be initialized is indicated with constructor as a comma separated list followed by a colon
  • 16. When to use initializer list
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 23.
  • 24.  Default Copy Constructor perform shallow copy .We can write our copy constructor to perform deep copy(Deep copy & Shallow copy will be discuses in a future session)
  • 25.  For More Information refer below link  Write Bug Free Efficient Code in C++ with Dynamic Memory Allocation
  • 26. C++ inheritance  C++ supports 3 types of inheritance -public -private -protected Public Inheritance
  • 30. C++ supports both single inheritance & Multiple inheritance  Single Inheritance Multiple Inheritance
  • 31. Method Overriding in C++  Method overriding add or replace existing functions  In C++ Methods declared as virtual in the base class can be properly overridden by Derived class  We don’t need to repeat virtual keyword in front of the method definition in base classes  It is recommended to add the override keyword to the end of the overriding method(C++ 11)  By adding final keyword at the end of the methods, we can prevent overriding(C++ 11)
  • 32.  A pointer or reference type of a class can refer to an object of that class or its derived classes  Here Comes to the C++ polymorphism  C++ supports different types of polymorphism 1)Ad hoc polymorphism 2)Parametric polymorphism 3)Subtyping
  • 33. Static polymorphism & Dynamic Polymorphism  Static polymorphism – Compile Time  Dynamic Polymorphism –Run time Polymorphism
  • 34.
  • 35. In C++ Methods declared as virtual in the base class can be properly overridden by Derived class
  • 36.
  • 37. Call Base class Method in derived Class
  • 38. Virtual Functions in C++  A virtual function a member function which is declared within base class and is re-defined (Overriden) 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.
  • 39.
  • 40.
  • 41. Function Pointer  it’s also possible to declare a non-constant pointer to a function
  • 42. How virtual Functions resolves  C++ compiler creates a hidden class member called virtual-pointer or in short vfptr when there are one or more virtual functions. This vfptr is a pointer that points to a table of function pointers. This table is also created by compiler and called virtual function table or vftable. Each row of the vftable is a function pointer pointing to a corresponding virtual function.
  • 43.
  • 44. Super *sPtr = new Sub();
  • 46. Making base class destructor virtual guarantees that the object of derived class is destructed properly  If derived class allocates dynamic memory ,open files, database connections if base class destructor is not virtual then only base destructor may call and oboe resources will not be released;
  • 47.
  • 48. Pure virtual Functions & Abstract Classes  A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration  When a class has one or more than pure virtual functions. That class becomes an abstract class & we cannot create an object out of it. But we can create pointers or reference types in abstract classes  If we inherit from abstract class we have to add implementation to all pure virtual functions ,Otherwise it will also become abstract  C++ does not have interfaces, Only abstract classes
  • 49.
  • 50. C++ allows multiple inheritance
  • 52.
  • 53. The solution to this problem is ‘virtual’ keyword. We make the classes ‘Faculty’ and ‘Student’ as virtual base classes to avoid two copies of ‘Person’ in ‘TA’ class. For example, consider the following program.
  • 54.
  • 56.
  • 57.
  • 58.
  • 59.
  • 61.
  • 62.
  • 63. Shallow copy vs Deep Copy Shallow Copy int *p = new int; *p =100; Int *q = p; Deep Copy int *p = new int ; Int *q = new int ; *p =*q; *q = 99;
  • 64.