Section 3 - Two Base Class Examples
10A.3.1 First Base Class CellPhone
If you found the last section dizzying, this concrete example should help clear the air.
We start with base class #1, a CellPhone. It will be a technical description of the product. The members are:
- string description
- int memCapacity
- 2 features (a yes/no enum): camera and gps
There is a constructor and a show() method. Its prototype and implementation are straightforward:
Prototype for CellPhone
//class CellPhone prototype ---------------- class CellPhone { public: enum feature {no = 0, yes}; protected: string description; int memCapacity; feature camera; feature gps; public: CellPhone(string dscr = "", int mem = 100, feature f1 = no, feature f2 = no); void show(); };
We are using the enum type as we did back in the first lecture, but doing so nested in the class.
CellPhone Implementation
// CellPhone method definitions ----------------- CellPhone::CellPhone(string dscr, int mem, feature f1, feature f2) { description = dscr; memCapacity = mem; camera = f1; gps = f2; } void CellPhone::show() { cout << "Phone: " << description << " with " << memCapacity << " storable numbers. Features: "; if (camera == yes) cout << "camera "; if (gps == yes) cout << "gps"; cout << endl; }
I could have used the shorter syntax for initializing members ( : description(dscr) ) but chose to do it this way, at least for this class. You should be saying to yourself, "he didn't add very much protection in the constructor for the private data", and you are absolutely correct. Since multiple inheritance can get long, I wanted to keep the classes short and simple to reveal the issues we are studying this lesson.
10A.3.2 Second Base Class InventoryItem
For base class #2, I chose an InventoryItem. This is a description used by a stocking or tracking system. The members are:
- string description (create ambiguity with CellPhone!)
- int numInStock (how many are in stock?)
- int itemNumber (a UPS or other tracking code)
There is a constructor and a show() method, just like the CellPhone. Its prototype and implementation also easy:
Prototype for InventoryItem
//class InventoryItem prototype ---------------- class InventoryItem { protected: string description; int numInStock; int itemNumber; public: InventoryItem(string dscr = "", int numstck = 1, int itmno = 0); void show(); };
We are using the enum type as we did back in the first lecture, but doing so nested in the class.
InventoryItem Implementation
// InventoryItem method definitions ----------------- InventoryItem::InventoryItem(string descr, int numstck, int itmno ) : numInStock(numstck), itemNumber(itmno), description(descr) { } void InventoryItem::show() { cout << "Item no. " << itemNumber << " " << description << ": " << "Number in Stock: " << numInStock << endl; }
This time I did use the shorthand notation, to help you remember the different ways we can initialize things ( : description(dscr) ). As I said, this is normally bad if you want to filter bad client input.
In the next section we will combine these two base classes in a way only an advanced C++ programmer can: with multiple inheritance.