C++ Constructors | Examples

Constructors

A constructor is a unique member function of a class in C++ that is invoked whenever an object of that class is created. Initializing the class’s data members and carrying out any necessary setup tasks are done in constructors.

The same name as the class, followed by parentheses (), should be used to create a constructor.

Example:

#include <iostream>
using namespace std;

class Car {
private:
    string make;
    string model;
    int year;

public:
    Car(const string& carMake, const string& carModel, int carYear) {
        make = carMake;
        model = carModel;
        year = carYear;
    }

    void displayInfo() {
        cout << "Make: " << make << endl;
        cout << "Model: " << model << endl;
        cout << "Year: " << year << endl;
    }
};

int main() {
    Car myCar("Toyota", "Camry", 2022);
    myCar.displayInfo();

    return 0;
}
OUTPUT:
Make: Toyota
Model: Camry
Year: 2022

Explanation:

Make, Model and Year are the three private data members of the Car class in the previously mentioned example. CarMake, CarModel, and CarYear are the three inputs required by the constructor of the Car class. The corresponding data members of the class are initialized using these parameters.

The make, model, and year variables in the constructor are set to the values passed as arguments using the assignment operator (=).

The constructor is called in the main function along with the parameters “Toyota“, “Camry“, and 2022 to create an object of the Car class with the name myCar. The information is then displayed by using the myCar object’s displayInfo member function.

It should be noted that the constructor has the same name as the class, is always open to the public, and has no return value.


Parameters within Constructor

When creating an object in C++, constructor parameters are used to pass values to initialize the data members of a class. Depending on the desired initialization logic, constructors can have zero or more parameters.

#include <iostream>
using namespace std;

class Rectangle {
private:
    int length;
    int width;

public:
    Rectangle(int len, int wid) {
        length = len;
        width = wid;
    }

    int calculateArea() {
        return length * width;
    }
};

int main() {
    Rectangle myRectangle(5, 3);
    int area = myRectangle.calculateArea();
    cout << "Area: " << area << endl;

    return 0;
}
OUTPUT:
Area: 15

Explanation:

In the above example, we have a class called Rectangle that represents a rectangle shape. The class has two private data members: length and width. The constructor of the Rectangle the class takes two parameters: len and wid, which represent the length and width of the rectangle, respectively.

Inside the constructor, the values of the parameters are assigned to the corresponding data members using the assignment operator (=).

In the main function, an object of the Rectangle class named myRectangle is created by calling the constructor with the arguments 5 and 3. These values are passed as parameters to the constructor, which initializes the length and width data members of the object.

Finally, the calculateArea a member function is called on the myRectangle object to calculate the area of the rectangle, which is then displayed on the console.

Leave a Reply

Your email address will not be published. Required fields are marked *