Composition builds complex objects from simple ones by making one class contain another rather than deriving from it. A car has an engine. It is not an engine. The container owns the contained object, it forwards the calls that it cares about, and it is free to change its internals later. Composition is usually the better first choice in C++, and the reason is that it has no inheritance traps.
The previous lesson in this module covered "How Does const Correctness Harden a Class?", and this lesson is the last one in the Classes and Objects module.
Containment is the simplest relationship
A member of a class type is containment. The outer object owns the inner one, constructs it, and destroys it.
#include <iostream>
#include <string>
class Engine {
public:
explicit Engine(int horsepower) : horsepower_(horsepower) {}
int horsepower() const {
return horsepower_;
}
std::string describe() const {
return std::to_string(horsepower_) + " hp engine";
}
private:
int horsepower_;
};
class Car {
public:
Car(const std::string& model, int horsepower)
: model_(model), engine_(horsepower) {}
std::string describe() const {
return model_ + " with a " + engine_.describe();
}
private:
std::string model_;
Engine engine_;
};
int main() {
Car car("Falcon", 240);
std::cout << car.describe() << '\n';
return 0;
}The output shows the outer object combining its own data with the data of the inner object.
Falcon with a 240 hp engineAnd the Car constructor takes an int rather than an Engine, so the caller never has to build the engine separately. That is a design choice worth making deliberately, because it keeps the parts of the API that the caller does not care about out of the way.
Forwarding and accessors
The contained object is private by default, so the outer class decides what to expose. Sometimes that means forwarding a method straight through.
#include <iostream>
#include <string>
class Engine {
public:
explicit Engine(int horsepower) : horsepower_(horsepower) {}
int horsepower() const { return horsepower_; }
void boost(int extra) { horsepower_ += extra; }
private:
int horsepower_;
};
class Car {
public:
Car(const std::string& model, int horsepower)
: model_(model), engine_(horsepower) {}
const std::string& model() const { return model_; }
int horsepower() const { return engine_.horsepower(); }
void boost(int extra) { engine_.boost(extra); }
private:
std::string model_;
Engine engine_;
};
int main() {
Car car("Falcon", 240);
std::cout << car.model() << ": " << car.horsepower() << " hp\n";
car.boost(20);
std::cout << "after boost: " << car.horsepower() << " hp\n";
return 0;
}The output shows the forwarded call reaching the engine.
Falcon: 240 hp
after boost: 260 hpComposition over inheritance
The classic mistake is to model "a car is a kind of vehicle" with inheritance, and then to discover that a car is also "a kind of thing with wheels" and "a kind of thing that can be driven". With inheritance, only one of those can be the base class. Composition has no such problem. A car can contain an engine, a gearbox, a chassis and a driver, and each of those can be reused elsewhere.
#include <iostream>
#include <string>
class Engine {
public:
explicit Engine(int horsepower) : horsepower_(horsepower) {}
int horsepower() const { return horsepower_; }
private:
int horsepower_;
};
class ElectricMotor {
public:
explicit ElectricMotor(int kilowatts) : kilowatts_(kilowatts) {}
int power_equivalent() const { return kilowatts_ * 2; }
private:
int kilowatts_;
};
class Vehicle {
public:
explicit Vehicle(std::string model) : model_(std::move(model)) {}
const std::string& model() const { return model_; }
private:
std::string model_;
};
class PetrolCar : public Vehicle {
public:
PetrolCar(std::string model, int horsepower)
: Vehicle(std::move(model)), engine_(horsepower) {}
int output() const { return engine_.horsepower(); }
private:
Engine engine_;
};
class ElectricCar : public Vehicle {
public:
ElectricCar(std::string model, int kilowatts)
: Vehicle(std::move(model)), motor_(kilowatts) {}
int output() const { return motor_.power_equivalent(); }
private:
ElectricMotor motor_;
};
int main() {
PetrolCar petrol("Falcon", 240);
ElectricCar electric("Spark", 120);
std::cout << petrol.model() << ": " << petrol.output() << " hp\n";
std::cout << electric.model() << ": " << electric.output() << " hp equivalent\n";
return 0;
}The output shows the two vehicles sharing the base but differing in the contained part.
Falcon: 240 hp
Spark: 240 hp equivalentThe fragile base class problem is the reason to prefer this. If the Vehicle base class later changes how it stores the model, then both derived classes are affected. With composition the shared part is a contained object that nobody inherits from.
Composition and ownership
The question of who owns the inner object decides the shape of the relationship.
the way who owns it
---------------------- -----------------------------------------------
by value the outer object, and it is the simplest
by std::unique_ptr the outer object, but it can hand the
ownership over
by raw pointer almost never the right choice, because
somebody has to remember to delete it
by reference nobody, and the outer object must not
destroy it
Composition in a larger design
Real objects are made of several parts, and each part is responsible for one thing. The outer class coordinates them.
#include <iostream>
#include <string>
#include <vector>
class Sensor {
public:
Sensor(const std::string& name, int reading) : name_(name), reading_(reading) {}
const std::string& name() const { return name_; }
int reading() const { return reading_; }
private:
std::string name_;
int reading_;
};
class DiagnosticPanel {
public:
void add_sensor(const Sensor& sensor) {
sensors_.push_back(sensor);
}
int highest_reading() const {
int best = 0;
for (const Sensor& sensor : sensors_) {
if (sensor.reading() > best) {
best = sensor.reading();
}
}
return best;
}
void report() const {
std::cout << "sensors: " << sensors_.size() << '\n';
for (const Sensor& sensor : sensors_) {
std::cout << " " << sensor.name() << ": " << sensor.reading() << '\n';
}
}
private:
std::vector<Sensor> sensors_;
};
int main() {
DiagnosticPanel panel;
panel.add_sensor(Sensor{"oil pressure", 42});
panel.add_sensor(Sensor{"coolant", 91});
panel.add_sensor(Sensor{"battery", 12});
panel.report();
std::cout << "highest: " << panel.highest_reading() << '\n';
return 0;
}The output shows the panel coordinating its sensors.
sensors: 3
oil pressure: 42
coolant: 91
battery: 12
highest: 91Quick reference
- Containment means that the outer object owns the inner one and it controls the lifetime.
- Construct the inner object through the outer constructor, so that the callers do not have to.
- Forward only the methods that the outside world needs, and leave everything else private.
- Prefer composition over inheritance when there is no genuine is-a relationship.
- The fragile base class problem is the classic reason to choose composition.
- Own by value, by
std::unique_ptrfor the optional parts, and by reference for the borrowed parts.
Try it yourself: build a Vehicle class that contains an Engine and a FuelTank, and a Car class that contains a Vehicle and adds the driver assistance features. Then write an ElectricCar that contains the same Vehicle but a different power unit. Print a summary of each and check that the shared behaviour lives in one place.