Title: Inheritance
1Inheritance
- Software re-use with derived classes
2Inheritance
- Object-oriented languages allow new classes to
acquire properties from an existing class - this
is known as inheritance - Original class is called the base or parent
class, or the superclass - The new, slightly different class is the derived
or child class, or the subclass
3Derived classes
- A derived class contains all members and
functions of the base class - The subclass inherits these members, along with
their access characteristics - The declaration of the child class determines how
these access characteristics are manifested in
the child class
4Derived class declaration
- In the declaration of the child class, the parent
class is listed as either public or private for
example - class Set public Bag
- With this declaration, public members of Bag are
inherited as public members of Set - private members are inherited, but can be
accessed only through inherited functions
5Derived class declaration
- A derived class can also be declared with a
private base class - class Set private Bag
- public members of Bag are inherited as private
members of Set - private members of Bag are accessible only
through Bag member functions
6Protected class members
- If a base class member is declared as protected,
rather than public or private, that member is
directly accessible by any class derived from the
base - Protected members act just like private members
in terms of access by non-member functions,
except for members of derived classes
7Example of inheritance in action
- We will define two classes, Point and Circle
- Circle inherits Point members and adds members of
its own - The base class (Point) data members are declared
protected, rather than private, enabling direct
access by the derived class (Circle)
8Base Class Declaration
class Point public Point (double a0.0,
double b0.0) void setPoint(double a, double
b) double getX()const return x double
getY()const return y friend ostream operator
ltlt (ostream o, const Point p)
protected double x double y
9Base class function definitions
PointPoint (double a, double b) void
PointsetPoint(double a, double b) x
a x a y b y b
ostream operator ltlt (ostream o, const Point
p) o ltlt ltlt p.x ltlt , ltlt p.y ltlt
return o
10Child class definition
class Circle public Point
public Circle (double r 0.0, double x0,
double y0) void setRadius(double r) radius
r double getRadius () const return
radius double area () const return
radiusradius3.14159 friend ostream operator
ltlt (ostream o, const Circle c)
protected double radius
11Circle constructor
CircleCircle(double r, double x, double y)
Point(x,y) radius r
The line preceding the functions begin bracket
Point (x,y) indicates a call to the
constructor of the parent class
This is not actually a separate line of code, but
part of the functions heading
12Circle friend function
ostream operator ltlt (ostream o, const Circle
c) o ltlt Center ltlt c.x ltlt , ltlt c.y ltlt
ltlt Radius ltlt
c.radius return o
Members x and y of parent class Point are
accessible to child class Circle because of
protected status
Members x and y are members of the Circle
object in other words, there is a Point in every
Circle
13The is-a relationship
- The Point/Circle example is atypical of the way
inheritance is usually used - Inheritance usually involves creating a generic
base class, then deriving several child classes
from it - The relationship of the child to the parent is
usually described using the phrase is a -- we
say a derived class object is a parent class
object
14The is-a relationship
- For example, your textbook describes a generic
Clock class, then creates the derived class
CuckooClock - It is natural to say a CuckooClock is a Clock -
meaning a cuckoo clock is a kind of clock - It wouldnt be too much of a stretch to derive
other Clock types - alarm clock, 24-hour clock,
etc.
15Using a derived class
- Derived class objects can call base class member
functions as if they were member functions of the
derived class - An object of the derived class may be used in
place of a base class object - for example, if a
function takes a base class object as a
parameter, a derived class object may be
substituted
16Derived classes assignment
- Assignments are allowed form a derived class to a
base class if the base class is public - However, a base class object cannot be used as if
it were an object of the derived class - so you
cant assign a base class object to a derived
class variable
17Inheritance Overriding
- A derived class inherits all members of the base
class, but often a derived object needs to
perform a particular operation a different way
than that defined in an inherited function - When a new function body is provided for an
inherited member function within a derived class,
the function is overridden
18Inheritance Overriding
- Sometimes overriding a function involves adding
functionality, rather than simply replacing an
original base class function - When this is the case, the overriding function
can call the base class function as its first
action, then proceed with additional operations
19Example
int Clock24get_hour() const // overrides
inherited function int ohr ohr
Clockget_hour() // call to overridden
function if (is_morning()) if (ohr
12) return 0 else return ohr etc.
20Using inheritance with ADTs
- Throughout the semester, we have studied various
container classes - In many ways, one container is very much like
another inheritance can be used to derive many
different types of containers by placing common
characteristics in a base class
21Example Set derived from Bag
- Both Bags and Sets contain unordered collection
of elements - The basic difference between them is that all
elements in a Set must be unique - We can create a Set class by inheriting from Bag,
and overriding the insert function to eliminate
insertion of duplicates
22Example Generic List Class
- Suppose you are provided with a generic ADT to
hold an ordered list of items - The data part of the List class is unknown to
you, but you are given a set of member functions
and a description of their actions, as shown on
the slides that follow
23List member functions
List() // postcondition initializes an empty
List bool is_item() const // postcondition
returns true if there is a valid current // item
false if there is no valid current item Item
current() const // precondition is_item returns
true // postcondition returns current List
item void start() // postconditon first item on
List becomes current item // if list is empty,
there is no current item
24List member functions
void advance() // precondition is_item returns
true // postcondition if current item was the
last on List, there is // now no current item
otherwise, the new current item is the // one
immediately following the original current
item void insert (const Item entry) //
postcondition a new copy of entry has been
inserted before // the current item if no
current item existed, entry was inserted // at
the front of the List. Item containing entry is
now the // current item
25List member functions
void attach (const Item entry) // postcondition
a new copy of enty has been inserted in list //
after the current item if there was no current
item, entry was // inserted at the end of the
list. Item containing entry is now the //
current item void remove_current() //
precondition is_item returns true //
postcondition current item has been removed
item following // current (if one existed) is now
current item size_t size() const //
postcondition returns count of items in List
26Deriving Stack from List
- Stack is defined by its push and pop functions,
respectively inserting and removing items from
the same end of the structure - Because List has many functions that wont (and
shouldnt) be used directly by users of Stack
code, List is inherited as a private base class
27Stack class definition
template ltclass Itemgt class Stack private
ListltItemgt public
void push(const Item entry) Item
pop() Item peek() const
ListltItemgtsize // changes inherited private
function size to public // Stack function
available to Stack user - this is an // example
of exempting a member from private base //
class bool is_empty() const return size()
0
28Stack member functions
template ltclass Itemgt Item pop() Item
answer assert(size() gt 0) answer
current() remove_current() return answer
template ltclass Itemgt void push (const Item
entry) insert(entry)
template ltclass Itemgt Item peek() assert
(size() gt 0) return current()
29Deriving Queue from List
- A child queue class could be derived from List in
a similar fashion - List would be inherited as a private base class,
and queue functions enqueue and dequeue would be
defined using inherited private functions from
List
30Inheritance
- Software re-use with derived classes
- - ends -