SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract classAmit Trivedi
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Abdul Hannan
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generatorsanchi29
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesFellowBuddy.com
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++imran khan
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language Mohamed Loey
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Janki Shah
 
Recursive Function
Recursive FunctionRecursive Function
Recursive FunctionHarsh Pathak
 

Tendances (20)

Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Recursion in c++
Recursion in c++Recursion in c++
Recursion in c++
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture Notes
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 

Similaire à 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
 

Similaire à 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
 

Dernier

MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 

Dernier (20)

MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 

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.