C++ Class Part 5

By Terry Norton

Welcome to Lesson 5 of our C++ class for beginners. This lesson is pretty big covering Chapters 9-12. In this lesson we finally study Classes and Objects (Chapters 10, 11, 12), the heart of Object Oriented Programming (OOP), at least to me.

Chapter 9 - Memory Models
You'll notice I didn't quite get the name for Chapter 9 correct. I left out namespaces. The only reason I did this is because OpenWatcom doesn't support this feature yet and I didn't want to study material we can't use at this time. But, if you are instead using the gcc compiler, then you can use namespaces. Since this class is semi-officially using the OpenWatcom compiler, I am just going to delay studying namespaces until a later time, or when every one of us is using gcc instead of OpenWatcom. OK, let's continue.

Objectives for Chapter 9
By the semi-end of Chapter 9, you should understand the following concepts: I'm going to temporarily jump ahead to page 352. I want to present a point about compilers and linking before getting into separate file compiling. On page 352 is a note titled Real World Note: Multiple Library Linking
 * Breaking a program into separate files and assembling a C++ program
 * Storage Classes and how they allow finer control of variables in a program
 * Scope and linkage rules

When reading programming newsgroups and forums, I always wondered a lot what all this recompiling talk was about concerning libraries. If libraries were already compiled, what the heck was the purpose of recompiling them. Sure seemed like a bunch of extra work for nothing. Of course I was (am) very ignorant about compilers. It was the "name mangling" done by the compiler which makes recompiling necessary. Remember I told you in Chapter 8 to keep this stuff in mind? This note explains this pretty good.

OK, back to the beginning of Chapter 9.

Separate Compilation
NOTE:

The book keeps referring to Listing 7.11. This is incorrect, it should be Listing 7.12

Reviewing Listing 7.12, the program describes (declares) two structures. One is called polar and the second is called rect. Remember, this creates two new types: rect and polar, just like int and char are types.

Separating the code into a file that contains main, and another file that contains the functions rect_to_polar</tt></tt> and show_polar</tt>, means each file would need to have the structures polar</tt> and rect</tt> declared in each file since each file uses the polar</tt> type and the rect</tt> type.

Look in main</tt> and you see variables created: a rect</tt> type called rplace</tt>, and a polar</tt> type called pplace</tt>.

Look in the rect_to_polar</tt> declaration or definition and you see the rect</tt> type as a parameter. In the definition you see an answer</tt> variable of type polar</tt> being created.

Look in the show_polar</tt> declaration or definition and you see the polar</tt> type as a parameter.

Having to declare these structures in both files makes it difficult if you ever need to change these structures because they are in more than one file, not good. The structures should only be declared in one place.

This is where the <tt>header</tt> file enters the picture. Place the structure declarations in yet another file called a <tt>header</tt> file. Then use <tt> #include </tt> to include the header file them in both of our code files. Now if you ever need to modify the structures, do so in the header file. We have been using header files already, mainly the <tt>iostream</tt> header file like this:
 * 1) include

All we're doing now is making our own header files and including them in our source code.

NOTE:

Be sure to read the note on page 350 called Header File Management. This explains how header files are, or aren't, included during compile time. You'll also notice the line
 * 1) define COORDIN_H_</tt>

If you ever wondered where the name <tt>COORDIN_H_</tt> comes from, it's just a name made up by the programmer. Take a look at any of the header files included with your compiler and you'll countless examples.

Storage Duration, Scope, and Linkage
I won't add anything here. The book explains this well. However, remembering all this info will make your head swim. I'd say this is a reference section of the book. If you need the info, look it up as you need it. It will eventually be remembered as you use it more often.

Chapter 10 - Objects and Classes
Here we are, finally. This is what we've been waiting to learn. All the previous material, learning syntax, general concepts, pointers, references, functions, variables, etc., has all been for using objects to accomplish a task. There are several chapters dealing with objects and classes because that's what C++ is all about.

