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

2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - AbstractionMichael Heron
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented ProgrammingGamindu Udayanga
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 
Object Oriented Programming With C++
Object Oriented Programming With C++Object Oriented Programming With C++
Object Oriented Programming With C++Vishnu Shaji
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritanceharshaltambe
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3C# Learning Classes
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop InheritanceHadziq Fabroyir
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++jehan1987
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Liju Thomas
 
Classes and data abstraction
Classes and data abstractionClasses and data abstraction
Classes and data abstractionHoang Nguyen
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three noskrismishra
 

What's hot (20)

2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
Object Oriented Programming With C++
Object Oriented Programming With C++Object Oriented Programming With C++
Object Oriented Programming With C++
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
C++ classes
C++ classesC++ classes
C++ classes
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
 
Classes and data abstraction
Classes and data abstractionClasses and data abstraction
Classes and data abstraction
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Basic c#
Basic c#Basic c#
Basic c#
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Introduction to c ++ part -2
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
C++ largest no between three nos
C++ largest no between three nosC++ largest no between three nos
C++ largest no between three nos
 

Viewers also liked

OOP in C++
OOP in C++OOP in C++
OOP in C++ppd1961
 
Overview- Skillwise Consulting
Overview- Skillwise Consulting Overview- Skillwise Consulting
Overview- Skillwise Consulting Skillwise Group
 
C++ Programming : Learn OOP in C++
C++ Programming : Learn OOP in C++C++ Programming : Learn OOP in C++
C++ Programming : Learn OOP in C++richards9696
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP ImplementationFridz Felisco
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator OverloadingHadziq Fabroyir
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++Nitin Jawla
 
Pipe & its wall thickness calculation
Pipe & its wall thickness calculationPipe & its wall thickness calculation
Pipe & its wall thickness calculationsandeepkrish2712
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Kumar Boro
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming LanguageSteve Johnson
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Valve Selection & Sizing
Valve Selection & SizingValve Selection & Sizing
Valve Selection & SizingRanjeet Kumar
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class StructureHadziq Fabroyir
 
Chapter 14 management (10 th edition) by robbins and coulter
Chapter 14 management (10 th edition) by robbins and coulterChapter 14 management (10 th edition) by robbins and coulter
Chapter 14 management (10 th edition) by robbins and coulterMd. Abul Ala
 
01 General Control Valves Training.
01 General Control Valves Training.01 General Control Valves Training.
01 General Control Valves Training.SuryamshVikrama
 
Valve types and selection
Valve types and selectionValve types and selection
Valve types and selectionMusa Sabri
 
Basic Control Valve Sizing and Selection
Basic Control Valve Sizing and SelectionBasic Control Valve Sizing and Selection
Basic Control Valve Sizing and SelectionISA Boston Section
 
VALVES AND THEIR TYPES
VALVES AND THEIR TYPESVALVES AND THEIR TYPES
VALVES AND THEIR TYPESMadhur Mahajan
 

Viewers also liked (20)

OOP in C++
OOP in C++OOP in C++
OOP in C++
 
Overview- Skillwise Consulting
Overview- Skillwise Consulting Overview- Skillwise Consulting
Overview- Skillwise Consulting
 
C++ Programming : Learn OOP in C++
C++ Programming : Learn OOP in C++C++ Programming : Learn OOP in C++
C++ Programming : Learn OOP in C++
 
C++ OOP Implementation
C++ OOP ImplementationC++ OOP Implementation
C++ OOP Implementation
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Pipe & its wall thickness calculation
Pipe & its wall thickness calculationPipe & its wall thickness calculation
Pipe & its wall thickness calculation
 
Valve selections
Valve selectionsValve selections
Valve selections
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Valve Selection & Sizing
Valve Selection & SizingValve Selection & Sizing
Valve Selection & Sizing
 
Atm proposal in oop
Atm proposal in oopAtm proposal in oop
Atm proposal in oop
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure
 
Chapter 14 management (10 th edition) by robbins and coulter
Chapter 14 management (10 th edition) by robbins and coulterChapter 14 management (10 th edition) by robbins and coulter
Chapter 14 management (10 th edition) by robbins and coulter
 
01 General Control Valves Training.
01 General Control Valves Training.01 General Control Valves Training.
01 General Control Valves Training.
 
Valve types and selection
Valve types and selectionValve types and selection
Valve types and selection
 
Control valve ppt
Control valve pptControl valve ppt
Control valve ppt
 
Basic Control Valve Sizing and Selection
Basic Control Valve Sizing and SelectionBasic Control Valve Sizing and Selection
Basic Control Valve Sizing and Selection
 
VALVES AND THEIR TYPES
VALVES AND THEIR TYPESVALVES AND THEIR TYPES
VALVES AND THEIR TYPES
 

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

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

6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
9781285852744 ppt ch11
9781285852744 ppt ch119781285852744 ppt ch11
9781285852744 ppt ch11
 
10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Inheritance
InheritanceInheritance
Inheritance
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Summer Training Project On C++
Summer Training Project On  C++Summer Training Project On  C++
Summer Training Project On C++
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of features
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Inheritance
InheritanceInheritance
Inheritance
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 

Recently uploaded

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 

Recently uploaded (20)

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

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.”
  • 16. THANKS! Any questions? Prepared by: Ameen Shaarawi ameencom4u@gmail.com