Освой самостоятельно С++ за 21 день.
Шрифт:
42: int itsModelYear;
43: };
44:
45: CarPart::CarPart(int year, int partNumber):
46: itsModelYear(year),
47: Part(partNumber)
48: { }
49:
50:
51: // *********** Класс AirPlane Part **********
52:
53: class AirPlanePart : public Part
54: {
55: public:
56: AirPlanePart:itsEngineNumber(1){ }
57: AirPlanePart
58: (int EngineNumber, int PartNumber);
59: virtual void Display const
60: {
61: Part::Display;
62: cout << "Engine No.: ";
63: cout << itsEngineNumber << endl;
64: }
65: private:
66: int itsEngineNumDer;
67: };
68:
69: AirPlanePart::AirPlanePart
70: (int EngineNumber, int PartNumber):
71: itsEngineNumber(EngineNumber),
72: Part(PartNumber)
73: { }
74:
75: // ************
76: class PartNode
77: {
78: public:
79: PartNode (Part>>);
80: ~PartNode;
81: void SetNext(PartNode * node)
82: { itsNext = node; }
83: PartNode * GetNext const;
84: Part * GetPart const;
85: private:
86: Part *itsPart;
87: PartNode * itsNext;
88: };
89: //Выполнение PartNode...
90:
91: PartNode::PartNode(Part* pPart):
92: itsPart(pPart),
93: itsNext(0)
94: { }
95:
96: PartNode::~PartNode
97: {
98: delete itsPart;
99: itsPart = 0;
100: delete itsNext;
101: itsNext = 0;
102: }
103:
104: // Возвращает NULL NULL, если нет следующего узла PartNode
105: PartNode * PartNode::GetNext const
106: {
107: return itsNext;
108: }
109:
110: Part * PartNode::GetPart const
111: {
112: if (itsPart)
113: return itsPart;
114: else
115: return NULL; //ошибка
116: }
117:
118:
119:
120: // ************ Класс Part List ************
121: class PartsList
122: {
123: public:
124: PartsList;
125: ~PartsList;
126: // Необходимо, чтобы конструктор-копировщик и оператор соответствовали друг другу!
127: void Iterate(void (Part::*f)const) const;
128: Part* Find(int & position, int PartNumber) const;
129: Part* GetFirst const;
130: void Insert(Part *);
131: Part* operator[](int) const;
132: int GetCount const { return itsCount; }
133: static PartsList& GetGlobalPartsList
134: {
135: return GiobalPartsList;
136: }
137: private:
138: PartNode * pHead;
139: int itsCount;
140: static PartsList GiobalPartsList;
141: };
142:
143: PartsList PartsList::GlobalPartsList;
144:
145:
146: PartsList::PartsList:
147: pHead(0),
148: itsCount(0)
149: { }
150:
151: PartsList::~PartsList
152: {
153: delete pHead;
154: }
155:
156: Part* PartsList::GetFirst const
157: {
158: if (pHead)
159: return pHead->GetPart;
160: else
161: return NULL; // ловушка ошибок
162: }
163:
164: Part * PartsList::operator[](int offSet) const
165: {
166: PartNode* pNode = pHead;
167:
168: if (!pHead)
169: return NULL; // ловушка ошибок
170:
171: if (offSet > itsCount)
172: return NULL; // ошибка
173:
174: for (int i=0;i<offSet; i++)
175: pNode = pNode->GetNext;
176:
177: return pNode->GetPart;
178: }
179:
180: Part* PartsList::Find(
181: int & position,
182: int PartNumber) const
183: {
184: PartNode * pNode = 0;
185: for (pNode = pHead, position = 0;
186: pNode!=NULL;
187: pNode = pNode->GetNext, position++)
188: {
189: if (pNode->GetPart->GetPartNumber == PartNumber)
190: break;
191: }
192: if (pNode == NULL)
193: return NULL;
194: else
195: return pNode->GetPart;
196: }
197:
198: void PartsList::Iterate(void (Part::*func)const) const
199: {
200: if (!pHead)
201: return;
202: PartNode* pNode = pHead;
203: do
204: (pNode->GetPart->*func);
205: while (pNode = pNode->GetNext);
206: }
207:
208: void PartsList::Insert(Part* pPart)
209: {
210: PartNode * pNode = new PartNode(pPart);