By the end of Chapter 13 you should understand the following concepts:
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.
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."
NOTE:
Substitute RatedPlayer for RealPlayer in the last paragraph on page 572.
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.
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.
This is the last section for Chapter 13. It reviews all we've learned so far about Classes.
This article is courtesy of www.os2ezine.com. You can view it online at http://www.os2ezine.com/20031216/page_5.html.