Constructors

Constructors in C++ are special member functions that are called when an object of a class is created. Their main purpose is to initialize the object's data members and perform any necessary setup tasks. Constructors have the same name as the class and do not have a return type (not even void). There are several types of constructors in C++:

  1. Default Constructor:

    • A default constructor is automatically generated by the compiler if no constructor is provided explicitly.

    • It initializes the data members to default values. For built-in types, this means they are not initialized, and for user-defined types, it may call their default constructors.

    • Example:

      class MyClass {
      public:
          // Default constructor
          MyClass() {
              // Initialization code (if needed)
          }
      };
  2. Parameterized Constructor:

    • A parameterized constructor allows you to initialize the object with specific values at the time of creation.

    • You can have multiple parameterized constructors with different parameter lists (overloading).

    • Example:

      class MyClass {
      public:
          // Parameterized constructor
          MyClass(int value) {
              // Initialization code with the given value
          }
      };
  3. Copy Constructor:

    • A copy constructor creates a new object by copying the values of another object of the same class.

    • It is used when an object is initialized from another object or when objects are passed by value.

    • By default, the compiler generates a shallow copy constructor, but you can provide your own for custom behavior.

    • Example:

      class MyClass {
      public:
          // Copy constructor
          MyClass(const MyClass& other) {
              // Copy data members from 'other'
          }
      };
  4. Constructor Overloading:

    • You can define multiple constructors with different parameter lists in a class. This is called constructor overloading.

    • The appropriate constructor is called based on the number and types of arguments used during object creation.

    • Example:

      class MyClass {
      public:
          MyClass() { /* Default constructor */ }
          MyClass(int value) { /* Parameterized constructor */ }
          MyClass(const MyClass& other) { /* Copy constructor */ }
      };
  5. Delegating Constructors (C++11 and later):

    • Delegating constructors allow one constructor of a class to call another constructor of the same class.

    • This can help reduce code duplication when you have multiple constructors with similar initialization logic.

    • Example (C++11 and later):

      class MyClass {
      public:
          MyClass(int value) { /* Initialization code */ }
          MyClass() : MyClass(0) { /* Delegating constructor */ }
      };
  6. Inheriting Constructors (C++11 and later):

    • Inheriting constructors allow derived classes to inherit constructors from their base classes.

    • This simplifies constructor definition in derived classes.

    • Example (C++11 and later):

      class Base {
      public:
          Base(int value) { /* Initialization code */ }
      };
      
      class Derived : public Base {
      using Base::Base; // Inheriting constructor
      };
  7. Explicit Constructor:

    • The explicit keyword can be used before a single-argument constructor to prevent implicit type conversion.

    • It ensures that the constructor is only called when explicitly invoked.

    • Example:

      class MyClass {
      public:
          explicit MyClass(int value) { /* Initialization code */ }
      };
      
      // Usage with explicit constructor
      MyClass obj1 = 42; // Error (implicit conversion not allowed)
      MyClass obj2(42);  // Correct

Constructors play a vital role in C++ classes and allow you to set up the initial state of objects, perform resource allocation, and ensure proper object creation and initialization. Depending on your class's requirements, you can choose the appropriate constructor types to implement.

Last updated

Was this helpful?