OS/2 eZine - http://www.os2ezine.com
December 16, 2003
Terry Norton is originally from California, moving to Vermont in 1984. His field of expertise is electrical engineering, having worked in electronics from 1969 until 1990. Starting in the Air Force for 6 years, then becoming Plant Engineer at Econco Broadcast Service in Woodland, CA. Upon moving to Vermont, he worked at Joslyn Defense System working in the field of EMP, Electro-Magnetic Pulse. In 1990 he retired from full time electronics to enter, along with his wife Lorraine, the field of helping the handicapped for an agency of the State of Vermont. Terry's first venture into the computer world was building his first 286 computer running DOS. He started using using OS/2 when version 2.0 was released. He's stayed with OS/2 ever since. At age 54, he now wishes to learn computer programming, a goal he has had for a number of years. Not necessarily as a career move, but for the self-satisfaction and love of OS/2 and eComstation.
If you have a comment about the content of this article, please feel free to vent in the OS/2 e-Zine discussion forums.

There is also a Printer Friendly version of this page.

Previous Article
Next Article

Advertise with OS/2 e-Zine

C++ Lesson 6: Class Inheritance

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 modeling 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 behavior 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 behavior. 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.

Previous Article
Next Article

Copyright (C) 2003. All Rights Reserved.