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
Ad

Recommended

C# Overriding
C# Overriding
Prem Kumar Badri
 
Function overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 
Static Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Encapsulation C++
Encapsulation C++
Hashim Hashim
 
OOP C++
OOP C++
Ahmed Farag
 
Classes and objects
Classes and objects
Anil Kumar
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Introduction to C++
Introduction to C++
Sikder Tahsin Al-Amin
 
virtual function
virtual function
VENNILAV6
 
Friends function and_classes
Friends function and_classes
asadsardar
 
11 constructors in derived classes
11 constructors in derived classes
Docent Education
 
Exception handling c++
Exception handling c++
Jayant Dalvi
 
Type conversion
Type conversion
PreethaPreetha5
 
Object oriented programming
Object oriented programming
Amit Soni (CTFL)
 
Operator overloading
Operator overloading
Burhan Ahmed
 
C++ Class & object pointer in c++ programming language
C++ Class & object pointer in c++ programming language
HariTharshiniBscIT1
 
Java Basic Oops Concept
Java Basic Oops Concept
atozknowledge .com
 
Destructors
Destructors
DeepikaT13
 
inheritance c++
inheritance c++
Muraleedhar Sundararajan
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
 
Constructors and destructors
Constructors and destructors
Vineeta Garg
 
OOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
sSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptx
Nidhi Mehra
 
Class introduction in java
Class introduction in java
yugandhar vadlamudi
 
Abstract class in c++
Abstract class in c++
Sujan Mia
 
Class and object in C++
Class and object in C++
rprajat007
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
Hadziq Fabroyir
 
OOP in C++
OOP in C++
ppd1961
 

More Related Content

What's hot (20)

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

Viewers also liked (20)

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

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

6 Inheritance
6 Inheritance
Praveen M Jigajinni
 
Inheritance
Inheritance
zindadili
 
9781285852744 ppt ch11
9781285852744 ppt ch11
Terry Yoast
 
10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt
VhlRddy
 
C++ presentation
C++ presentation
SudhanshuVijay3
 
Inheritance
Inheritance
Pranali Chaudhari
 
Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
11 Inheritance.ppt
11 Inheritance.ppt
LadallaRajKumar
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Inheritance
Inheritance
Burhan Ahmed
 
Opp concept in c++
Opp concept in c++
SadiqullahGhani1
 
Oops concept on c#
Oops concept on c#
baabtra.com - No. 1 supplier of quality freshers
 
Inheritance in C++
Inheritance in C++
Shweta Shah
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 
asic computer is an electronic device that can receive, store, process, and o...
asic computer is an electronic device that can receive, store, process, and o...
vaishalisharma125399
 
C++ Object Oriented Programming
C++ Object Oriented Programming
Gamindu Udayanga
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
Overview of Object Oriented Programming using C++
Overview of Object Oriented Programming using C++
jayanthi699330
 
Inheritance and its types explained.ppt
Inheritance and its types explained.ppt
SarthakKumar93
 
Introduction to object oriented programming concepts
Introduction to object oriented programming concepts
Ganesh Karthik
 
9781285852744 ppt ch11
9781285852744 ppt ch11
Terry Yoast
 
10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt
VhlRddy
 
Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Inheritance in C++
Inheritance in C++
Shweta Shah
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 
asic computer is an electronic device that can receive, store, process, and o...
asic computer is an electronic device that can receive, store, process, and o...
vaishalisharma125399
 
C++ Object Oriented Programming
C++ Object Oriented Programming
Gamindu Udayanga
 
Overview of Object Oriented Programming using C++
Overview of Object Oriented Programming using C++
jayanthi699330
 
Inheritance and its types explained.ppt
Inheritance and its types explained.ppt
SarthakKumar93
 
Introduction to object oriented programming concepts
Introduction to object oriented programming concepts
Ganesh Karthik
 
Ad

Recently uploaded (20)

ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
“THE BEST CLASS IN SCHOOL”. _
“THE BEST CLASS IN SCHOOL”. _
Colégio Santa Teresinha
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
JHS SHS Back to School 2024-2025 .pptx
JHS SHS Back to School 2024-2025 .pptx
melvinapay78
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
JHS SHS Back to School 2024-2025 .pptx
JHS SHS Back to School 2024-2025 .pptx
melvinapay78
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 

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.”