C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

Constructor in C++

Constructor in C++ Tutorial

A constructor is a special member function of a class that is automatically called when an object of the class is created. The constructor's main purpose is to initialize the object's member variables and set up the required resources. This tutorial will cover the basics of constructors in C++.

Defining a Constructor

A constructor has the same name as the class and has no return type. It can take parameters to initialize the object's member variables.

Example:

class MyClass {
    int x;

public:
    MyClass(int a) { // Constructor with one parameter
        x = a;
    }
};

In this example, we define a constructor for the MyClass class that takes one parameter a and initializes the x member variable with its value.

Default Constructor

A default constructor is a constructor that takes no parameters. If you don't define any constructor for a class, the compiler automatically generates a default constructor for you. However, if you define a constructor with parameters, the compiler no longer generates a default constructor. In this case, you should define one yourself if you want to create objects without providing initial values for the member variables.

Example:

class MyClass {
    int x;

public:
    MyClass() { // Default constructor
        x = 0;
    }

    MyClass(int a) { // Constructor with one parameter
        x = a;
    }
};

In this example, we define both a default constructor and a constructor with one parameter for the MyClass class.

Constructor Overloading

Like other functions in C++, constructors can be overloaded. You can define multiple constructors with different numbers of parameters or different types of parameters.

Example:

class MyClass {
    int x, y;

public:
    MyClass() { // Default constructor
        x = 0;
        y = 0;
    }

    MyClass(int a) { // Constructor with one parameter
        x = a;
        y = 0;
    }

    MyClass(int a, int b) { // Constructor with two parameters
        x = a;
        y = b;
    }
};

In this example, we define three constructors for the MyClass class with different numbers of parameters.

Member Initializer Lists

A more efficient and concise way to initialize member variables in the constructor is to use member initializer lists. Instead of assigning values inside the constructor body, you can initialize the member variables directly in the constructor's declaration.

Example:

class MyClass {
    int x, y;

public:
    MyClass() : x(0), y(0) {} // Default constructor with member initializer list

    MyClass(int a) : x(a), y(0) {} // Constructor with one parameter and member initializer list

    MyClass(int a, int b) : x(a), y(b) {} // Constructor with two parameters and member initializer list
};

In this example, we use member initializer lists to initialize the member variables in the constructors for the MyClass class.

  1. Default constructor in C++:

    • Description: Explains the default constructor, a constructor with no parameters, used when an object is created without explicit initialization.
    • Example Code:
      class MyClass {
      public:
          // Default constructor
          MyClass() {
              // Initialization code if needed
          }
      };
      
  2. Parameterized constructor in C++:

    • Description: Introduces parameterized constructors, which accept parameters during object creation to initialize member variables.
    • Example Code:
      class MyClass {
      public:
          // Parameterized constructor
          MyClass(int value) {
              // Initialization using the parameter
          }
      };
      
  3. Constructor overloading in C++:

    • Description: Discusses constructor overloading, where a class has multiple constructors with different parameter lists.
    • Example Code:
      class MyClass {
      public:
          // Default constructor
          MyClass() {}
      
          // Parameterized constructor
          MyClass(int value) {}
      };
      
  4. Initialization lists in C++ constructors:

    • Description: Introduces initialization lists in constructors, providing a way to initialize member variables before entering the constructor body.
    • Example Code:
      class MyClass {
      private:
          int data;
      
      public:
          // Constructor with initialization list
          MyClass(int value) : data(value) {
              // Constructor body
          }
      };
      
  5. Constructor chaining in C++:

    • Description: Explains constructor chaining, where one constructor calls another constructor within the same class.
    • Example Code:
      class MyClass {
      private:
          int data;
      
      public:
          // Parameterized constructor
          MyClass(int value) : data(value) {}
      
          // Constructor chaining using another constructor
          MyClass() : MyClass(0) {}
      };
      
  6. Copy constructor in C++:

    • Description: Introduces the copy constructor, responsible for creating a new object as a copy of an existing object.
    • Example Code:
      class MyClass {
      public:
          int data;
      
          // Copy constructor
          MyClass(const MyClass& other) : data(other.data) {}
      };
      
  7. Constructor with default arguments in C++:

    • Description: Demonstrates constructors with default arguments, allowing objects to be created with or without specific values.
    • Example Code:
      class MyClass {
      public:
          int data;
      
          // Constructor with default argument
          MyClass(int value = 42) : data(value) {}
      };
      
  8. Static constructor in C++:

    • Description: Introduces static constructors, which are called once when a class is loaded and primarily used in specific scenarios, such as initializing static members.
    • Example Code:
      class MyClass {
      public:
          static int staticData;
      
          // Static constructor
          static MyClass initialize() {
              staticData = 10;
              return MyClass();
          }
      };
      
  9. Inheritance and constructors in C++:

    • Description: Explores how constructors work in the context of inheritance, including calling base class constructors and handling virtual functions.
    • Example Code:
      class Base {
      public:
          Base(int value) {
              // Base class constructor
          }
      };
      
      class Derived : public Base {
      public:
          // Derived class constructor calling base class constructor
          Derived(int value) : Base(value) {
              // Derived class constructor body
          }
      };