Objectives for Chapter 10

 * Object-oriented programming concepts of abstraction, encapsulation, and data hiding
 * How these OOP concepts benefit the programmer and user
 * C++ Classes: the mechanics of declaring and defining them
 * <tt>const</tt> member functions and the protection of data and functions
 * Public and private Class members, both data and functions
 * Class scope to understand the visibility of Class members
 * Creating objects and how their methods are used
 * Class constructors and destructors for creating and releasing class objects
 * Self identity and the <tt>this</tt> pointer to Class objects
 * Using arrays of objects to represent and work with collections of objects
 * Abstract data types to represent more general objects

Procedural and Object-Oriented Programming
I already expressed my own views about procedural and OOP in earlier lessons, so no since repeating myself.

Abstraction and Classes
I tried reading this section and in the end, sat there scratching my head. Say what? Abstraction is what? I finally went looking on the web for a definition a little more definite for a beginning C++ programmer. Believe it or not, most of the results of my search weren't any better, but I am able to tell you, in just a few sentences, what the big deal is all about.

Abstraction is the process of taking away or removing characteristics from "something" in order to reduce it to a set of essential characteristics. Objects we want to create only have to contain the data we need to accomplish the task. The object that remains is a representation of the original, with unwanted detail omitted.

Abstraction, sure sounds like a difficult way to describe something that is real life is very simple, but I wasn't consulted when words were chosen as definitions for C++.

In the Listing 10.1, <tt>stocks.cpp</tt>, there are certain things we need to know about a stock such as the share price (value), how many we own. Things we don't need to know about a stock is when was the stock certificate printed, what color of ink was used to print it, what kind of paper is it, etc. All these things, plus more, are all part of a stock, the original object. But in a stock program we need only a tiny portion of what a stock is, so what we have is an abstract of a stock.

Type
We just covered this in the last chapter while dealing with structures. When we defined a structure, we created a new <tt>type</tt> for our enjoyment and use. Just like <tt>int</tt> and <tt>char</tt> are <tt>types</tt>, they just happen to be supplied as part of the programming language.

Here it comes:

The Class (yeah!!!)
This is the Holy Grail of C++. We create our Classes, like we created structures (sorta), so suit our need to describe what the objects we want to make. Remember, a Class is like the manual that tells you how to make a car. The manual isn't a car, it defines what a car will be when we do decide to make one, the object.

The example in the book is a stock. I'm sure there must be some laws that say what a stock certificate is and what it has to include. However, we aren't going to make stocks with a program, we just want to use some of its characteristics. So once again, we will use an abstract of a stock, just using the parts we need, like its value.

Like a structure, the Class defines the data an object can hold. Unlike a structure, a Class can also define the functions that are allowed to work with the data, and those functions are also part of the object. One nice, neat little package. The only way your program can (should) access the data in an object is through its own contained functions, also called class methods or member functions. Actually, you can get at the data if you really want without member functions (class methods), but that's not a good idea. But it does show that you're the boss and can do whatever you darn well please.

I would bet the word <tt>method</tt> was chosen so programmers would know the difference between a function that is part of a Class and its objects, and a function you have elsewhere in your program that isn't part of some Class. Now that makes sense to me. I'm sure I just have some sort of mental block about the word abstraction, but I sure you can handle it much easier than I can.

Public Interface is something you will hear a lot, so let's see what it means, especially since you have to define it for a Class. The Public Interface (or just Interface) lets the world know the data an object can store, and the methods (functions) available to access or work with the data. The Interface is all a user of the Class needs to know. They don't need to know the specific details of how the methods actually do their work. Take a car for example. You are familiar with the Interface of a car, such as how to make it go. All you need to know is that pushing on the gas pedal makes the car go. Do you need to know how the pedal connects to the engine, or whether it's connected to a carburetor, or to a fuel injection valve, or to an alien anti-grav unit?

