Inheritance: CS 308 - Data Structures
Inheritance: CS 308 - Data Structures
Vehicle
Car Bicycle
2-door 4-door
C++ and inheritance
• The language mechanism by which one
class acquires the properties (data and
operations) of another class
• Base Class (or superclass): the class being
inherited from
• Derived Class (or subclass): the class that
inherits
Advantages of inheritance
• When a class inherits from another class,
there are three benefits:
• (1) You can reuse the methods and data of
the existing class
(2) You can extend the existing class by
adding new data and new methods
(3) You can modify the existing class by
overloading its methods with your own
implementations
Deriving One Class from Another
Deriving One Class from Another (cont’d)
• Define a new class CountedQue from QueType such
that it has a new data member (length) that records
the number of items in the queue
template<class ItemType>
class CountedQue : public QueType<ItemType> {
public:
CountedQue();
void Enqueue (ItemType newItem);
void Dequeue (ItemType& item);
int LengthIs() const;
private:
int length;
};
Inheritance and accessibility
template<class ItemType>
int CountedQue<ItemType>::LengthIs() const
{
return length;
}
// class constructor
template<class ItemType>
CountedQue<ItemType>::CountedQue() : QueType<ItemType>()
{
length=0;
}
Polymorphism
• Any code you write to manipulate a base class will
also work with any class derived from the base class.
• C++ general rule for passing objects to a function:
“the actual parameters and their corresponding formal
parameters must be of the same type”
• With inheritance, C++ relaxes this rule:
“the type of the actual parameter can be a class derived
from the class of the formal parameter”
An example
template<class ItemType>
void Test(QueType& q, ItemType item)
{
q.Enqueue(item);
....
}
• Any object of a class derived from QueType can
be passed to the function !!
• Which Enqueue() function should be used? (the
compiler does not know that at compile time)
Static vs. dynamic binding
Y (base for Z)
Z
Example
template<class ItemType>
class StackType {
public:
StackType();
~StackType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Push (ItemType);
void Pop(ItemType&);
private:
NodeType<ItemType>* topPtr;
};
template<class ItemType>
class LookAheadStack : public StackType<ItemType>
{
public:
void Push(ItemType);
LookAheadStack();
~LookAheadStack();
};
b) Implement the new push function and the
derived class’ constructor.
template<class ItemType>
void LookAheadStack <ItemType>::Push(ItemType newItem)
{
ItemType item;
if ( !StackType<ItemType>::IsEmpty() ) {
StackType<ItemType>::Pop(item);
StackType<ItemType>::Push(item);
if (item != newItem)
StackType<ItemType>::Push(newItem);
}
else
StackType<ItemType>::Push(newItem);
}
Constructor:
template<class ItemType>
LookAheadStack <ItemType>:: LookAheadStack():StackType()
{
}
c) Which functions and from which class
should be declared as virtual?
The functions that should be declared as
virtual are: