0% found this document useful (0 votes)
5 views39 pages

Lecture 06

Uploaded by

Yunus Gk
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)
5 views39 pages

Lecture 06

Uploaded by

Yunus Gk
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
You are on page 1/ 39

Lecture 06

Inheritance and Composition

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

1
Objectives

• In this chapter, you will:


• Learn about inheritance
• Learn about derived and base classes
• Explore how to redefine the member functions of a base class
• Examine how the constructors of base and derived classes work
• Learn how the destructors of base and derived classes work

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 2
or otherwise on a password-protected website for classroom
Introduction

• Two common ways to relate two classes in a meaningful way are:


• Inheritance (“is-a” relationship)
• Composition or aggregation: (“has-a” relationship)

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 3
or otherwise on a password-protected website for classroom
Introduction

• Two common ways to relate two classes in a meaningful way are:


• Inheritance (“is-a” relationship)
• Composition or aggregation: (“has-a” relationship)

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 4
or otherwise on a password-protected website for classroom
Introduction

• Two common ways to relate two classes


in a meaningful way are:
• Inheritance (“is-a” relationship)
• Composition or aggregation: (“has-a”
relationship)

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 5
or otherwise on a password-protected website for classroom
Inheritance (1 of 5)

• Inheritance is an “is-a” relationship


• Example: “every employee is a person”
• Inheritance, creating new classes from existing classes
• Derived classes: new classes created from the existing classes
• Base class: the original class
• A derived class inherits the properties of its base classes

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 6
or otherwise on a password-protected website for classroom
Inheritance - Example

• An object from a derived class (subclass) “is an” object of a base class
(superclass)

Base Class Derived Class

STUDENT GraduateStudent, UndergraduateStudent

SHAPE Circle, Triangle, Rectangle, Sphere, Cube

LOAN CarLoan, HomeImprovementLoan, MortgageLoan

EMPLOYEE Faculty, Staff

ACCOUNT CheckingAccount, SavingsAccount

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 7
or otherwise on a password-protected website for classroom
Inheritance (2 of 5)

• Inheritance helps reduce software development 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

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 8
or otherwise on a password-protected website for classroom
Inheritance (2 of 5)

• Inheritance helps reduce software development 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

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 9
or otherwise on a password-protected website for classroom
Inheritance (2 of 5)

• Inheritance helps reduce software development 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

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 10
or otherwise on a password-protected website for classroom
Inheritance (3 of 5)

Inheritance can be viewed as a tree-like, or hierarchical, structure between the base


class and its derived classes
Inheritance hierarchy
for university 1000 members

Each arrow in the hierarchy represents an is-a relationship

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 11
or otherwise on a password-protected website for classroom
Inheritance (4 of 5)

Inheritance hierarchy for Shapes

Each arrow in the hierarchy represents an is-a relationship

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 12
or otherwise on a password-protected website for classroom
Inheritance (4 of 5)

Inheritance hierarchy for Shapes

Each arrow in the hierarchy represents an is-a relationship

class TwoDimensionalShape : public Shape

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 13
or otherwise on a password-protected website for classroom
Inheritance (4 of 5)

• 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

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 15
or otherwise on a password-protected website for classroom
Inheritance : Example of derived class

• Syntax of a public inheritance:

• Definition of circle

• Syntax of a private inheritance:

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 16
or otherwise on a password-protected website for classroom
Inheritance : Example of derived class

• Syntax of a public inheritance:

• Definition of circle

• Syntax of a private inheritance:

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 17
or otherwise on a password-protected website for classroom
Inheritance : Example of derived class

• Syntax of a public inheritance:

• Definition of circle

• Syntax of a private inheritance:

private

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 18
or otherwise on a password-protected website for classroom
Inheritance (5 of 5)

• private members of a base class cannot directly access


• public members of the base class can be inherited as public or private
members
• The derived class can contain additional members
• The derived class can redefine public member functions of the base class
• Applies only to the objects of the derived class
• All member variables of the base class are also members of the derived class

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 19
or otherwise on a password-protected website for classroom
Redefining (Overriding) Member Functions of the Base Class (1 of 3)

