Classes Composition in C++

Lesson Introduction and What is Composition?

Welcome to the lesson on class composition in C++. You have now learned the basics of classes, methods, constructors, and encapsulation. Today, we are going to take a step further into object-oriented programming by exploring class composition. The goal of this lesson is to understand how to construct complex classes by combining simpler, reusable components.

Class composition is an essential design principle that enables you to create complex objects. We will learn how to build classes containing other objects and see practical applications of this approach. Let's get started!

What is Composition? A real-life analogy would be a car. A car comprises various parts, such as an engine, wheels, and a transmission. These parts work together to make the car function. Similarly, in programming, composition allows you to build a Car class that includes an Engine class, a Wheel class, and so on.

Basic Class Composition

Let's start with a simple example to understand class composition:

#include <iostream>

class Engine {
public:
    void start() {
        std::cout << "Engine started";
    }
};

class Car {
private:
    Engine engine;  // Car has an Engine
public:
    void start() {
        engine.start();               // Start the engine first
        std::cout << " Car started";
    }
};

int main() {
    Car myCar;
    myCar.start();  // Outputs: Engine started Car started
    return 0;
}

In this example, the Car class contains an Engine object. Here's what's happening:

  1. The Engine class has a method called start(), which prints "Engine started".
  2. The Car class has a private data member engine of type Engine.
  3. The Car class also has a start() method that calls the Engine's start() method before printing "Car started".

When you create a Car object and call its start() method, it first starts the engine and then starts the car, showing how the Car uses functionality provided by the Engine.

Advanced Class Composition: Part 1

Now let's look at a more advanced example, where a Car is composed of both Engine and Wheel objects:

#include <iostream>

class Engine {
private:
    int horsepower;   // Engine has a horsepower attribute
public:
    Engine(int hp) : horsepower(hp) {}  // Constructor initializes horsepower
    int getHorsepower() const {
        return horsepower;
    }
};

class Wheel {
private:
    int size;  // Wheel has a size attribute
public:
    Wheel(int s) : size(s) {}  // Constructor initializes size
    int getSize() const {
        return size;
    }
};

In this example, the Car class will have both Engine and Wheel objects. Here are some key points:

  1. The Engine class has a private attribute horsepower and a method getHorsepower() to retrieve it.
  2. The Wheel class has a private attribute size and a method getSize() to retrieve it.
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal