0% found this document useful (0 votes)
4 views15 pages

Module 22

Module M22 focuses on inheritance in C++, detailing how derived classes inherit data members and member functions from base classes, including the concepts of overriding and overloading. It explains the object layout, access specifications, and the implications of inheritance on member functions. The module concludes with a summary of the effects of inheritance on both data members and member functions.

Uploaded by

rashmibe.nayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Module 22

Module M22 focuses on inheritance in C++, detailing how derived classes inherit data members and member functions from base classes, including the concepts of overriding and overloading. It explains the object layout, access specifications, and the implications of inheritance on member functions. The module concludes with a summary of the effects of inheritance on both data members and member functions.

Uploaded by

rashmibe.nayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module M22

L
Partha Pratim
Das
Programming in Modern C++

E
Objectives &
Outlines
Module M22: Inheritance: Part 2: Data Member & Member Function: Override & Overload

T
Inheritance in
C++

P
Data Members
Object Layout

Member Partha Pratim Das

N
Functions
Overrides and
Overloads Department of Computer Science and Engineering
Comparison Indian Institute of Technology, Kharagpur
Module Summary
[email protected]

All url’s in this module have been accessed in September, 2021 and found to be functional

Programming in Modern C++ Partha Pratim Das M22.1


Module Recap

Module M22

• Understood Hierarchy or ISA Relationship in OOAD

L
Partha Pratim
Das
• Introduced the Semantics of Inheritance in C++

E
Objectives &
Outlines

T
Inheritance in
C++

P
Data Members
Object Layout

Member

N
Functions
Overrides and
Overloads

Comparison

Module Summary

Programming in Modern C++ Partha Pratim Das M22.2


Module Objectives

Module M22

• Understand how inheritance impacts data members and member functions

L
Partha Pratim
Das
• Introduce overriding of member function and its interactions with overloading

E
Objectives &
Outlines

T
Inheritance in
C++

P
Data Members
Object Layout

Member

N
Functions
Overrides and
Overloads

Comparison

Module Summary

Programming in Modern C++ Partha Pratim Das M22.3


Module Outline

Module M22

L
Partha Pratim
Das 1 Inheritance in C++

E
Objectives &
Outlines
2 Data Members

T
Inheritance in
C++
Object Layout

P
Data Members
Object Layout

Member

N
Functions 3 Member Functions
Overrides and
Overloads Overrides and Overloads
Comparison

Module Summary
4 Comparison

5 Module Summary

Programming in Modern C++ Partha Pratim Das M22.4


Inheritance in C++

Module M22

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Inheritance in
C++

P
Data Members
Object Layout

Member

N
Functions
Overrides and
Overloads

Comparison

Module Summary

Inheritance in C++

Programming in Modern C++ Partha Pratim Das M22.5


Inheritance in C++: Semantics

Module M22 • Derived ISA Base


• Data Members

L
Partha Pratim
Das
◦ Derived class inherits all data members of Base class
◦ Derived class may add data members of its own

E
Objectives &
Outlines
• Member Functions

T
Inheritance in
C++ ◦ Derived class inherits all member functions of Base class
◦ Derived class may override a member function of Base class by redefining it with the same signature

P
Data Members
Object Layout ◦ Derived class may overload a member function of Base class by redefining it with the same name;
Member
but different signature

N
Functions ◦ Derived class may add new member functions
Overrides and
Overloads • Access Specification
Comparison ◦ Derived class cannot access private members of Base class
Module Summary ◦ Derived class can access protected members of Base class
• Construction-Destruction
◦ A constructor of the Derived class must first call a constructor of the Base class to construct the
Base class instance of the Derived class
◦ The destructor of the Derived class must call the destructor of the Base class to destruct the Base
class instance of the Derived class

Programming in Modern C++ Partha Pratim Das M22.6


Data Members

Module M22

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Inheritance in
C++

P
Data Members
Object Layout

Member

N
Functions
Overrides and
Overloads

Comparison

Module Summary

Data Members

Programming in Modern C++ Partha Pratim Das M22.7


Data Members

Module M22

• Derived ISA Base

L
Partha Pratim
Das
• Data Members

E
Objectives &
Outlines ◦ Derived class inherits all data members of Base class

T
Inheritance in
C++
◦ Derived class may add data members of its own
• Object Layout

P
Data Members
Object Layout

Member
◦ Derived class layout contains an instance of the Base class

N
Functions ◦ Further, Derived class layout will have data members of its own
Overrides and
Overloads ◦ C++ does not guarantee the relative position of the Base class instance and
Comparison
Derived class members
Module Summary

Programming in Modern C++ Partha Pratim Das M22.8


Object Layout

Module M22
class B { // Base Class

L
Partha Pratim int data1B_;
Das
public:
Object Layout

E
Objectives & int data2B_;
Outlines // ...

T
Inheritance in }; Object b Object d
C++

