C++ Inheritance: Understanding Derived and Base Classes, Lecture notes of Design

An introduction to inheritance in C++ programming, explaining the concept of 'is-a' relationship, derived and base classes, redefining member functions, constructors, and accessing private members. It also covers public, protected, and private inheritance, and the use of virtual functions.

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

aristocrat
aristocrat 🇬🇧

5

(5)

240 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Module 8
Inheritance
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download C++ Inheritance: Understanding Derived and Base Classes and more Lecture notes Design in PDF only on Docsity!

Module 8Inheritance

Objectives

-^ In this topic, you will:^ –^ Learn about inheritance^ –^ Learn about derived and base classes^ –^ Redefine the member functions of a base class^ –^ Examine how the constructors of base and derived classeswork^ –^ Learn how to construct the header file of a derived class C++ Programming: Program Design Including Data Structures, Sixth Edition

Inheritance

-^ Inheritance: “is-a” relationship^ –^ Example: “every employee is a person” •^ Inheritance allows creation of new classes fromexisting classes^ –^ Derived classes

: new classes created from the existing class – Base class: the original class • Derived class inherits the properties of its baseclasses C++ Programming: Program Design Including Data Structures, Sixth Edition

Inheritance (cont’d.)

-^ Inheritance helps reduce software complexity •^ Single inheritance

: derived class has a single base class • Multiple inheritance

: derived class has more than one base class • Public inheritance

: all public members of base class are inherited as public members by derived class C++ Programming: Program Design Including Data Structures, Sixth Edition

Inheritance (cont’d.)

-^ Syntax of a derived class:^ –^ memberAccessSpecifier

is^ public

,^ protected

,

or^ private

(default)

-^ private

members of a base class are

private

to

the base class^ –^ Derived class cannot directly access them C++ Programming: Program Design Including Data Structures, Sixth Edition

Inheritance (cont’d.)

-^ public

members of base class can be inherited as public or^ private

members

-^ Derived class can include additional members (dataand/or functions) •^ Derived class can redefine

public

member

functions of the base class^ –^ Applies only to the objects of the derived class • All members variables of the base class are alsomember variables of the derived class C++ Programming: Program Design Including Data Structures, Sixth Edition

-^ boxType

is derived from

rectangleType

, and it

is a^ public

inheritance

-^ Also overrides

print^ and

area

Redefining Member Functions ofthe Base Class (cont’d.)

C++ Programming: Program Design Including Data Structures, Sixth Edition

Constructors of Derived and Base

Classes

-^ Derived class constructor cannot directly access^ private

members of the base class

-^ It can directly initialize only

public^ member variables of the base class • When a derived object is declared, it must executeone of the base class constructors • Call to base class constructor is specified in headingof derived class constructor definition C++ Programming: Program Design Including Data Structures, Sixth Edition

Header File of a Derived Class • To define new classes, create new header files • To create new derived classes, include commandsthat specify where the base class definitions can befound • Definitions of the member functions can be placed ina separate file

C++ Programming: Program Design Including Data Structures, Sixth Edition

Multiple Inclusions of a Header

File

-^ Use the preprocessor command (

#include

) to

include a header file in a program^ –^ Preprocessor processes the program before it is compiled • To avoid multiple inclusion of a file in a program, usecertain preprocessor commands in the header file C++ Programming: Program Design Including Data Structures, Sixth Edition

C++ Stream Classes (cont’d.) • istream^ and

ostream

provide operations for data transfer between memory and devices^ –^ istream

defines the extraction operator (

) and

functions^ get

and^ ignore

-^ ostream

defines the insertion operator (

<<) which is

used by^ cout • ifstream

/ofstream

objects are for file I/O

-^ Header file

fstream

contains the definitions for these C++ Programming: Program Design Including Data Structures, Sixth Edition

Protected Members of a Class • Derived class cannot directly access

private

members of it base class^ •^ To give it direct access, declare that member as^ protected C++ Programming: Program Design Including Data Structures, Sixth Edition

OOD and OOP (cont’d.)

-^ In OOD:^ –^ Object is a fundamental entity^ –^ Debug at the class level^ –^ A program is a collection of interacting objects •^ OOD encourages code reuse •^ Object-oriented programming (OOP) implementsOOD C++ Programming: Program Design Including Data Structures, Sixth Edition

OOD and OOP (cont’d.)

-^ C++ supports OOP through the use of classes •^ Function name and operators can be overloaded •^ Polymorphic function or operator: has many forms^ –^ Example: division with floating point and division withinteger operands C++ Programming: Program Design Including Data Structures, Sixth Edition