class CarPart protected std vector CarPart mParts public virtual CarPa

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class CarPart
{
protected:
std::vector<CarPart*> mParts;
public:
virtual ~CarPart() {};
virtual float getCost() const = 0;
virtual void addPart(CarPart* part)
{
mParts.push_back(part);
};
virtual void removePart(CarPart* part)
{
mParts.erase(std::find(mParts.begin(), mParts.end(), part));
}
virtual float calculateFullCost() const
{
float summary = getCost();
for(int i = 0; i < (int)mParts.size(); ++i)
summary += mParts[i]->calculateFullCost();
return summary;
}
};