Section 5 - Full Program Listing

10A.5.1 The Multiple Inheritance Cell Phone Listing

This would be a good program to divide into multiple files, but I want you to have it in one file so there will be no problems with the headers, file names, etc.  Multiple inheritance is complicated enough as it is,  and I want to keep the number of issues down to a manageable minimum.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

//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();
};

//class InventoryItem prototype ----------------
class InventoryItem
{
protected:
    string  description;
    int   numInStock;
    int   itemNumber;

public:
    InventoryItem(string dscr = "",
    int numstck = 1, int itmno = 0);
    void show();
};

// ----------- class CellPhoneForSale -----------
class CellPhoneForSale :
public CellPhone,
public  InventoryItem
{
private:
    int price;

public:
    CellPhoneForSale(
        string dscr, int mem = 100, feature f1 = no, feature f2 = no,
	int numstck = 1, int itmno = 0,
	int price = 0.0);
    CellPhoneForSale(
	CellPhone &phone,
	InventoryItem &invitm,
	int price);
    void show();
};


int main()
{
    CellPhone   cp1("Erricson Slim", 200,
    CellPhone::yes, CellPhone::no);
    CellPhone   cp2 ("Motorola Lite", 110,
    CellPhone::yes, CellPhone::yes);
    InventoryItem   ii1("The Erricson Phone", 5, 11111);

    CellPhoneForSale   cpfs1(   cp1, ii1, 405);
    CellPhoneForSale   cpfs2("AT&T Super", 150,
                             CellPhone::no, CellPhone::yes,
                             3, 22222, 150);

    cp1.show();
    cp2.show();
    cout << endl;
    ii1.show();
    cout << endl;
    cpfs1.show();
    cpfs2.show();
}


// 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;
}

// 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;
}

// CellPhoneForSale method definitions -----------------
CellPhoneForSale::CellPhoneForSale(
    string dscr, int mem, feature f1, feature f2,
    int numstck, int itmno,
    int price) :
      CellPhone(dscr, mem, f1, f2),
      InventoryItem(dscr, numstck, itmno)
    {
        this->price = price;
    }
CellPhoneForSale::CellPhoneForSale(
    CellPhone &phone,
    InventoryItem &invitm,
    int prc) :
        CellPhone(phone),
        InventoryItem(invitm),
        price(prc)
{
}

void CellPhoneForSale::show()
{
    cout <<"\n-------- Phone for sale: ------\n";
    CellPhone::show();
    InventoryItem::show();
    cout << "Retail price: " << price << endl;
    cout <<  "-------------------------------\n";
}

And that's the story.