Title: Chapter 15 What is Inheritance?
1Chapter 15 What is Inheritance?
- Inheritance allows a new class to be based on an
existing class. - The new class inherits all the member variables
and functions of the class it is based on. - Note difference between is-a and has-a.
- A station-wagon is-a car, but a car has-a wheel.
2Inherited Class
3Inheriting from Base Class
4 test.h - Program
- ifndef TEST_H
- define TEST_H
- include "grade.h"
- class Test public Grade
- private
- int numQuestions
- float pointsEach
- int numMissed
- public
- Test ( int, int )
- // Test
- endif
5Why would you want?
- In our set example, we could derive sets with
various differences - traits, prints differently
- new methods, isKind
- keep track of number of traits
- Animals, add/replace features. Logical
categorization - Cars, add common characteristics, options
- Allows customization, reuse of base class.
- Keeps design consistent. All cars have
dealerRetail. All cars have taxAmount.
6Protected Members and Class Access
- Protected members of a base class are like
private members, but they may be accessed by
derived classes. - The base class access specification determines
how private, protected, and public base class
members may be accessed by derived classes.
7Base Class Access
8Constructors and Destructors
- The base classs constructor is called before the
derived classs constructor. - Since constructors have arguments, we are often
not content with an argument-less call (the
default). - The destructors are called in reverse order, with
the derived classs destructor being called first.
9Passing Arguments to Base Class Constructors
- Assume a class called Cube is derived from a
class called Rect. Here is how the constructor
looks in Rect - RectRect ( float w, float l )
- width w
- length l
- area length width
- // RectRect
10Cube Constructor
- CubeCube( float wide, float long, float high )
- Rect(wide, long)
- height high
- volume area high
- // CubeCube
11Overriding Base Class Functions
- A member function of a derived class may have the
same name as a member function of a base class.
12Polymorphism and Virtual Member Functions
- The term polymorphism means the ability to take
many form. - A virtual member function in a base class expects
to be overridden in a derived class
13Need for Virtual Member Functions
14Abstract Base Classes and Pure Virtual Functions
- An abstract base class is not instantiated, but
other classes are derived from it. - A pure virtual function is a virtual member
function of a base class that must be overridden.
15Abstract Base Classes and Pure Virtual Functions
(cont)
- A class becomes an abstract base class when it
contains one or more pure virtual functions. - A pure virtual function is a virtual member
function declared in a manner similar to the
followingvirtual void showInfo(void) 0
16Base Class Pointers
- Pointers to a base class may be assigned the
address of a derived class object. - The pointer, however, ignores any overrides the
derived class performs
17Classes Derived from Derived Classes
- A base class can also be derived from another
class. - This gives us a chain of inheritance where one
class is derived from a second, which in turn is
derived from a third.
18Chain of Inheritance
19Multiple Inheritance
- Multiple inheritance is when a derived class has
two or more base classes.
20Multiple Inheritance (cont)
21Examples
- Could have a fruit which falls under two
categories both fruit and vegetable. A tomato
is sometimes referred to as a fruit and sometimes
as a vegetable. - fruit - the ripened reproductive body of a seed
plant - vegetable - edible seeds or roots or stems or
leaves or bulbs or tubers or nonsweet fruits of
any of numerous herbaceous plant - Cross-categorization. An apple is a fruit and an
apple is a breakfast food.
22Given the following, organize them into
inheritance or composition
- A test for 1720, a student in 1720
- An electronic device, a computer, a calculating
tool - A woman, an employee, a person
- A course, a course evaluation
- Clothing, scarf, winter clothes, boots
- Sesame Street Character, Big Bird, Oscar the
Grouch
23Concerns on multiple inheritance
- Multiple inheritance is generally thought to be a
bad idea because a common base class (B and C
inherit from A, and D inherits from both B and C)
cause problems with both B and C using the common
variables differently. - Java handles with interface function use is
specified, but no implementation.