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:

console shot

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

guy with blackboard

Prevent Point Loss