WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Basic · question 6 of 100

What is a constructor, and when is it called in the lifecycle of a C++ object?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

A constructor is a special member function in C++ that is called when an object of a class is created. Its purpose is to initialize the object’s data members and to perform any other necessary setup operations.

In C++, a class can have one or more constructors, and each constructor has the same name as the class. The constructor is typically used to initialize the data members of the class, but it can also perform other operations such as allocating memory, opening files, or initializing other objects.

When an object of a class is created, the constructor is called automatically. The exact time when the constructor is called depends on how the object is created:

Default constructor: If no constructor is defined for a class, a default constructor is automatically generated by the compiler. This constructor takes no arguments and initializes the object’s data members to their default values (which may be zero or null, depending on the data type). For example:

    class MyClass {
        public:
        // Default constructor
        MyClass() {
            std::cout << "Default constructor called" << std::endl;
        }
        
        private:
        int myValue;
    };
    
    int main() {
        MyClass myObject; // default constructor called
        return 0;
    }

Parameterized constructor: A parameterized constructor takes one or more arguments and initializes the object’s data members based on the values of those arguments. For example:

    class MyClass {
        public:
        // Parameterized constructor
        MyClass(int value) {
            myValue = value;
            std::cout << "Parameterized constructor called with value " << value << std::endl;
        }
        
        private:
        int myValue;
    };
    
    int main() {
        MyClass myObject(42); // parameterized constructor called with value 42
        return 0;
    }

Copy constructor: A copy constructor is called when an object is created as a copy of another object of the same class. The copy constructor takes a reference to another object of the same class and initializes the new object’s data members based on the values of the original object’s data members. For example:

    class MyClass {
        public:
        // Copy constructor
        MyClass(const MyClass& other) {
            myValue = other.myValue;
            std::cout << "Copy constructor called with value " << myValue << std::endl;
        }
        
        private:
        int myValue;
    };
    
    int main() {
        MyClass originalObject(42);
        MyClass copyObject(originalObject); // copy constructor called with value 42
        return 0;
    }

In summary, a constructor is a special member function in C++ that is called when an object of a class is created. Its purpose is to initialize the object’s data members and to perform any other necessary setup operations. Constructors can be default, parameterized, or copy constructors, and they are called automatically depending on how the object is created.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics