Advanced Inheritance 563
16
implemented in terms of a PartsList, just as would have been the case with aggrega-
tion. The private inheritance is just a convenience.
Listing 16.6 demonstrates the use of private inheritance by rewriting the PartsCatalog
class as privately derived from PartsList.
LISTING16.6 Private Inheritance
0: //Listing 16.6 demonstrates private inheritance
1: #include <iostream>
2: using namespace std;
3:
4: // **************** Part ************
5:
6: // Abstract base class of parts
7: class Part
8: {
9: public:
10: Part():itsPartNumber(1) {}
11: Part(int PartNumber):
12: itsPartNumber(PartNumber){}
13: virtual ~Part(){}
14: int GetPartNumber() const
15: { return itsPartNumber; }
16: virtual void Display() const =0;
17: private:
18: int itsPartNumber;
19: };
20:
21: // implementation of pure virtual function so that
22: // derived classes can chain up
23: void Part::Display() const
24: {
25: cout << “\nPart Number: “ << itsPartNumber << endl;
26: }
27:
28: // **************** Car Part ************
29:
30: class CarPart : public Part
31: {
32: public:
33: CarPart():itsModelYear(94){}
34: CarPart(int year, int partNumber);
35: virtual void Display() const
36: {
37: Part::Display();
38: cout << “Model Year: “;
39: cout << itsModelYear << endl;
40: }
41: private:
42: int itsModelYear;