Implementing Class Member Functions
This whole section is very well explained, so I won't add any confusion. I will just point out that on page 405, second to last sentence:

"Calling a member function is what OOP languages term sending a message."

And I'll bet you thought the whole idea of OOP would be hard. The only thing hard about C++ is learning the basic rules of syntax, but that's no different than learning any kind of language, even English. Syntax is one of those things that you learn more as you use it. If you can't remember something, look it up, no big deal. Also, once you know how objects are these clever, encapsulated entities, using objects makes programming so much easier compared to procedural programming. Yes, C is required for things like device drivers, but for user applications, OOP is great. Did I ever mention I like object-oriented programming?

Using a Class
Using a <tt>Class</tt> is almost the same as using a <tt>Struct (structure)</tt>, which we covered in the last lesson. I included, to save you some typing, and compiled Listing 10.3, <tt>stocks.cpp</tt>. I made the changes so it would compile using OpenWatcom. In the <tt>main</tt> function I had to change <tt>ios_base::</tt> to <tt>ios::</tt>. I also include some extra comments in case a little review is necessary about pointers and strings.

// stocks.cpp class Stock { private: char company[30]; int shares; double share_val; double total_val; void set_tot { total_val = shares * share_val; } public: void acquire(const char * co, int n, double pr); void buy(int num, double price); void sell(int num, double price); void update(double price); void show; }; void Stock::acquire(const char * co, int n, double pr) // Note the pointer parameter { strncpy(company, co, 29);      // truncate co to fit if needed company[29] = '\0';           // make the end of the string the NULL character if (n < 0) {  cerr << "Number of shares can't be negative; " << "shares set to 0.\n"; shares = 0; } else shares = n; share_val = pr; set_tot; } void Stock::buy(int num, double price) { if (num < 0) {  cerr << "Number of shares purchased can't be negative. " << "Transaction aborted.\n"; } else {  shares += num; share_val = price; set_tot; } } void Stock::sell(int num, double price) { if (num < 0) {  cerr << "Number of shares purchased can't be negative. " << "Transaction aborted.\n"; } else if (num > shares) {  cerr << "You can't sell more than you have! " << "Transaction is aborted.\n"; } else {  shares -= num; share_val = price; set_tot; } } void Stock::update(double price) { share_val = price; set_tot; } void Stock::show { cout << "Company: " << company << " Shares: " << shares << "\n" << " Share Price: $" << share_val << " Total Worth: $" << total_val << "\n"; } int main { Stock stock1;                           // Create a Stock object called stock1 // Remember, Stock is a Type stock1.acquire("NanoSmart", 20, 12.50); // Literal string, see page 273 to refresh // how the first element of a string is actually // a pointer (address) for this array cout.setf(ios::fixed);                 // #.## format cout.precision(2);                     // #.## format cout.setf(ios::showpoint);             // #.## format stock1.show;                         // Send a message to the stock1 object stock1.buy(15, 18.25);                 // Send a message...  stock1.show;                          // Send a message...  stock1.sell(400, 20.00);                // Send a message...  stock1.show;                          // Send a message... cin.get; return 0; }
 * 1) include
 * 2) include   // cstring doesn't exist with OW

Now that you have some code you can copy and paste, doing the other code listings will be a bit easier. You'll just need to make necessary modification.

Our Story to Date
Read this section and understand it. This is the key to using Classes and their objects. Everything discussed here we just did in Listing 10.3. The author switched to using a different Class name, Bozo, and an object named Bozetta, to illustrate his point, so if you don't like Bozo, then substitute Stock instead, the object we just created, stock1.

Class Constructors and Destructors
I can remember being really confused about Constructors when I was learning Object REXX, but it is quite simple. I just have a bad habit of thinking things are harder than they are.

The constructor, a Class function, is automatically run (called) when the object is created. The best use of a constructor that I used with Object REXX was reading stored data from a file. When my program would start, if there was data in a file, a new object would be created which made the constructor function read the data from the file to initialize the object's variables. Objects kept being created until all the data from the file had been retrieved.

