Chapter 11 Inheritance
Chapter 11 Inheritance
COMPOSITION
Chapter 11
C++ Programming
OBJECTIVES:
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
Learn how the construct the header file of a derived class
Become aware of stream classes hierarchy
Explore three types of inheritance public, protected, and private
Learn about composition (aggregation)
Become familiar with the three basic principles of object-oriented design.
INHERITANCE
This important feature encourages code reuse.
Relate two or more classes in more than one way.
Two common ways to relate classes in a meaningful way are:
Inheritance (“is-a” relationship) ex: rectangle IS A shape.
Composition (aggregation) (“has-a” relationship)
OOP – INHERITANCE – (CLASS & SUBCLASSES)
1. width = w
2. height = h
Shape
+ setWidth (int): void
+ setHeight (int): void
# width: int
# height: int
DERIVED CLASS
# width: int
# height: int
NEXT MEETING
REDEFINING (OVERRIDING)
A derived class can include its own constructors since it can contain private
members.
Derived class object cannot directly access the private (data) members of the
base class.
The same is true for the member functions of a derived class.
So, constructors of a derived class can (directly) initialize only the (public
data) members inherited from the base class.
Derived class object triggers the execution of one of the base class’s
constructors. Done when create the derived class header definition.
CONSTRUCTORS OF DERIVED AND BASE CLASSES
myrectangle.print();
cout << endl;
mybox.print();
cout <<endl;
VALUE AND CALLING STATEMENTS
When a derived class object goes out of scope, it automatically invokes its
destructor.
When the destructor of the derived class executes, it automatically invokes the
destructor of the base class.
So when writing the definition of the destructor of the derived class, an explicit
call to the destructor of the base class is not needed.
Furthermore, when the destructor of the derived class executes, it executes its
own code first and then calls the destructor of the base class.
HEADER FILE OF A DERIVED CLASS
When base classes are already defined, and header files contain their definitions.
The header files of the new classes contain commands that tell the computer where to
look for the definitions of the base classes.
class personType is placed in the header file personType.h.
To create the definition of the class partTimeEmployee.
partTimeEmployee.h—must contain the preprocessor directive:
#include "personType.h
MULTIPLE INCLUSIONS OF A HEADER FILE
Suppose you have header file of test.h
//Header file test.h
const int ONE = 1;
const int TWO = 2;
testA.h includes the file test.h in order to use the identifiers ONE and TWO and
looks as:
//Header file testA.h
Which of these files execute first?
#include "test.h" //Header file test.h
//Header file testA.h
. //Program headerTest.cpp
Consider the following code:
This implementation generates Run
//Program headerTest.cpp
time error. Why?
#include "test.h"
#include "testA.h"
.
MULTIPLE INCLUSIONS OF A HEADER FILE
Void multiple inclusions:
//Header file test.h
#ifndef H_test
#define H_test
What is the effect of these statements?
const int ONE = 1;
const int TWO = 2;
#endif
class dateType {
public:
private:
dMonth = month;
dDay = day;
dYear = year;
};
COMPOSITION (AGGREGATION)
class personalInfo
{
public:
private:
personType name;
dateType bDay;
int personID;
};