WikiGalaxy

Personalize

C++ Constructors

Default Constructor:

A default constructor is a constructor that takes no arguments. It is used to initialize objects with default values.


class Example {
  public:
    Example() {
      cout << "Default Constructor Called" << endl;
    }
};
int main() {
  Example obj;
  return 0;
}
    

Usage:

Default constructors are useful when you want to create objects without providing initial values.

Console Output:

Default Constructor Called

Parameterized Constructor

Definition:

A parameterized constructor is a constructor that takes arguments. It allows for custom initialization of objects.


class Example {
  int x;
  public:
    Example(int a) : x(a) {
      cout << "Parameterized Constructor Called with value: " << x << endl;
    }
};
int main() {
  Example obj(10);
  return 0;
}
    

Purpose:

Parameterized constructors are used to initialize objects with specific values at the time of creation.

Console Output:

Parameterized Constructor Called with value: 10

Copy Constructor

Overview:

A copy constructor is a constructor that initializes an object using another object of the same class.


class Example {
  int x;
  public:
    Example(int a) : x(a) {}
    Example(const Example &obj) {
      x = obj.x;
      cout << "Copy Constructor Called" << endl;
    }
};
int main() {
  Example obj1(10);
  Example obj2 = obj1;
  return 0;
}
    

Importance:

Copy constructors are essential for copying objects, especially when dealing with dynamic memory allocation.

Console Output:

Copy Constructor Called

Destructor

Definition:

A destructor is a special member function that is called when an object goes out of scope or is explicitly deleted.


class Example {
  public:
    ~Example() {
      cout << "Destructor Called" << endl;
    }
};
int main() {
  Example obj;
  return 0;
}
    

Function:

Destructors are used to release resources allocated by the object, such as memory or file handles.

Console Output:

Destructor Called

Constructor Overloading

Concept:

Constructor overloading is the practice of defining multiple constructors with different sets of parameters.


class Example {
  int x;
  public:
    Example() : x(0) {}
    Example(int a) : x(a) {}
};
int main() {
  Example obj1;
  Example obj2(10);
  return 0;
}
    

Advantage:

Overloading constructors allows for flexible initialization of objects based on the provided parameters.

Console Output:

No specific output; constructors initialize objects differently.

Constructor with Default Arguments

Explanation:

Constructors can have default arguments, allowing them to be called with fewer arguments than declared.


class Example {
  int x;
  public:
    Example(int a = 5) : x(a) {
      cout << "Constructor Called with value: " << x << endl;
    }
};
int main() {
  Example obj1;
  Example obj2(10);
  return 0;
}
    

Benefit:

Default arguments provide flexibility and reduce the need for multiple overloaded constructors.

Console Output:

Constructor Called with value: 5

Constructor Called with value: 10

Delegating Constructors

Concept:

Delegating constructors allow one constructor to call another constructor within the same class.


class Example {
  int x;
  public:
    Example() : Example(0) {}
    Example(int a) : x(a) {
      cout << "Constructor Called with value: " << x << endl;
    }
};
int main() {
  Example obj1;
  Example obj2(10);
  return 0;
}
    

Purpose:

Delegating constructors simplify code and avoid duplication by reusing constructor logic.

Console Output:

Constructor Called with value: 0

Constructor Called with value: 10

Move Constructor

Introduction:

A move constructor transfers ownership of resources from a temporary object to a new object.


class Example {
  int* ptr;
  public:
    Example(int a) : ptr(new int(a)) {}
    Example(Example &&obj) : ptr(obj.ptr) {
      obj.ptr = nullptr;
      cout << "Move Constructor Called" << endl;
    }
    ~Example() {
      delete ptr;
    }
};
int main() {
  Example obj1(10);
  Example obj2 = std::move(obj1);
  return 0;
}
    

Significance:

Move constructors are crucial for optimizing performance by eliminating unnecessary copying.

Console Output:

Move Constructor Called

Constructor Initialization List

Details:

An initialization list allows members to be initialized before the constructor body executes.


class Example {
  int x;
  public:
    Example(int a) : x(a) {
      cout << "Initialization List Used with value: " << x << endl;
    }
};
int main() {
  Example obj(10);
  return 0;
}
    

Benefit:

Using initialization lists improves performance and is necessary for initializing const or reference members.

Console Output:

Initialization List Used with value: 10

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025