How to Call Base Class Constructor in C++: Syntax and Examples
In C++, you call a base class constructor from a derived class by using an
initializer list in the derived class constructor. This is done by specifying the base class constructor with its arguments after a colon : following the derived constructor signature.Syntax
To call a base class constructor in C++, use the derived class constructor's initializer list. The syntax is:
DerivedConstructor(args) : BaseConstructor(base_args) { }Here:
- DerivedConstructor(args) is the constructor of the derived class.
- BaseConstructor(base_args) calls the base class constructor with its required arguments.
- The colon
:separates the constructor signature from the initializer list.
cpp
class Base { public: Base(int x) { // base constructor code } }; class Derived : public Base { public: Derived(int x, int y) : Base(x) { // derived constructor code } };
Example
This example shows a base class Base with a constructor that takes an integer. The derived class Derived calls the base constructor using an initializer list to pass the value.
cpp
#include <iostream> class Base { public: Base(int x) { std::cout << "Base constructor called with value: " << x << std::endl; } }; class Derived : public Base { public: Derived(int x, int y) : Base(x) { std::cout << "Derived constructor called with value: " << y << std::endl; } }; int main() { Derived obj(10, 20); return 0; }
Output
Base constructor called with value: 10
Derived constructor called with value: 20
Common Pitfalls
Common mistakes when calling base class constructors include:
- Not using the initializer list and trying to call the base constructor inside the derived constructor body, which does not work.
- Forgetting to pass required arguments to the base constructor.
- Not specifying the base constructor explicitly when it requires parameters, causing compilation errors.
Example of wrong and right ways:
cpp
class Base { public: Base(int x) {} }; class DerivedWrong : public Base { public: DerivedWrong(int x) { // Wrong: base constructor not called explicitly // Base(x); // This is just a function call, not constructor call } }; class DerivedRight : public Base { public: DerivedRight(int x) : Base(x) { // Correct: base constructor called in initializer list } };
Quick Reference
Tips for calling base class constructors in C++:
- Always use the initializer list to call base constructors.
- If the base constructor takes parameters, you must call it explicitly.
- If the base class has a default constructor, it is called automatically if you don't specify.
- Initializer lists run before the constructor body.
Key Takeaways
Call base class constructors in the derived class initializer list using a colon after the constructor signature.
You must explicitly call base constructors with parameters; otherwise, compilation errors occur.
Initializer lists run before the constructor body, so base initialization happens first.
Do not try to call base constructors inside the derived constructor body; it only calls a function, not the constructor.
If the base class has a default constructor, it is called automatically if not specified.