I have a question I would like clearing up for me. There seems to be so many terms for 'composition' (i.e. aggregation is another) and I get confused between them.
What is meant precisely by 'composition' and/or 'aggregation'? Here are two different examples. Is it A or B? Or both? Or neither?
Example A
I have a class Car. And I produce a class SportsCar which has Car as a public member, like so:
Code:
class SportsCar
{
public:
Car carObj;
};
Therefore SportsCar is a composite. Is this right? Or is it the slightly more sophisticated example B, below:
Example B
My Car class now has a method for returning the number of wheels, like so:
Code:
int Car::NumOfWheels() const;
Now I could build my SportsCar class so that it inherits from Car. However, I chose not to, but instead I do the following:
Code:
class SportsCars
{
private:
Car pCar;
public:
int NumOfWheels() const;
};
Notice I has placed the Car object as a private member & reproduced its method(s) explicitly in SportsCar public. The implementation of SportsCar::NumOfWheels can be just a pass through to pCar, like so:
Code:
int SportsCar::NumOfWheels() const
{
return pCar.NumOfWheels();
}
Is this what is meant by composition?
I ask this question because I often hear that 'VB doesn't support inheritance, but supports aggregation instead.' Example B seems a way [albeit a laborious one] of replicating some features of inheritance.