Composition is the main alternative to inheritance in OOP. It extends the capabilities of a class by having a instance of the extended class as a property.

Example using Inheritance

#include <iostream>
 
class User {
public:
    User()
    void getName() = 0;
    void setPassword() = 0;
};
 
class Admin : public Animal {
public:
    void makeSound() {
        printf("Bark\n");
    }
};
 
int main() {
    Dog
    return 0;
}

Example using Composition

#include <iostream>
 
class Printer {
public:
    void print(int value) {
        printf("Printing int: %d\n", value);
    }
 
    void print(double value) {
        printf("Printing double: %.2f\n", value);
    }
 
    void print(const char* value) {
        printf("Printing string: %s\n", value);
    }
};
 
int main() {
    Printer printer;
    printer.print(42);
    printer.print(3.14);
    printer.print("Hello");
    return 0;
}
Output
Printing int: 42
Printing double: 3.14
Printing string: Hello