Lesson 4
Composition in C++: Building Blocks of Software Design Patterns
Lesson Overview and Goals

Hello, and welcome back! Today, we will delve into the fundamentals of Composition in C++! A crucial aspect of C++ design patterns, composition enables the construction of complex classes from simpler ones. In today's lesson, we will understand the concept of composition, its significance in software development, and how to implement it effectively in C++.

Garnering Clarity on the Composition Design Pattern

To kick-start our exploration, let's understand Composition. In object-oriented programming (OOP), composition allows a class to contain instances of other classes, facilitating the creation of complex systems from simple components. Take, for instance, building a car: independent objects like the engine, wheels, and seats are all brought together — a perfect example of composition in daily life. Under composition, if the parent object (the car) is destroyed, the child objects (the components) also cease to exist.

Acing the Composition Design in C++

Now, let's translate the theory into practical C++ code. Revisiting the car example, a Car class in C++ "composes" objects of the Engine, Wheels, and Seats classes. The Car class owns these child objects, meaning their existence is tied to the Car.

C++
1#include <iostream> 2#include <string> 3 4class Engine { 5public: 6 void start() { 7 std::cout << "Engine starts" << std::endl; // Engine start message 8 } 9 10 void stop() { 11 std::cout << "Engine stops" << std::endl; // Engine stop message 12 } 13}; 14 15class Wheels { 16public: 17 void rotate() { 18 std::cout << "Wheels rotate" << std::endl; // Wheel rotation message 19 } 20}; 21 22class Seats { 23public: 24 void adjust(const std::string& position) { 25 std::cout << "Seats adjusted to position " << position << std::endl; // Seat adjustment message 26 } 27}; 28 29class Car { 30private: 31 Engine engine; 32 Wheels wheels; 33 Seats seats; 34 35public: 36 void start() { 37 engine.start(); // Call to start engine 38 seats.adjust("upright"); // Adjust seat position 39 wheels.rotate(); // Get wheels rolling 40 } 41}; 42 43int main() { 44 Car myCar; 45 myCar.start(); // Begin car functions 46 47 /* 48 Output: 49 Engine starts 50 Seats adjusted to position upright 51 Wheels rotate 52 */ 53 return 0; 54}

In the code above, the Car class encapsulates Engine, Wheels, and Seats objects, which operate independently but are part of the Car class, demonstrating the Composition pattern.

Discerning Composition From Inheritance

In OOP, Composition and Inheritance are two foundational ways to express relationships between classes. While inheritance implies an "is-a" relationship, composition suggests a "has-a" relationship. For example, a Car IS A Vehicle (inheritance), but a Car HAS AN Engine (composition).

Lesson Summary and Practice

Fantastic work! You’ve now unpacked composition and implemented it in C++! Next, you'll engage in stimulating exercises where you'll gain hands-on experience with composition using C++. Practice building classes with composition to strengthen your understanding and proficiency in applying this design pattern. Stay motivated, and keep experimenting to reinforce your concepts!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.