SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop pptdaxesh chauhan
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingAmit Soni (CTFL)
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and PackagesDamian T. Gordon
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...Simplilearn
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaMadishetty Prathibha
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
Inheritance in OOPs with java
Inheritance in OOPs with javaInheritance in OOPs with java
Inheritance in OOPs with javaAAKANKSHA JAIN
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Sanjit Shaw
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingIqra khalil
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programmingSachin Sharma
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overridingRajab Ali
 

La actualidad más candente (20)

Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
JAVA PROGRAMMING
JAVA PROGRAMMING JAVA PROGRAMMING
JAVA PROGRAMMING
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Oops
OopsOops
Oops
 
Inheritance in OOPs with java
Inheritance in OOPs with javaInheritance in OOPs with java
Inheritance in OOPs with java
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 

Similar a 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 a 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
 

Último

Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionSneha Padhiar
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESkarthi keyan
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfChristianCDAM
 
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptJohnWilliam111370
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书rnrncn29
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdfAkritiPradhan2
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Sumanth A
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming languageSmritiSharma901052
 
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
 
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmComputer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmDeepika Walanjkar
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
signals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsignals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsapna80328
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsResearcher Researcher
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdfHafizMudaserAhmad
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfDrew Moseley
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSneha Padhiar
 

Último (20)

Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based question
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdf
 
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.pptROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming language
 
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
 
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithmComputer Graphics Introduction, Open GL, Line and Circle drawing algorithm
Computer Graphics Introduction, Open GL, Line and Circle drawing algorithm
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
signals in triangulation .. ...Surveying
signals in triangulation .. ...Surveyingsignals in triangulation .. ...Surveying
signals in triangulation .. ...Surveying
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending Actuators
 
Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdf
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATIONSOFTWARE ESTIMATION COCOMO AND FP CALCULATION
SOFTWARE ESTIMATION COCOMO AND FP CALCULATION
 

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.