Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Constructor in C++

Introduction

A constructor is a special type of function in a class that is automatically called when an object of the class is created. It is used to initialize the object’s properties with some initial values.

Key Points About Constructor

Same Name as Class: A constructor has the same name as the class.

No Return Type: Unlike other functions, constructors do not have a return type, not even void.

Automatic Invocation: A constructor is automatically called when an object is created.

For example :

The given below code defines a Student class with a constructor that initializes the attributes name and age to default values. When an object of the class is created, the constructor is called automatically, and the default values are assigned to the object, which are then displayed using the displayInfo() method.

#include <iostream>
using namespace std;

// Class Definition
class Student {
    public:
        string name;
        int age;

        // Constructor
        Student() {
            name = "Unknown";  // Default value for name
            age = 0;           // Default value for age
        }

        // Method to display student information
        void displayInfo() {
            cout << "Name: " << name << endl;
            cout << "Age: " << age << endl;
        }
};

// Main function
int main() {
    // Create an object of the Student class
    Student student1;

    // Call the displayInfo method
    student1.displayInfo();  // It will print the default values set in the constructor

    return 0;
}


Output:

Name: Unknown
Age: 0

Explanation:

Step 1: Class Definition:

class Student {
    public:
        string name;
        int age;
  • The class Student is defined with two public attributes:
    • name: A string to store the student’s name.
    • age: An integer to store the student’s age.

Step 2: Create a Constructor:

Student() {
    name = "Unknown";  // Default value for name
    age = 0;           // Default value for age
}
  • This is a constructor for the Student class.
  • A constructor is a special method that is automatically called when an object of the class is created.
  • In this constructor, the values of name and age are initialized to default values:
    • name = "Unknown": If no name is provided, the name will be set to "Unknown".
    • age = 0: If no age is provided, the age will be set to 0.

Step 3: Method to Display Information:

void displayInfo() {
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
}
  • This method displayInfo() is used to print the student’s information.
  • It uses cout to display the values of the name and age attributes.

Step 4: Main Function:

int main() {
    // Create an object of the Student class
    Student student1;

    // Call the displayInfo method
    student1.displayInfo();  // It will print the default values set in the constructor

    return 0;
}

  • In the main() function, an object student1 of the Student class is created.
  • When the object is created, the constructor is automatically called, and the default values "Unknown" for name and 0 for age are set.
  • Then, the displayInfo() method is called to print these values.

    Leave a Reply

    Your email address will not be published.

    Need Help?