How does the object know which member function to run when it's created? The function is named the same as the Class the object is created from. For our stock program, since the Class is the <tt>Stock</tt> type, the constructor is named <tt>Stock</tt>.

The Default Constructor
The constructor that is called when you don't provide any Constructor. Note these points:

No arguments at all like this: Stock; or with arguments with default values assigned, something like this: Stock(const char * co = "Error", init n = 0, double pr = 0.0)
 * The compiler provides one if you don't provide any constructors.
 * If you do provide any constructors at all, the compiler will NOT automatically provide a default constructor, so also code in a default constructor with either:

Just to muddy the waters a little bit, even if you only code in the default constructor in your declaration like this, <tt>Stock;</tt>, you can still set the initial values for the member variables in the function definition file something like this:

Stock::Stock        // this is the default constructor, it also has a block of code {  strcpy(company, "no name"); shares = 0; share_val = 0.0; total_val = 0.0; }

This is the preferred way to set up a default constructor. Read the tip on page 414.

Improving the Stock Class
Not much to add here except notice the next three titles of the subsections:
 * The Header File
 * The Implementation File
 * The Client File

The program is separated into specific files.

Knowing Your Objects: The <tt>this</tt> Pointer
The <tt>this</tt> pointer is simply the address of an object. In Listing 10.5, <tt>&stock1</tt> would be the address of <tt>stock1</tt>. Well, you can also see the address of <tt>stock1</tt> using the <tt>this</tt> pointer while calling a <tt>stock1</tt> function. You can test this for yourself with some minor changes to the code. Here is the change I made in <tt>main</tt> :

cout << "Using constructors to create new objects\n"; Stock stock1("NanoSmart", 12, 20.0);           // Syntax 1 cout << "The address of stock1 is: " << &stock1 << "\n"; stock1.show;                                 // Send a message to the stock1 object Stock stock2 = Stock ("Boffo Objects", 2, 2.0); // Syntax 2 cout << "The address of stock2 is: " << &stock2 << "\n"; stock2.show;                                 // Send a message to the stock1 object

and here's the change to <tt>show</tt>:

void Stock::show { cout << "Company: " << company << " Shares: " << shares << "\n" << " Share Price: $" << share_val << " Total Worth: $" << total_val << "\n"; cout << "This address is: " << this << "\n"; }

Here's the relevant part of the output when I run Listing 10.5:

Using constructors to create new objects Constructor using NanoSmart called The address of stock1 is: 0x42ec4   <-- address of stock1 Company: NanoSmart Shares: 12 Share Price: $20.00 Total Worth: $240.00 This address is: 0x42ec4     <-- address of stock1 while in show Constructor using Boffo Objects called The address of stock2 is: 0x42efc   <-- address of stock2 Company: Boffo Objects Shares: 2 Share Price: $2.00 Total Worth: $4.00 This address is: 0x42efc     <-- address of stock2 while in show

If you want to run this yourself, you may have to add a <tt>cin.get</tt> midway in the <tt>main</tt> code, otherwise the output may scroll off the screen and you won't see the first part of the output.

An Array of Objects
This section shows the program creating 4 objects, but without any names. This was another mental block I had when learning object REXX. How do you deal with the objects if they aren't named. It was like dealing with ghosts. How do you work with something you can't see? Well, I did get over this eventually. What I learned is that a name doesn't do anything to help working with objects.

Take the stocks program for example. At first the examples provide names for each object, like <tt>stock1</tt> and <tt>stock2</tt>. But that's not what's important. In Listing 10.9, <tt>usestok2.cpp</tt>, none of the objects have names, so it is kind of like they exist in never-never land, but yet there's no problem working with them. Why? Because everything you need to know about an object is contained in it. We work with the data contained in the object, not some name we might want to give it.

OK, so we work with the data, how do you work with a specific object then? You will discover that just about anything you do with data, whether you do procedural programming or OOP, will involve some kind of sorting, searching, selecting, filtering, whatever. For instance, you might have stored a bunch of data in an array, but you only want to see data that meets certain criteria, so you program some filter, run the data through the filter and display the result in a list box. From there the user can make further selections.

You can store objects in an array. Actually you'll eventually learn to store objects in several available container class objects. An object is a container of stored data as well. All data gets stored somewhere no matter how you program. So when you want to display certain data, contained objects, in a list box, it won't be the objects the user sees, it's the data from the object that gets displayed. To get the list box filled with the particular data you want, you'll run some sort of filter on one or more of the objects data members, which you retrieve by sending each object the same message.

Please note the last sentence on page 431: "One thing to note is how most of the work goes into designing the class. Once that's done, writing the program itself is rather simple."

I love object programming, did I ever mention that?

Objectives for Chapter 11
By the end of Chapter 11, you should understand the following concepts:


 * Operator overloading, the details of its use, and why it is a useful tool
 * Overloading of <tt>+</tt>, <tt>-</tt>, and <tt> * </tt> operators
 * Overloading of <tt>ostream</tt> objects, <tt>&lgt;&lgt;</tt> in particular
 * Using <tt>friend</tt> functions to extend the flexibility of Class definitions
 * State members used to add intelligence to Classes
 * Generating random numbers and the use of the system clock as a seed
 * Decisions a compiler makes in converting data and how to give it guidance
 * Conversion functions to explicitly define conversions involving objects
 * The difference between explicit and implicit conversions

Operator Overloading
A note before starting with operator overloading. This isn't anything that's required for creating Classes and using objects. Operator overloading can make coding easier once you know the material. You can accomplish your programming tasks without using overloading, however, the rest of the book's examples are going to make heavy use of operator overloading, so in a word, learn it now.

The built-in <tt>types</tt>, such as <tt>int</tt>, can make use of operators such as "+", "-", etc. But the Class <tt>types</tt> you create aren't able to use them by default. C++ does provide a way that you can use them with operator overloading.

I'm not saying you can't use C++ operators in your Class' code to do the usual things like adding or multiplying numbers with the built-in types, like adding two <tt>int</tt> types together. However if you create a Class to make <tt>time</tt> types (time objects), you aren't allowed to just add together two <tt>time</tt> types together because your compiler hasn't been told it can do such a thing. Operator overloading simply means we are going to tell our C++ compiler that a time object can be added to another time object and not produce a compiling error, and we're going to tell the operator(s) how to do it.

Let's see if I can summarize what's taking place. Let's use an overloaded <tt>+</tt> and <tt>A = B + C</tt>, which means we are adding two <tt>Time</tt> objects, <tt>B</tt> & <tt>C</tt> and assigning the result to another <tt>Time</tt> object, <tt></tt>A. You define <tt>operator+</tt> as a Time Class member function. So now when the compiler sees the <tt>+</tt> operator it checks all the types it's allowed to work with and behold, it finds that we defined in our Time Class definition that it can add two Time object together, and how to do it. So it sees that Time object <tt>B</tt> is the first object, so it calls the member function, <tt>operator+</tt> in the B object. The compiler sees that the <tt>operator+</tt> function requires an argument, so it looks at the expression <tt>A = B + C</tt> and finds that <tt>C</tt> is the other operand, so the <tt>C</tt> object is used as the argument for the <tt>B</tt> member function, <tt>operator+</tt>. You notice that the <tt>C</tt> object has the same <tt>operator+</tt> function, but in this expression it isn't called. <tt>B</tt> was first in the expression <tt>A = B + C</tt>, so it's member function was called. <tt>C</tt> is then just the argument for <tt>B</tt>'s function.

OK, that's one way to add two objects, Time objects in this case. But there's another way that doesn't use a member function. To do it another way we need a Friend.

Introducing Friends(More overloading)
To keep a linear thought process going here, only read the first two paragraphs of this section about Friends, then go to page 466 and read the section:

