Article by Ayman Alheraki on January 11 2026 10:33 AM
C++ has seen significant improvements in object-oriented programming (OOP) features with the introduction of delegating constructors and inheriting constructors. These features simplify constructor management, reduce code duplication, and make the overall design more efficient.
In this article, we'll cover:
Delegating Constructors: Reusing constructors within the same class.
Inheriting Constructors: Automatically inheriting constructors from a base class in derived classes.
A delegating constructor allows one constructor to call another constructor within the same class. This feature, introduced in C++11, helps reduce code duplication by centralizing the object initialization logic.
Code Reusability: Avoid duplicating initialization logic across multiple constructors.
Maintainability: Any changes in the core initialization logic are reflected across all delegating constructors.
The delegating constructor calls another constructor using the colon initializer syntax.
class MyClass { int x, y;
public: // Main constructor MyClass(int a, int b) : x(a), y(b) {}
// Delegating constructor MyClass() : MyClass(0, 0) { // Calls the main constructor std::cout << "Default constructor delegating to main constructor\n"; }
// Another delegating constructor MyClass(int a) : MyClass(a, 0) { std::cout << "Constructor delegating with one parameter\n"; }};int main() { MyClass obj1; // Calls the default delegating constructor MyClass obj2(5); // Calls the constructor with one parameter MyClass obj3(5, 10); // Calls the main constructor}In this example, the constructors MyClass() and MyClass(int a) delegate to the main constructor MyClass(int a, int b), avoiding repetition of the initialization logic.
Consider a class that has multiple constructors for different initialization scenarios (default, partial, and full initialization). Instead of duplicating logic in each constructor, you delegate most of them to the main constructor.
class Rectangle { int width, height;
public: // Main constructor Rectangle(int w, int h) : width(w), height(h) {}
// Delegating to default values Rectangle() : Rectangle(1, 1) {}
// Delegating with width only Rectangle(int w) : Rectangle(w, 1) {}};This approach ensures that the main logic for setting width and height resides in only one place, making the class easier to maintain and extend.
Inheriting constructors, introduced in C++11, enable derived classes to inherit the constructors of their base class. This eliminates the need to explicitly redefine or write constructors for a derived class that would merely pass parameters to the base class.
Simplifies Class Hierarchies: No need to redefine constructors in derived classes if they perform the same initialization as the base class.
Reduces Boilerplate Code: Automatically forwards constructor parameters to the base class, reducing redundant code.
Inheriting constructors is achieved using the using keyword.
class Base {public: Base(int x) { std::cout << "Base constructor called with value: " << x << std::endl; }};
class Derived : public Base { using Base::Base; // Inherit constructors from Base};int main() { Derived d(5); // Calls Base's constructor with value 5}In this example, the derived class Derived inherits the constructor of the base class Base. The derived class doesn’t need to explicitly define a constructor—it automatically uses the constructor from Base as if it were its own.
Consider a scenario where you have a base class Shape with various constructors to initialize geometric shapes. If a derived class Circle doesn't add any additional member variables or logic, you can inherit all constructors from Shape without rewriting them in Circle.
class Shape { int width, height;public: Shape(int w, int h) : width(w), height(h) {} Shape(int w) : width(w), height(1) {} Shape() : width(1), height(1) {}};
class Circle : public Shape { using Shape::Shape; // Inherit constructors};With using Shape::Shape;, the Circle class automatically gains all three constructors from Shape.
You can also combine both features to create more concise and maintainable class hierarchies.
class Base {public: Base(int x, int y) { std::cout << "Base constructor called with values: " << x << ", " << y << std::endl; } Base(int x) : Base(x, 0) {}};
class Derived : public Base { using Base::Base; // Inherit constructors from Base};In this example, Base has two constructors, one of which is a delegating constructor. Derived inherits both constructors without needing any additional code.
Code Reuse: Use delegating constructors to avoid repeating initialization logic within the same class. This helps avoid potential bugs caused by having redundant code that might become inconsistent.
Simplify Class Hierarchies: Use inheriting constructors to simplify derived classes, especially if they don't add any new member variables or custom initialization logic. This keeps the class structure clean and focused on what each class does uniquely.
Self-Delegation: When designing constructors, ensure that self-delegation (one constructor delegating to another) maintains logical consistency and that no redundant initialization occurs.
Avoid Complexity in Delegation Chains: While delegating constructors can reduce code duplication, avoid creating overly complex chains of delegating constructors, as this can make the code harder to understand and maintain.
Modern C++ features such as delegating and inheriting constructors offer significant improvements in code efficiency, maintainability, and clarity. Delegating constructors allow constructors within the same class to reuse initialization logic, while inheriting constructors simplify derived class construction by borrowing the constructors from base classes. Together, these features contribute to writing cleaner, more efficient C++ code, enabling programmers to focus on extending functionality without repeating common patterns.
By mastering these modern OOP features, C++ developers can create more robust, maintainable, and efficient codebases that leverage the full power of object-oriented programming in C++.