















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















-^ 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: “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 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
-^ 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
-^ 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
C++ Programming: Program Design Including Data Structures, Sixth Edition
-^ 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
C++ Programming: Program Design Including Data Structures, Sixth Edition
-^ 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
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
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
-^ 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
-^ 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