c-plus-plus
  1. c-plus-plus-constructor

C++ Object Class Constructor

The constructor is a special type of member function that is called automatically when an object is created. It initializes the object's data members to their default values. In C++, there are two types of constructors: default and parameterized. The default constructor takes no arguments and initializes the data members to their default values. The parameterized constructor, on the other hand, takes one or more arguments and initializes the data members to the values provided.

Syntax

The syntax for declaring a constructor in C++ is as follows:

class MyClass {
public:
    MyClass();                      // default constructor
    MyClass(int arg1, int arg2);    // parameterized constructor
};

Example

Here is an example of a simple class with a default and parameterized constructor:

#include <iostream>

class Rectangle {
public:
    Rectangle();                            // default constructor
    Rectangle(int width, int height);       // parameterized constructor

    int area() { return width * height; }
private:
    int width;
    int height;
};

Rectangle::Rectangle() : width(0), height(0) {}                      // default constructor
Rectangle::Rectangle(int w, int h) : width(w), height(h) {}          // parameterized constructor

int main() {
    Rectangle rect1;                            // creates object using default constructor
    Rectangle rect2(5, 10);                     // creates object using parameterized constructor

    std::cout << "Area of rect1: " << rect1.area() << std::endl;
    std::cout << "Area of rect2: " << rect2.area() << std::endl;

    return 0;
}

Output

Area of rect1: 0
Area of rect2: 50

Explanation

In the above example, we have defined a Rectangle class with two constructors, a default constructor and a parameterized constructor. The data members, width and height, are initialized to their default values using the default constructor and to the values provided by the user using the parameterized constructor.

In the main function, we create two objects, rect1 and rect2, using the default and parameterized constructors, respectively. We then call the area function to calculate the area of each object, which is printed to the console.

Use

Constructors are used to initialize the data members of an object to their default or user-provided values. They are typically used to create objects of a class and initialize the data members at the same time.

Important Points

  • A class can have multiple constructors with different argument lists.
  • If a class does not have a constructor, the compiler generates a default constructor.
  • The constructor can be defined inside or outside the class definition.
  • Constructors cannot have a return type, not even void.
  • A constructor with no arguments is called a default constructor.

Summary

In C++, the constructor is a special member function that is called automatically when an object is created. It initializes the object's data members to their default values. Constructors can be default or parameterized, and can be defined inside or outside the class definition. They are used to create objects of a class and initialize the data members at the same time.

Published on: