Section 3 - Using the FloatStack Class
4B.3.1 Our Client
Our client is very simple because we have provided it with a beautiful FloatStack class. Have a look:
// main method --------------------------------------- int main() { FloatStack fstk; float f; fstk.push(1.1); fstk.push(2.2); fstk.push(3.3); fstk.push(4.4); for (int k = 0; k < 5; k++) if (fstk.pop(f)) cout << f << " "; else cout << "(empty stack) "; cout << endl; return 0; }
And here is the output:
4B.3.2 Other Types of Stacks
Now that we've done this with a float type, can you do the same thing with some other type? How about a stack of strings? Or Galaxies?
Here are some hints to help you:
- You don't really need a show() method in your derived Node class (StringNode, GalaxyNode, etc.) because that is only needed for demonstration of the Node class. We only demonstrate the Node class at the client level as an intermediate step. Note that we never used the show() method in the final Stack application.
- When you derive your GalaxyNode or StringNode from the StackNode, you only need to add a string or Galaxy data item to the class and then supply a trivial constructor. The FloatNode we did is a perfect template. Short and precise.
- When you derive your GalaxyStack or StringStack from the Stack, you only have to override the destructor, push() and pop() methods.
- The push() and pop() methods you write will take a string, Galaxy or whatever your data type of interest is as a parameter. You have to new (in push()) and delete (in pop()) the Nodes to hold your data. Again, the template I gave you for FloatStack works with practically no change for any data type!
You will see that there are other ways to structure this Stack data structure. We could use standard STL stack templates or create our own template classes, and we will do both these things shortly. However, there are situations in which creating the classes from the ground up is required. Furthermore, when in development, you may want to create specific classes manually, and later "templatize" these classes to make them generic. These fancy words merely mean that what you have just learned is not made irrelevant by the more powerful techniques coming up. If you are going to be a professional programmer you need to know how to generate lists and stacks from scratch as well as use and build generic tools in the STL library. So master this now, but be ready for some new techniques in a couple weeks.
This was a more challenging example of inheritance than the first one with Phones and Extensions, I will admit. It had two base classes and two classes that were derived from them. Also it made use of dynamic memory allocation and deletion, which added to the complexity. However, this is not over your head. Furthermore, it represents the kind of logic and thinking that goes into real C++ programs at the most technical level.
If you can understand this lecture, you have made it past the hardest part of this course, and you can call yourself a real C++ programmer.
But don't drop the course, just yet. The fun is about to begin.
See you next week.
Prevent Point Loss
- Use function chaining. Instead of duplicating code from your base class, or worse, omitting important code, use function chaining in any derived class function that needs to initialize or operate on base class data. This should always be done in constructors, and is also usually needed in derived class methods that override base class methods. (1 - 2 point penalty)
- Do not duplicate data or methods in derived classes. If you have a member in a base class, there is no reason to duplicate that member in the derived class. The same goes for methods (except you may have overriding methods in your derived class). (3 - 4 point penalty)