P
Data Members
class D: public B { // Derived Class
Object Layout
// Inherits B::data1B_ data1B
Member data1B

N
Functions // Inherits B::data2B_ data2B
data2B
Overrides and int infoD_; // Adds D::infoD_
Overloads
public: infoD
Comparison
// ...
Module Summary }; • d cannot access data1B even though is a part of d!
• d can access data2B
B b; // Base Class Object

D d; // Derived Class Object

Programming in Modern C++ Partha Pratim Das M22.9


Member Functions

Module M22

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Inheritance in
C++

P
Data Members
Object Layout

Member

N
Functions
Overrides and
Overloads

Comparison

Module Summary

Member Functions

Programming in Modern C++ Partha Pratim Das M22.10


Member Functions

Module M22
• Derived ISA Base

L
Partha Pratim
Das • Member Functions
◦ Derived class inherits all member functions of Base class

E
Objectives &
Outlines
. Note: Derived class does not inherit the Constructors and Destructor of Base

T
Inheritance in
C++ class but must have access to them

P
Data Members
Object Layout
◦ Derived class may override a member function of Base class by redefining it with
Member the same signature

N
Functions
Overrides and
◦ Derived class may overload a member function of Base class by redefining it with
Overloads
the same name; but different signature
Comparison
◦ Derived class may add new member functions
Module Summary
• Static Member Functions
◦ Derived class does not inherit the static member functions of Base class
• Friend Functions
◦ Derived class does not inherit the friend functions of Base class
Programming in Modern C++ Partha Pratim Das M22.11
Overrides and Overloads

Module M22 Inheritance Override & Overload


class B { public: // Base Class class B { public: // Base Class

L
Partha Pratim void f(int i); void f(int);
Das
void g(int i); void g(int i);
}; };

E
Objectives &
Outlines class D: public B { public: // Derived Class class D: public B { public: // Derived Class
// Inherits B::f(int) // Inherits B::f(int)

T
Inheritance in
C++
void f(int); // Overrides B::f(int)
void f(string&); // Overloads B::f(int)

P
Data Members // Inherits B::g(int)
// Inherits B::g(int)
Object Layout
void h(int i); // Adds D::h(int)
Member }; };

N
Functions B b;
B b;
Overrides and
Overloads D d; D d;
Comparison
b.f(1); // Calls B::f(int) b.f(1); // Calls B::f(int)
Module Summary b.g(2); // Calls B::g(int) b.g(2); // Calls B::g(int)

d.f(3); // Calls B::f(int) d.f(3); // Calls D::f(int)


d.g(4); // Calls B::g(int) d.g(4); // Calls B::g(int)

d.f("red"); // Calls D::f(string&)


d.h(5); // Calls D::h(int)
• D::f(int) overrides B::f(int)
• D::f(string&) overloads B::f(int)
Programming in Modern C++ Partha Pratim Das M22.12
Overloading vis-a-vis Overriding

Module M22

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Inheritance in
C++

P
Data Members
Object Layout

Member

N
Functions
Overrides and
Overloads

Comparison

Module Summary

Overloading vis-a-vis Overriding

Programming in Modern C++ Partha Pratim Das M22.13


Comparison of Overloading vis-a-vis Overriding

Module M22
Basis Function Overloading Function Overriding

L
Partha Pratim
Das
Name of Function • All overloads have the same function name • All overrides have the same function name
Signature • Function signatures must be different • Function signatures are same

E
Objectives &
Outlines Type of Function • Can be global, friend, static or non-static • Must be a non-static member function - non-
member function virtual or virtual

T
Inheritance in
C++ Inheritance • Can happen with or without inheritance • Happens only with inheritance
Polymorphism • Static (Compile time) • Static (Compile time) or Dynamic (Runtime)

P
Data Members
Scope • Overloaded functions are in the same scope • Functions are in different scopes (base clase
Object Layout
and derived class)
Member Purpose • To have multiple functions with same name • To perform additional or different tasks than

N
Functions
that act differently depending on parameters the base class function
Overrides and
Overloads
Constructor • Constructors can be overloaded • Constructors cannot be overridden
Destructor • The destructor cannot be overloaded • The destructor cannot be overridden
Comparison
Usage • Can be overloaded multiple times • Can be overridden once in the derived class
Module Summary

Programming in Modern C++ Partha Pratim Das M22.14


Module Summary

Module M22

• Discussed the effect of inheritance on Data Members and Object Layout

L
Partha Pratim
Das
• Discussed the effect of inheritance on Member Functions with special reference to

E
Objectives &
Outlines Overriding and Overloading

T
Inheritance in
C++

P
Data Members
Object Layout

Member

N
Functions
Overrides and
Overloads

Comparison

Module Summary

Programming in Modern C++ Partha Pratim Das M22.15

You might also like