The StockItem class

Now let's start on our first class definition, which is designed to help solve the problem of maintaining inventory in a small grocery store. We need to keep track of all the items that we carry, so we're going to define a class called StockItem. The StockItem class, like other classes, is composed of a number of functions and variables. As I suggested earlier, to make this more concrete, think of something like Lego™ blocks, which you can put together to make parts that can in turn be used to build bigger structures. The smallest Legos are the native types, and the bigger, composite ones are class types.

For the compiler to be able to define an object correctly, we'll have to tell it the names and types of the member variables that will be used to store the information about each StockItem; otherwise, it wouldn't know how much memory to allocate for a StockItem object.

To figure out what member variables a StockItem needs, we must consider what information it will require to keep track of its corresponding item in the stock of the store. After some thought, I've come up with the following list of member variables:

  1. The name of the item (m_Name),

  2. The number in stock (m_InStock),

  3. The distributor that we purchase it from (m_Distributor),

  4. The price we charge (m_Price), and

  5. The item number, or UPC (m_UPC).

What I mean by “item” is actually something like "chunky chicken soup, 16 oz.", rather than a specific object like a particular can of soup. In other words, every can of soup with the same item number is considered equivalent to every other can of soup with the same item number, so all we have to keep track of for each item can be described by the above data. For the item number, we'll use the Universal Product Code (UPC), which is printed as a bar code on almost every product other than fresh produce; it's a 10-digit number, which we'll represent as a string for convenience.

Susan took me to task about the notion of a StockItem object vs. a specific object like a particular can of soup:

Susan: When you say "rather than a specific object", how much more specific can you get than "chunky chicken soup, 16 oz."?

Steve: Each can of chunky chicken soup is at least slightly different from every other one; at least they are in different places.

Let's recap what we know about a StockItem so far. We need a member variable in the StockItem class definition for each value in the above description: the name of the item (m_Name), its price (m_Price), the number of items in stock (m_InStock), the name of the distributor (m_Distributor), and the UPC (m_UPC) of the item. Of course, merely storing these data isn't very useful unless we can do something with them. Therefore, objects of the StockItem class also need to be able to perform several operations on their data; we'll start by giving them the ability to display their contents. Figure 6.1 illustrates a very simple way that this class might be used.

Figure 6.1. The initial sample program for the StockItem class (codeitemtst1.cpp)
#include <iostream>
#include <string>
#include "item1.h"
using namespace std;

int main()
{
   StockItem soup;

   soup = StockItem("Chunky Chicken",32,129,
   "Bob's Distribution","123456789");

   soup.Display();

   return 0;
}

This program defines a StockItem named soup with no initial data specified, tells it to assign itself some data, asks it to display itself via a function called Display, and finally terminates normally. By the time we're done with this chapter, you'll understand exactly how every operation in this program is performed by the StockItem class. Before we get too deeply into this particular class, however, we should look at the functions that almost all classes have in common. First, let's define some more terms we'll need for the discussion.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset