Inheritance
Inheritance
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)
template<class ItemType>
void CountedQue<ItemType>::Dequeue(ItemType& item)
{
length--;
QueType<ItemType>::Dequeue(item);
}
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
....
PrintResult(item1, item2);
PrintResult(item3, item4);
Protected class members
• Derived classes cannot access the private data
of the base class
• Declaring methods and data of the base class
as protected (instead of private) allows
derived classes to access them
• Objects outside the class, however, cannot
access them (same as private)
Warning: call by reference
vs. call by value
• If the object of the derived class is passed by
reference, everything works fine.
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: