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

Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
Prabhu Govind
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
abhishek kumar
 
Overview of c language
Overview of c languageOverview of c language
Overview of c languageshalini392
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Oops presentation
Oops presentationOops presentation
Oops presentation
sushamaGavarskar1
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
Hitesh Kumar
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Nilesh Dalvi
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
Tareq Hasan
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
C++ string
C++ stringC++ string
C++ string
Dheenadayalan18
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 
java token
java tokenjava token
java token
Jadavsejal
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 

What's hot (20)

Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
 
Overview of c language
Overview of c languageOverview of c language
Overview of c language
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Function in C
Function in CFunction in C
Function in C
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
C++ string
C++ stringC++ string
C++ string
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
java token
java tokenjava token
java token
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 

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++.pdf
ismartshanker1
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
SURBHI SAROHA
 
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++ Programming
Preeti Kashyap
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
nirajmandaliya
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
akreyi
 
C++ Version 2
C++  Version 2C++  Version 2
C++ Version 2
JIGAR MAKHIJA
 
C questions
C questionsC questions
C questions
parm112
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
Eng Teong Cheah
 
14_inheritance.ppt
14_inheritance.ppt14_inheritance.ppt
14_inheritance.ppt
sundas65
 
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, polymorphism
Jawad Khan
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
SadiqullahGhani1
 
C# interview
C# interviewC# interview
C# interview
Thomson Reuters
 
Class and object
Class and objectClass and object
Class and object
prabhat kumar
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
 

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

Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 

Recently uploaded (20)

Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.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.