C++ Class Part 6

From EDM2
Jump to: navigation, search
C++ Class / Part
1 2 3 4 5 6

By Terry Norton

Welcome to C++ lesson 6 about Class Inheritance. Due to the holiday period, and home projects totally unrelated to computers, this is a very small lesson. You'll also notice I haven't written a whole lot. The book is pretty straight forward so I didn't add too much.

Chapter 13 Class Inheritance

Objectives for Chapter 13

By the end of Chapter 13 you should understand the following concepts:

  • Class inheritance, what it means, and why and how it is used
  • Building base classes for use in derived classes
  • How functionality is added to a derived class while not taking away from the base class
  • Strategies for effective use of base and derived classes
  • Public, protected, and private class members and when to use each
  • The members that are inherited and those that are not
  • Taking full advantage of inheritance by modelling it on an is-a relationship
  • How the compiler decides what methods to use
  • How objects are created and destroyed and how to code those functions
  • virtual functions: control binding explicitly
  • Typical implementations of virtual functions
  • Using pure virtual functions to create an abstract base class

Here's where we get a BIG hint about the benefits of OOP programming. As you start reading chapter 13 you'll begin to see the book talking about eCS and OS/2. No, they aren't specifically mentioned, but it sure sounds like it. On page 568, right in the middle of the page, read the paragraph that begins with "Of course,..."

Deriving a Class

I find it slightly odd that Figure 13.1 gives a visual indication for deriving a Class using a bank account, while the text talks about table tennis players. Oh well. At least you can see that a derived Class object encloses the base Class object within it.

What immediately comes to mind is XWorkPlace of Object Desktop. One of their features is enhancing folders. They make the standard eCS folder (folder class) look better and provide more data about the folders contents. As a programmer, what you are doing is simply wrapping your new code around a standard eCS Class. You will soon see that you can even change the behaviour of the base Class to suit your needs.

Constructors: Access Considerations

Take note of the very first sentence: "A derived class does NOT have direct access to the private members of the base class; it has to work through the base class members."

Substitute RatedPlayer for RealPlayer in the last paragraph on page 572.

Polymorphic Public Inheritance

This section gets into changing the base Class behaviour. The first, most obvious, way to change how a method behaves is to create a new method (function) for the derived Class that has the same method name as the function in the base Class. This way, when a derived object's method is called (sent a message), the newer method is used instead of the older one of the base Class.

OK, this is great as long as you use objects created on the stack. A problem arises when we have objects created on the HEAP, or Free Store memory because we're dealing with pointers and references. Take the two types of accounts: Brass and BrassPlus. The bank has no idea which type of account will be will be used or created during the business day as the program is running, neither does the program creator.

As the programmer, at compile time, you have no way of knowing in advance what type of object will be created by the user. To keep your code simple, either type of account, Brass or BrassPlus, will create a pointer or reference to a Brass Class. Huh? Create a BrassPlus object and have it be a pointer or reference of a Brass Class?? Yes, you can do this since the BrassPlus Class is derived from the Brass Class.

Brass * pacct = new BrassPlus;

This says a Brass pointer points to a BrassPlus object.

Virtual methods defined in the Brass Class allows the Brass pointer to call the BrassPlus methods instead of the Brass methods.

At the top of page 594 is some code that shows an array of pointers of type Brass. This array holds objects of the Brass Class and the BrassPlus Class. Virtual methods are used to make sure the correct method is used for each object.

Inheritance and Dynamic Memory Allocation

When using new and delete, you will need to define a destructor, a copy constructor, and an assignment operator. Stuff we've already learned. The main point to note begins on page 611 in Case 2. Basically, base Class methods, in this case the copy constructor and the assignment operator methods, can use references from any Class derived from the base Class.

Class Design Review

This is the last section for Chapter 13. It reviews all we've learned so far about Classes.