SUMMARY

This chapter covered the principal ideas involved in using inheritance.

You have now gone through all of the important language features of C++. It’s important that you feel comfortable with the mechanisms for defining and deriving classes and the process of inheritance. Windows programming with Visual C++ involves extensive use of all these concepts.

EXERCISES
1. What’s wrong with the following code?
2. Suppose you have a class CBird, as follows, that you want to use as a base class for deriving a hierarchy of bird classes:
class CBird
{
protected:
   int wingSpan;
   int eggSize;
   int airSpeed;
   int altitude;
public:
   virtual void fly() { altitude = 100; }
};
Is it reasonable to create a CHawk by deriving from CBird? How about a COstrich? Justify your answers. Derive an avian hierarchy that can cope with both of these birds.
3. Given the following class,
class CBase
{
protected:
   int m_anInt;
public:
   CBase(int n): m_anInt(n) { std::cout << "Base constructor\n"; }
   virtual void Print() const = 0;
};
what sort of class is CBase and why? Derive a class from CBase that sets its inherited integer value, m_anInt, when constructed, and prints it on request. Write a test program to verify that your class is correct.
4. A binary tree is a structure made up of nodes, where each node contains a pointer to a “left” node and a pointer to a “right” node plus a data item, as shown in Figure 9-6.
The tree starts with a root node, and this is the starting point for accessing ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.