• To redefine a public member function:


• The corresponding function in the derived class must have the same name, number,
and types of parameters
• If the derived class overrides a public member function of the base class, then
to call the base class function, specify the:
• Name of the base class
• Scope resolution operator (::)
• Function name with appropriate parameter list

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 21
or otherwise on a password-protected website for classroom
Redefining (Overriding) Member Functions of the Base Class (1 of 3)

• To redefine a public member function:


• The corresponding function in the derived class must have the same name, number,
and types of parameters
• If the derived class overrides a public member function of the base class, then
to call the base class function, specify the:
• Name of the base class
• Scope resolution operator (::)
• Function name with appropriate parameter list

class Base {
public:
void show() { cout << "Base class"; } };

class Derived: public Base{

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 22
or otherwise on a password-protected website for classroom
Redefining (Overriding) Member Functions of the Base Class (1 of 3)

• To redefine a public member function:


• The corresponding function in the derived class must have the same name, number,
and types of parameters
• If the derived class overrides a public member function of the base class, then
to call the base class function, specify the:
• Name of the base class
• Scope resolution operator (::)
• Function name with appropriate parameter list

class Base {
public:
void show() { cout << "Base class"; } };

class Derived: public Base{


public:
void show() { cout << " Derived class"; } }

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 23
or otherwise on a password-protected website for classroom
Redefining (Overriding) Member Functions of the Base Class (1 of 3)

• The definition of class rectangleType:

class rectangleType
{
public:
void setDimension(double l, double w);
double getLength() const;
double getWidth() const;
double area() const;
double perimeter() const;
void print() const;

rectangleType();
rectangleType(double l, double w);

private:
double length;
double width;
};

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 24
or otherwise on a password-protected website for classroom
Redefining (Overriding) Member Functions of the Base Class (2 of 3)

FIGURE 11-2 UML class diagram of the class rectangleType

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 25
or otherwise on a password-protected website for classroom
Redefining (Overriding) Member Functions of the Base Class (2 of 3)

void rectangleType::setDimension(double l,
double w)
{
if (l >= 0)
length = l;
else
length = 0;
if (w >= 0)
width = w;
else
width = 0;
}
double rectangleType::getLength() const
{
return length;
}
double rectangleType::getWidth()const
{
return width;
}
double rectangleType::area() const
{
return length * width;
}
perimeter(), print()…
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 26
or otherwise on a password-protected website for classroom
Redefining (Overriding) Member Functions of the Base Class (2 of 3)

void rectangleType::setDimension(double l,
double w)
{
if (l >= 0)
length = l;
else • The derived class from class rectangleType:
length = 0;
if (w >= 0) class boxType: public rectangleType
width = w; {
else public:
width = 0; void setDimension(double l, double
} w, double h);
double rectangleType::getLength() const double getHeight() const;
{ double volume() const;
return length; double area() const;
} void print() const;
double rectangleType::getWidth()const boxType();
{ boxType(double l, double w, double
return width; h);
} private:
double rectangleType::area() const double height;
{ };
return length * width;
}
perimeter(), print()…
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 27
or otherwise on a password-protected website for classroom
Redefining (Overriding) Member Functions of the Base Class (2 of 3)

void rectangleType::setDimension(double l,
double w)
{
if (l >= 0)
length = l;
else • The derived class from class rectangleType:
length = 0;
if (w >= 0) class boxType: public rectangleType
width = w; {
else public:
width = 0; void setDimension(double l, double
} w, double h);
double rectangleType::getLength() const double getHeight() const;
{ double volume() const;
return length; double area() const;
} void print() const; overrides
double rectangleType::getWidth()const boxType();
{ boxType(double l, double w, double
return width; h);
} private:
double rectangleType::area() const double height;
{ };
return length * width;
}
perimeter(), print()…
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 28
or otherwise on a password-protected website for classroom
Redefining (Overriding) Member Functions of the Base Class (3 of 3)

• boxType is derived from rectangleType, and it is a public inheritance


• Also overrides the functions print and area

FIGURE 11-3 UML class diagram of the class boxType and the inheritance hierarchy

• Calling the function area of the class rectangleType


• rectangleType::area()

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 29
or otherwise on a password-protected website for classroom
Constructors of Derived and Base Classes

• A derived class constructor cannot directly access private members of the


base class
• Can directly initialize only public member variables of the base class
• When a derived object is declared, it must execute one of the base class
constructors
• A call to the base class constructor is specified in the heading of the derived
class constructor definition

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 30
or otherwise on a password-protected website for classroom
Constructors of Derived and Base Classes

• Default constructor of the class boxType.


boxType::boxType()
{
height = 0.0;
}

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 31
or otherwise on a password-protected website for classroom
Constructors of Derived and Base Classes

• Default constructor of the class boxType.


boxType::boxType()
{
height = 0.0;
}

• Constructor with parameters of the class boxType:


boxType::boxType(double l, double w,
double h)
: rectangleType(l, w)
{
if (h >= 0)
height = h;
else
height = 0;
}

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 32
or otherwise on a password-protected website for classroom
Constructors of Derived and Base Classes

• Default constructor of the class boxType.


boxType::boxType()
{
height = 0.0;
}

• Constructor with parameters of the class boxType:


boxType::boxType(double l, double w,
double h)
: rectangleType(l, w)
{
if (h >= 0)
height = h;
else
height = 0;
}

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 33
or otherwise on a password-protected website for classroom
Constructors of Derived and Base Classes

• Consider the following statements:


rectangleType myRectangle(5.0, 3.0); //Line 1
boxType myBox(6.0, 5.0, 4.0); //Line 2

FIGURE 11-4 Objects myRectangle and myBox

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 34
or otherwise on a password-protected website for classroom
Check Yourself!

• What is the output of the statements?


myRectangle.print(); //Line 3
cout << endl; //Line 4
myBox.print(); //Line 5
cout << endl; //Line 6

FIGURE 11-4 Objects myRectangle and myBox

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 35
or otherwise on a password-protected website for classroom
Check Yourself!

• What is the output of the statement?


myRectangle.print(); //Line 3
cout << endl; //Line 4
myBox.print(); //Line 5
cout << endl; //Line 6

FIGURE 11-4 Objects myRectangle and myBox

Length = 5.0; Width = 3.0


Length = 6.0; Width = 5.0; Height = 4.0

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 36
or otherwise on a password-protected website for classroom
Destructors in a Derived Class

• Destructors deallocate dynamic memory allocated by the objects of a class


• When a derived class object goes out of scope
• Automatically invokes its destructor
• When the destructor of the derived class executes
• Automatically invokes the destructor of the base class

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 37
or otherwise on a password-protected website for classroom
Quick Review (1 of 4)

• Inheritance and composition are meaningful ways to relate two or more classes
• Inheritance is an “is-a” relation
• Single inheritance: a derived class is derived from one class, called the base class
• Multiple inheritance: a derived class is derived from more than one base class
• Composition is a “has-a” relation

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 38
or otherwise on a password-protected website for classroom
Quick Review (2 of 4)

• private members of a base class are private to the base class


• public members of a base class can be inherited either as public or
private
• A derived class can redefine function members of a base class
• Redefinition applies only to objects of derived class

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 39
or otherwise on a password-protected website for classroom
Quick Review (3 of 4)

• A call to a base class constructor (with parameters) is specified in the heading


of the definition of the derived class constructor
• When initializing object of a derived class, the base class constructor is
executed first
• In composition (aggregation):
• A class member is an object of another class
• A call to constructor of member objects is specified in heading of the definition of
class’s constructor

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 40
or otherwise on a password-protected website for classroom
Quick Review (4 of 4)

• Three basic principles of OOD:


• Encapsulation
• Inheritance
• Polymorphism
• To find classes:
• Describe the problem
• Choose classes from the list of nouns
• Choose operations from the list of verbs

© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 41
or otherwise on a password-protected website for classroom

You might also like