Overloaded Operators: Member Versus Nonmember Functions
Now to continue with the second way to do the expression <tt>A = B + C</tt>. By defining a function in the Time Class as a <tt>friend</tt>, we're making it a nonmember function. That means the compiler knows there's a way to add two Time objects together because the definition for the <tt>operator+</tt> is in the Time Class, but it's not calling a member function to do it. Since it's not a member function, the compiler doesn't call the <tt>operator+</tt> like this:

B.operator+ // this is for a member function only, not what we're doing here

Instead, the compiler can see that the two operand in <tt>A = B + C</tt> are <tt>B</tt> and <tt>C</tt>, and that they can indeed be added together, but <tt>operator+</tt> is just another function. In other words, you don't have to send any object a message to use it. When the compiler sees the <tt>+</tt> operator, it has to look at the <tt>types</tt> being added together, then go look to see if the types being added are defined somewhere as being addable. Sure enough, it finds that there's a definition in the Time Class that allows them to be added. But this time the <tt>operator+</tt> takes two arguments, both of the operands <tt>B</tt> & <tt>C</tt>.

Since this time it's not a member function, both <tt>B</tt> & <tt>C</tt> objects need to access their data explicitly. That means the code for <tt>operator+</tt> has to send messages to both objects to get the required data.

OK, now go back to page 458 and finish reading the Friend section.

A Common Kind of Friend: Overloading the <tt><<</tt> Operator
Don't burn the First Version of Overloading too deep into your brain because the Second Version of Overloading is the keeper. Also catch the note on page 464 about using <tt>friend</tt> in the Class declaration only.

I want to take the time to make sure there isn't a fuzzy spot in your brain concerning functions that return a value, such as an <tt>int</tt> value, and an object, such as the <tt>cout</tt> object.

When a function returns a value, that's easy to understand, but returning an object may drive you crazy. What do you mean return an object? An object is a particular <tt>type</tt> just like <tt>int</tt> is a particular <tt>type</tt>. An object will usually contain data and functions.

<tt>cout</tt> is an object that has a bunch of overloaded functions in it that outputs some data, usually to the screen. Each member function in the cout Class has a different signature, just like we can overload functions simply by using the same name for a function but with each argument list having different arguments, the signature of a function.

When we do something like: cout << "Send this text to the screen" << a_variable; // a cout statement

the literal string is just an argument for one of the member functions in cout. The compiler knows this is a literal string and selects the function that takes a literal string as an argument. Also in the member function is a return statement. When the function in cout finishes sending the string to the screen, the function exits with a return statement that returns itself, the cout object.

Since a <tt>cout</tt> object has been returned, the next part, <tt><< a_variable</tt> is now sent to a function in <tt>cout</tt> that accepts a variable as an argument. The compiler sees it's a variable, so the proper function is selected once again and the value that's stored at <tt>a_variable</tt> is sent to the screen.

Had the function that processed the literal string not returned the <tt>cout</tt> object, the <tt>a_variable</tt> would not have been processed because the <tt><<</tt> operator only knows how to send what follows it to a member function that's in the <tt>cout</tt> object. The compiler would have just produced an error.

So when a function returns an object, it's just providing you with this encapsulated chunk of code that you can send a message to. That's the purpose of an object, to have it do some sort of work for you.

The rest of the chapter about conversion is pretty straight forward about <tt>types</tt> being converted when an object is created. Constructors arguments accepts a specific <tt>type</tt>, but the compiler can automatically convert one type to another, such as an <tt>int</tt> to a <tt>double</tt>. Or using conversion functions to go from some object type to a built-in type.

The rest of the chapter discusses conversion. It's pretty straight forward.

