SlideShare a Scribd company logo
Support for
OOP in C++
Support for OOP in C++
▸ General Characteristics:
1- Evolved from SIMULA 67.
• Simula is a name for two simulation languages,
Simula I and Simula 67, Developed in the 1960s, by
Ole-Johan Dahl and Kristen Nygaard.
• Simula is considered the first Object-Oriented
Programming language.
• Simula provided the framework for many of the
OOP languages today.
2- Most widely used OOP language.
2
Support for OOP in C++
3
3- Mixed typing system (Retains C types, adds classes).
• Static typing: A variable has a single type associated with it throughout it is life at run time (Types of all
variables/expressions are determined at compile time.
• Dynamic typing: Allow the type of a variable. As well as it is value, to change as the program runs.
4- Constructors and Destructors (Implicitly called when objects are created/cease to
exist).
• Constructor is a special member function of a class that is executed whenever we create new objects of
that class.
A constructor will have exact same name as the class and it does not have any return type at all, not even
void. Constructors can be very useful for setting initial values for certain member variables.
• Destructor is a special member function that is called when the lifetime of an object ends.
The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.
Support for OOP in C++
4
5- Elaborate access controls to class entities.
Private Protected Public
Same Class YES YES YES
Derived Class NO YES YES
Friend Class YES YES YES
Other class NO NO YES
Access
ModifiersAccess
Locations
“Access controls”
Support for OOP in C++
5
 Inheritance
• A class need not be the subclass of any class
• Access controls for members are
– Private (visible only in the class and friends) (disallows subclasses from being subtypes)
– Public (visible in subclasses and clients)
– Protected (visible in the class and in subclasses, but not clients)
Support for OOP in C++
6
Public base classes in C++ has the class declaration:
class < derived > : public < base >
{
< member-declarations >
};
Private base classes in C++ has the class declaration:
class < derived > : private < base >
{
< member-declaration >
};
Support for OOP in C++
7
Inheritance example in C++ :
class base_class
{
private:
int a;
float x;
protected:
int b;
float y;
public:
int c;
float z;
};
class subclass_1 : public base_class { … };
//b and y are protected and c and z are public
class subclass_2 : private base_class { … };
//b, y, c, and z are private, and no derived class of
//subclass_2 has access to any member of base_class
//Note that a and x are not accessible in either
//subclass_1 or subclass_2
class subclass_3 : private base_class {
base_class :: c; }
//Instances of subclass_3 can access c.
An object is
an instance of a class,
and may be called a
class instance or
class object.
Support for OOP in C++
8
In addition, the subclassing process can be declared with access controls (private or public), which define
potential changes in access by subclasses.
> Private derivation: inherited public and protected members are private in the subclasses (Does
not represent an is-a relationship (Inheritance).
> Public derivation: public and protected members are also public and protected in subclasses.
 In Private derivation:
- By default, all members inherited from < base > become private members of <derived > .
• Privacy principle:
- The private members of a class are accessible only to member functions of the class.
- Functions in a derived class cannot access the private members of it’s base class.
Support for OOP in C++
9
Multiple inheritance is supported
> If there are two inherited members with the same name, they can both be referenced using
the scope resolution operator.
> Multiple inheritance allows a new class to inherit from two or more classes.
class A { … };
class B : public A { … };
class C : public A { … };
class D : public B, public C { … };
Support for OOP in C++
10
Common problem with multiple Inheritance
> Diamond Problem
Is an ambiguity that arises when two classes B and C inherited from class A, and class D inherited
from both class B and class C.
If a method in D calls a method defined in A , and B and C overridden that method differently,
then from which class does it inherit: B or C?
D
B C
A
Support for OOP in C++
11
 Dynamic Binding
> Method can be defined to be virtual, which means that they can be called
through polymorphic variables and dynamically bound to messages.
> A pure virtual function has no definition at all.
“It cannot be called, unless it is redefined in the derived class.”
> A class that has at least one pure virtual function is an abstract class.
“An abstract class cannot be instantiated.”
Support for OOP in C++
12
class shape
{
public:
virtual void draw()=0;
};
Dynamic Binding Example
class rectangle : public shape
{
public:
void draw()
{
cout<<"rect n";
}
};
class square : public rectangle
{
public:
void draw()
{
cout<<"square n";
}
};
virtual void draw()=0;
“=0” in function definition
indicates a pure virtual
function.
Support for OOP in C++
13
Dynamic Binding Example
int main()
{
square *sq = new square;
rectangle *rect = new rectangle;
shape *ptr_shape = sq;
ptr_shape -> draw(); //Square
rect ->draw(); //Rect
rect = sq;
rect ->draw(); //Square
square sq2;
rectangle r2 = sq2;
r2.draw(); //Rect
}
Even thought it
contains a square
Support for OOP in C++
14
 Evaluation
> C++ provides extensive access controls (unlike other OO language such
as Smalltalk).
> C++ provides multiple inheritance.
> In C++, the programmer must decide at design time which methods
will be statically bound and which must be dynamically bound.
- Static binding is faster!
- Design Decision may be wrong, requiring change later.
- Dynamic binding in C++ is faster than Smalltalk.
Support for OOP in C++
14
Static binding
The choice of
which function to
call in a class that
employs
inheritance is made
at compile time.
Dynamic binding
The choice is
made at run
time.
“If you use dynamic binding, The program will look up which function to use in a virtual function table,
which takes a little time, making dynamic binding slower.”
THANKS!
Any questions?
Prepared by: Ameen Shaarawi
ameencom4u@gmail.com

More Related Content

What's hot (20)

PPTX
Introduction to C++
Sikder Tahsin Al-Amin
 
PPTX
virtual function
VENNILAV6
 
PPT
Friends function and_classes
asadsardar
 
PPT
11 constructors in derived classes
Docent Education
 
PPTX
Exception handling c++
Jayant Dalvi
 
PPTX
Type conversion
PreethaPreetha5
 
PPTX
Object oriented programming
Amit Soni (CTFL)
 
PPTX
Operator overloading
Burhan Ahmed
 
PPTX
C++ Class & object pointer in c++ programming language
HariTharshiniBscIT1
 
PDF
Java Basic Oops Concept
atozknowledge .com
 
PPTX
Destructors
DeepikaT13
 
PPTX
inheritance c++
Muraleedhar Sundararajan
 
PPTX
INLINE FUNCTION IN C++
Vraj Patel
 
PPTX
Constructors and destructors
Vineeta Garg
 
PPTX
OOPS Basics With Example
Thooyavan Venkatachalam
 
PPTX
sSCOPE RESOLUTION OPERATOR.pptx
Nidhi Mehra
 
PPTX
Class introduction in java
yugandhar vadlamudi
 
PPTX
Abstract class in c++
Sujan Mia
 
PPT
Class and object in C++
rprajat007
 
PPTX
Object Oriented Programming Using C++
Muhammad Waqas
 
Introduction to C++
Sikder Tahsin Al-Amin
 
virtual function
VENNILAV6
 
Friends function and_classes
asadsardar
 
11 constructors in derived classes
Docent Education
 
Exception handling c++
Jayant Dalvi
 
Type conversion
PreethaPreetha5
 
Object oriented programming
Amit Soni (CTFL)
 
Operator overloading
Burhan Ahmed
 
C++ Class & object pointer in c++ programming language
HariTharshiniBscIT1
 
Java Basic Oops Concept
atozknowledge .com
 
Destructors
DeepikaT13
 
inheritance c++
Muraleedhar Sundararajan
 
INLINE FUNCTION IN C++
Vraj Patel
 
Constructors and destructors
Vineeta Garg
 
OOPS Basics With Example
Thooyavan Venkatachalam
 
sSCOPE RESOLUTION OPERATOR.pptx
Nidhi Mehra
 
Class introduction in java
yugandhar vadlamudi
 
Abstract class in c++
Sujan Mia
 
Class and object in C++
rprajat007
 
Object Oriented Programming Using C++
Muhammad Waqas
 

Viewers also liked (20)

PPTX
#OOP_D_ITS - 6th - C++ Oop Inheritance
Hadziq Fabroyir
 
PPT
OOP in C++
ppd1961
 
PPTX
Overview- Skillwise Consulting
Skillwise Group
 
PDF
C++ Programming : Learn OOP in C++
richards9696
 
PPT
C++ OOP Implementation
Fridz Felisco
 
PPTX
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Hadziq Fabroyir
 
PPTX
Control structures in c++
Nitin Jawla
 
PPTX
Pipe & its wall thickness calculation
sandeepkrish2712
 
PDF
Valve selections
Sandip Sonawane
 
PDF
Implementation of oop concept in c++
Swarup Kumar Boro
 
PPTX
Learn c++ Programming Language
Steve Johnson
 
PPT
Valve Selection & Sizing
Ranjeet Kumar
 
DOCX
Atm proposal in oop
Muzammal Hussain
 
PPTX
#OOP_D_ITS - 4th - C++ Oop And Class Structure
Hadziq Fabroyir
 
PPT
Chapter 14 management (10 th edition) by robbins and coulter
Md. Abul Ala
 
PPSX
01 General Control Valves Training.
SuryamshVikrama
 
PDF
Valve types and selection
Musa Sabri
 
PPTX
Control valve ppt
Tarit Mahata
 
PPT
Basic Control Valve Sizing and Selection
ISA Boston Section
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
Hadziq Fabroyir
 
OOP in C++
ppd1961
 
Overview- Skillwise Consulting
Skillwise Group
 
C++ Programming : Learn OOP in C++
richards9696
 
C++ OOP Implementation
Fridz Felisco
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Hadziq Fabroyir
 
Control structures in c++
Nitin Jawla
 
Pipe & its wall thickness calculation
sandeepkrish2712
 
Valve selections
Sandip Sonawane
 
Implementation of oop concept in c++
Swarup Kumar Boro
 
Learn c++ Programming Language
Steve Johnson
 
Valve Selection & Sizing
Ranjeet Kumar
 
Atm proposal in oop
Muzammal Hussain
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
Hadziq Fabroyir
 
Chapter 14 management (10 th edition) by robbins and coulter
Md. Abul Ala
 
01 General Control Valves Training.
SuryamshVikrama
 
Valve types and selection
Musa Sabri
 
Control valve ppt
Tarit Mahata
 
Basic Control Valve Sizing and Selection
ISA Boston Section
 
Ad

Similar to Support for Object-Oriented Programming (OOP) in C++ (20)

PDF
C++ Version 2
JIGAR MAKHIJA
 
PPTX
C++ first s lide
Sudhriti Gupta
 
PPTX
C++ Object Oriented Programming
Gamindu Udayanga
 
PPTX
C++ programming introduction
sandeep54552
 
PPTX
Procedure Oriented programming Object Oriented programming Basic Concept of ...
Govt Engineering college badliya ajmer Rajasthan
 
PDF
@vtucode.in-module-1-c++-2022-scheme.pdf
TheertheshTheertha1
 
PPTX
Four Pillers Of OOPS
Shwetark Deshpande
 
PPTX
Object oriented programming. (1).pptx
baadshahyash
 
PPTX
Lecture 1.pptx
IndraKhatri
 
PPTX
Interoduction to c++
Amresh Raj
 
PPTX
Features Of OOPS and characteristics.pptx
waarrior1234567
 
PDF
C++ Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
PDF
C++ OOPS Concept
Boopathi K
 
PDF
L10
lksoo
 
PDF
Classes-and-Objects-in-C++.pdf
ismartshanker1
 
PPTX
Summer Training Project On C++
KAUSHAL KUMAR JHA
 
PPTX
full defination of final opp.pptx
rayanbabur
 
PPT
Inheritance.ppt
KevinNicolaNatanael
 
PPT
The smartpath information systems c plus plus
The Smartpath Information Systems,Bhilai,Durg,Chhattisgarh.
 
C++ Version 2
JIGAR MAKHIJA
 
C++ first s lide
Sudhriti Gupta
 
C++ Object Oriented Programming
Gamindu Udayanga
 
C++ programming introduction
sandeep54552
 
Procedure Oriented programming Object Oriented programming Basic Concept of ...
Govt Engineering college badliya ajmer Rajasthan
 
@vtucode.in-module-1-c++-2022-scheme.pdf
TheertheshTheertha1
 
Four Pillers Of OOPS
Shwetark Deshpande
 
Object oriented programming. (1).pptx
baadshahyash
 
Lecture 1.pptx
IndraKhatri
 
Interoduction to c++
Amresh Raj
 
Features Of OOPS and characteristics.pptx
waarrior1234567
 
C++ Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
C++ OOPS Concept
Boopathi K
 
L10
lksoo
 
Classes-and-Objects-in-C++.pdf
ismartshanker1
 
Summer Training Project On C++
KAUSHAL KUMAR JHA
 
full defination of final opp.pptx
rayanbabur
 
Inheritance.ppt
KevinNicolaNatanael
 
The smartpath information systems c plus plus
The Smartpath Information Systems,Bhilai,Durg,Chhattisgarh.
 
Ad

Recently uploaded (20)

PPTX
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
PDF
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
PDF
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
PPTX
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PDF
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
PDF
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
PPTX
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPTX
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PPTX
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
PDF
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PDF
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
PPTX
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PPTX
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
 
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 

Support for Object-Oriented Programming (OOP) in C++

  • 2. Support for OOP in C++ ▸ General Characteristics: 1- Evolved from SIMULA 67. • Simula is a name for two simulation languages, Simula I and Simula 67, Developed in the 1960s, by Ole-Johan Dahl and Kristen Nygaard. • Simula is considered the first Object-Oriented Programming language. • Simula provided the framework for many of the OOP languages today. 2- Most widely used OOP language. 2
  • 3. Support for OOP in C++ 3 3- Mixed typing system (Retains C types, adds classes). • Static typing: A variable has a single type associated with it throughout it is life at run time (Types of all variables/expressions are determined at compile time. • Dynamic typing: Allow the type of a variable. As well as it is value, to change as the program runs. 4- Constructors and Destructors (Implicitly called when objects are created/cease to exist). • Constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables. • Destructor is a special member function that is called when the lifetime of an object ends. The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.
  • 4. Support for OOP in C++ 4 5- Elaborate access controls to class entities. Private Protected Public Same Class YES YES YES Derived Class NO YES YES Friend Class YES YES YES Other class NO NO YES Access ModifiersAccess Locations “Access controls”
  • 5. Support for OOP in C++ 5  Inheritance • A class need not be the subclass of any class • Access controls for members are – Private (visible only in the class and friends) (disallows subclasses from being subtypes) – Public (visible in subclasses and clients) – Protected (visible in the class and in subclasses, but not clients)
  • 6. Support for OOP in C++ 6 Public base classes in C++ has the class declaration: class < derived > : public < base > { < member-declarations > }; Private base classes in C++ has the class declaration: class < derived > : private < base > { < member-declaration > };
  • 7. Support for OOP in C++ 7 Inheritance example in C++ : class base_class { private: int a; float x; protected: int b; float y; public: int c; float z; }; class subclass_1 : public base_class { … }; //b and y are protected and c and z are public class subclass_2 : private base_class { … }; //b, y, c, and z are private, and no derived class of //subclass_2 has access to any member of base_class //Note that a and x are not accessible in either //subclass_1 or subclass_2 class subclass_3 : private base_class { base_class :: c; } //Instances of subclass_3 can access c. An object is an instance of a class, and may be called a class instance or class object.
  • 8. Support for OOP in C++ 8 In addition, the subclassing process can be declared with access controls (private or public), which define potential changes in access by subclasses. > Private derivation: inherited public and protected members are private in the subclasses (Does not represent an is-a relationship (Inheritance). > Public derivation: public and protected members are also public and protected in subclasses.  In Private derivation: - By default, all members inherited from < base > become private members of <derived > . • Privacy principle: - The private members of a class are accessible only to member functions of the class. - Functions in a derived class cannot access the private members of it’s base class.
  • 9. Support for OOP in C++ 9 Multiple inheritance is supported > If there are two inherited members with the same name, they can both be referenced using the scope resolution operator. > Multiple inheritance allows a new class to inherit from two or more classes. class A { … }; class B : public A { … }; class C : public A { … }; class D : public B, public C { … };
  • 10. Support for OOP in C++ 10 Common problem with multiple Inheritance > Diamond Problem Is an ambiguity that arises when two classes B and C inherited from class A, and class D inherited from both class B and class C. If a method in D calls a method defined in A , and B and C overridden that method differently, then from which class does it inherit: B or C? D B C A
  • 11. Support for OOP in C++ 11  Dynamic Binding > Method can be defined to be virtual, which means that they can be called through polymorphic variables and dynamically bound to messages. > A pure virtual function has no definition at all. “It cannot be called, unless it is redefined in the derived class.” > A class that has at least one pure virtual function is an abstract class. “An abstract class cannot be instantiated.”
  • 12. Support for OOP in C++ 12 class shape { public: virtual void draw()=0; }; Dynamic Binding Example class rectangle : public shape { public: void draw() { cout<<"rect n"; } }; class square : public rectangle { public: void draw() { cout<<"square n"; } }; virtual void draw()=0; “=0” in function definition indicates a pure virtual function.
  • 13. Support for OOP in C++ 13 Dynamic Binding Example int main() { square *sq = new square; rectangle *rect = new rectangle; shape *ptr_shape = sq; ptr_shape -> draw(); //Square rect ->draw(); //Rect rect = sq; rect ->draw(); //Square square sq2; rectangle r2 = sq2; r2.draw(); //Rect } Even thought it contains a square
  • 14. Support for OOP in C++ 14  Evaluation > C++ provides extensive access controls (unlike other OO language such as Smalltalk). > C++ provides multiple inheritance. > In C++, the programmer must decide at design time which methods will be statically bound and which must be dynamically bound. - Static binding is faster! - Design Decision may be wrong, requiring change later. - Dynamic binding in C++ is faster than Smalltalk.
  • 15. Support for OOP in C++ 14 Static binding The choice of which function to call in a class that employs inheritance is made at compile time. Dynamic binding The choice is made at run time. “If you use dynamic binding, The program will look up which function to use in a virtual function table, which takes a little time, making dynamic binding slower.”