Objectives for Chapter 12
By the end of Chapter 12, you should understand the following concepts:
 * Using dynamic memory with <tt>new</tt> and <tt>delete</tt> and Classes
 * How to use Classes to make the free store (heap) to hold data
 * <tt>static</tt> Class members to hold data about all the objects in a Class
 * Constructors and destructors to manage dynamic memory
 * Freeing dynamic memory versus freeing memory used by an object
 * Default member functions provided by C++ and what can go wrong
 * Deep copying in constructors and assignment operators
 * Deleting memory multiple times - why it is a problem and how to avoid it
 * Effectively using pointer to objects
 * How a queue simulation works and implementing Classes with dynamic memory
 * Nesting structures and Classes within a Class (OpenWatcom doesn't allow this)

Dynamic Memory and Classes
The review of <tt>new</tt> and <tt>delete</tt> is very well done. I will not add anything to confuse what the book already explains. At the very end of this review section is mentioned a new constructor called the copy constructor, so be sure to follow the explanation for Listing 12.3, <tt>vegnews.cpp</tt>.

Yes, these examples compile and run with OpenWatcom, however, run this program from the commandline. If you place <tt>cin.get</tt> ; before the return statement, and run from OpenWatcom, you'll miss the last few lines of output since some of the object destructors don't get called until the program ends.

The Copy Constructor
The last chapter talked about conversion, specifically how something such as an <tt>int</tt> type being assigned to a <tt>Class</tt> type becomes a <tt>Class</tt> type. Constructors handle the conversion as we define them. But what happens when conversion isn't necessary? What happens when you assign a <tt>Class</tt> type to the same <tt>Class</tt> type? There is no conversion since they're the same type. It's a straight forward copy of one object to another, and the constructors we have defined so far don't handle copying. In the <tt>StringBad</tt> situation, a copy constructor wasn't defined, so when a new <tt>StringBad</tt> type was created from data contained in another <tt>StringBad</tt> type, a default copy constructor was used that the compiler automatically created. If we want the <tt>num_strings</tt> static variable to be incremented, we need to define our own copy constructor.

Just to make sure you understand, the copy constructor is called when you copy an object to a "newly" created object. The assignment operator, discussed next, is used when you assign one object to an "existing" object.

The Assignment Operator
This is almost exactly the same as the copy constructor problem. If your program will be assigning the same type objects, you will also need to define an assignment operator.

To get the StringBad program to work correctly, add the copy constructor and assignment operator function to the header file and the class methods source file. The header file will look like this: // strngbad.h - - corrected string class definition #include #ifndef STRNGBAD_H_ #define STRNGBAD_H_ class StringBad {  private: char * str; int len; static int num_strings; public: StringBad(const char * s);                  //pointer to string StringBad & operator=(const StringBad & st); //assignment operator StringBad(const StringBad & st);            //copy constructor StringBad;                                //default constructor ~StringBad;                               //destructor friend ostream & operator<<(ostream & os, const StringBad & s); }; #endif

The New, Improved String Class
Many more overloaded operators are added to the Class definition to make dealing with objects so easy.

NOTE: In Listing 12.4, <tt>string1.h</tt>, it appears that OpenWatcom 1.0 doesn't support static class constants, so the statement: static const in CINLIM = 80; produced a compiler error message. The fix for this is provided on page 527: enum {CINLIM = 80}; //should be 80, not 90

NOTE: On page 531 is another compatibility note. OpenWatcom does need to use the second test in the <tt>if</tt> statement. I tried both tests separately to see if they would both work. The <tt> !cin </tt> did not work for me. When using just this test, an empty line did not result in the program breaking out of the <tt>for</tt> loop.

The book explains the overloaded operators very well. Also, on page 532 - 533 is a good review. However, I can't leave this section without a brief comment. If you've already got the hang of what's going on here, that's great, but I want to make sure the beginners have the big picture. Believe me, the more I get into this material the more enlightened I'm becoming as well.

All these overloaded operators may seem way too confusing, but the concept is simple. As you are aware, you can do things with <tt>int</tt> types quite easily, such as add, subtract, divide, compare, etc. The reason you can do these things is because someone else did the programming and included the necessary files so the compiler knows how to handle <tt>int</tt> types. <tt>cout</tt> and <tt>cin</tt> also know how to handle all the types that are built-in, like <tt>int</tt> and <tt>char</tt>, etc. How? Because someone else did the programming so that the <tt>cout</tt> and <tt>cin</tt> objects know how to handle these types.

It's very obvious the compiler won't know how to handle something it doesn't know about. When you make your own Classes, you are creating a new <tt>type</tt> the compiler has never seen. So you can't just add these new types together, nor can cout deal with them either. We are presently creating our own version of the String class. We have been using it instead of the built-in version, but the compiler has no idea what to do with our version.

We have been teaching the compiler how to handle our version of a <tt>String</tt> Class. We taught it to use the cout object when we defined the << operator in the Class definition. In Listing 12.6 there was a String object created called <tt>name</tt>. Go to line 14 and you see cout << name. We told the compiler that when it sees a cout object and a String object with << in the middle to go find the operator<< function in our String Class definition, defined in Listing 12.5, and to take the data at the str pointer and send it to the screen. This is the same thing somebody else did so that an <tt>int</tt> type could be sent to the screen.

So when we tell the compiler how to handle our objects with the available operators, it's called operator overloading. All we did was add another definition to the compiler's dictionary of knowledge.

Using Pointers to Objects
In Listing 12.6, <tt>sayings1.cpp</tt>, we created an array of <tt>String</tt> objects, <tt>sayings[]</tt>. To keep track of the <tt>String</tt> object containing the first string alphabetically, and the <tt>String</tt> object containing the shortest string in our array of <tt>String</tt> objects, we simply stored the array index value in two <tt>int</tt> variables called <tt>first</tt> and <tt>shortest</tt>.

Now we are going to change how we do this by using pointers instead of <tt>int</tt> variables. This means that instead of storing some index values of the <tt>sayings[]</tt> array for the corresponding <tt>String</tt> objects, we are storing the memory addresses for the <tt>String</tt> objects in two pointers.

NOTE: In Listing 12.7, <tt>sayings2.cpp</tt>, remember to change the #include statements to instead of, and instead of.

A Queue Class
C++ already comes with containers, however, here we create our own just so you can see how this particular kind of container works. Containers are pretty neat, they hold the objects we create. It's not any different that a container you have in your house called a cupboard. The cupboard container holds all your dish and glass objects.

There are several kinds of containers we can use, depending on how you want to store and retrieve your objects (data). The <tt>Queue</tt> container has the feature that you put your objects into one end of a <tt>Queue</tt>, and retrieve then from the other end. Picture the <tt>Queue</tt> as a tube. The first object you put into one end of the tube will be the first object that you can retrieve from the other end.

If you wish, think of a container as a small database, it simply stores information. The information a container holds is the references (addresses in memory) of objects.

Note: It appears that OpenWatcom doesn't support nested structures and classes. To get the Queue program to compile, I had to declare the <tt>Node</tt> structure outside of the <tt>Queue</tt> class, like this:

typedef Customer Item; struct Node { Item item; struct Node * next; };  <--- Node declaration

class Queue {...

I will include the three files for this example on the V.O.I.C.E. forum, under lesson 5. Look for <tt>queue.zip</tt>.

So what did this Queue example show? The objects and their data are created in the computer's memory. A Queue node holds the address of one object. The node also holds the address of the next node in the Queue, which holds the address of another object in the Queue. This simply repeats for all the nodes in the Queue container.

Phew! Big lesson, but well worth the effort. Have you noticed that defining Classes where most of the C++ effort is located for a program? You will find, like I did with Object REXX, that the supporting code you create is mainly for sending messages to objects to get data. This is where objects are no nice. As you develop a program you may discover that your objects may need to contain some data you hadn't planned on. No big deal. Just add the new data member to the Class definition, and the member functions to access the data. I love object programming. Did I mention this before?