Is a special member function that is called automatically whenever a new object is created?
Published Mar 08, 2022Last updated Sep 03, 2022 Show
Source Constructor and Destructor are the special member functions of the class which are created by the C++ compiler or can be defined by the user. Constructor is used to initialize the object of the class while destructor is called by the compiler when the object is destroyed. What is Constructor in C++?A constructor is a special member function of a class and shares the same name as of class, which means the constructor and class have the same name. Constructor is called by the compiler whenever the object of the class is created, it allocates the memory to the object and initializes class data members by default values or values passed by the user while creating an object. Constructors don’t have any return type because their work is to just create and initialize an object. What is the basic syntax of Constructor in C++?The basic syntax of the constructor is given below: class class_name{ private: // private members public: // declaring constructor class_name({parameters}) { // constructor body } };In the above syntax, we can see the class has the name class_name and the constructor have also the same name. A constructor can have any number of parameters as per requirements. Also, there is no return type or return value of the constructor. Important Points about ConstructorsAccess specifiersConstructors can be defined as public, protected, or private as per the requirements. By default or default constructors, which are created by the compiler are declared as the public. If the constructor is created as
private, then we are not able to create its object. InheritanceAs a derived class can access all the public properties of the base class, it can call its constructor also if it is not declared as private. Also, the constructor's address cannot be referenced. VirtualConstructor in C++ cannot be declared as virtual because when we declare any function of a class as a virtual function, at compile time compiler creates a virtual table to store the address of each function that is declared as virtual. Also, it creates a data member of the class virtual pointer to points towards the virtual table. But as we have discussed we cannot refer to the address of the constructor, which means we are not able to declare the constructor as virtual. How many types of Constructors are present in C++?There are four types of constructors in c++
Let’s discuss them in detail Default ConstructorDefault constructor is also known as a zero-argument constructor, as it doesn’t take any parameter. It can be defined by the user if not then the compiler creates it on his own. Default constructor always initializes data members of the class with the same value they were defined. Syntax class class_name{ private: // private members public: // declaring default constructor class_name() { // constructor body } };Code to show the working of default constructor #includeOutput Default constructor is called Name of current object: student Age of current object: 12In the above code, we have created a class with two data members. Declared a default constructor which always initializes objects of a class with the same name and age. In the main function, we have created an object of the class and printed its data member values by using the display function. Parameterized ConstructorParameterized constructor is used to initialize data members with the values provided by the user. This constructor is basically the upgraded version of the default constructor. We can define more than one parameterized constructor according to the need of the user, but we have to follow the rules of the function overloading, like a different set of arguments must be there for each constructor. Syntax class class_name{ private: // private members public: // declaring parameterized constructor class_name(parameter1, parameter2,...) { // constructor body } };Code to understand the working of the parameterized constructor Output Constructor to set name is called Name of current object: First person Age of current object: 12 Constructor to set age is called Name of current object: Student Age of current object: 25 Constructor for both name and age is called Name of current object: Second person Age of current object: 15In the above code, we have created three types of the parametric constructor, one for initialization of name only, second to initialization of age only, and third to initialize both name and age. In the main function, we have created three different types of objects and initialized them in different ways, and printed values for each of them. Copy ConstructorIf we have an object of a class and we want to create its copy in a new declared object of the same class, then a copy constructor is used. The compiler provides each class a default copy constructor and users can define it also. It takes a single argument which is an object of the same class. Syntax class class_name{ private: // private members public: // declaring copy constructor class_name(const class_name& obj) { // constructor body } };In the above syntax, we created a copy constructor which takes an object of the same class as a parameter but it is declared constant and passed as a reference because when an argument is passed as a function parameter it creates a copy for it, to create that copy compiler will again call the copy constructor, means it will call the same function and for that call again there will be a call to create copy which will take this process in neverending recursion of creating copies. To prevent such conditions we pass it as a reference. Code to understand the working of the copy constructor #includeOutput Constructor for both name and age is called Name of current object: First person Age of current object: 25 Copy constructor is called Name of current object: First person Age of current object: 25In the above code, we have created a class and defined two types of constructors in it, the first is a parameterized constructor and another is a copy constructor. Parameterized constructor is used to create an object then by using the copy constructor we create a copy of it and stored it in another object. Dynamic ConstructorWhen memory is allocated dynamically to the data members at the runtime using a new operator, the constructor is known as the dynamic constructor. This constructor is similar to the default or parameterized constructor; the only difference is it uses a new operator to allocate the memory. Syntax Code to understand the working of the dynamic constructor #includeOutput Constructor for age is called Age of current object: 25In the above code, we have created a class with a dynamic constructor. In the main function, we have created an object and initialized it using a dynamic constructor, where we have given memory dynamically using a new operator. What is Destructor in C++?Destructor
is just the opposite function of the constructor. A destructor is called by the compiler when the object is destroyed and its main function is to deallocate the memory of the object. The object may be destroyed when the program ends, or local objects of the function get out of scope when the function ends or in any other case. What is the Basic Syntax of the Destructor?The basic syntax of the Destructor is given below class class_name{ private: // private members public: // declaring destructor ~class_name() { // destructor body } };In the above syntax, we can see the class has the name class_name and the destructor also has the same name, in addition there is a tilde(~). Also, there is no return type and return value of the destructor. Important Points about the Destructor
How Constructor and Destructor are called when the object is Created and DestroyedAs constructor is the first function called by the compiler when an object is created and the destructor is the last class member called by the compiler for an object. If the constructor and destructor are not declared by the user, the compiler defines the default constructor and destructor of a class object. Output Constructor is called Value of a: 5 Value of b: 6 Destructor is called Value of a: 5 Value of b: 6In the above code, we have created a class with constructor and destructor. In the main function, an object uses a parametric constructor, and when the program ends the destructor is automatically called by the compiler and we get the values of our variables. Conclusion
Is a special member function for automatic initialization of an object?Answer: A constructor can be defined as a special member function which is used to initialize the objects of the class with initial values. It is special member function as its name is the same as the class name. It enables an object to initialize itself during its creation.
Is a special type of method which is called automatically when an object is created?A constructor is a special method of a class or structure in object-oriented programming that initializes a newly created object of that type. Whenever an object is created, the constructor is called automatically.
Which is a special member function?The special member functions are class (or struct) member functions that, in certain cases, the compiler automatically generates for you. These functions are the default constructor, the destructor, the copy constructor and copy assignment operator, and the move constructor and move assignment operator.
Is a special function in a class that is called when a new object of the class is created?Constructors in Python
This special function gets called whenever a new object of that class is instantiated. This type of function is also called constructors in Object Oriented Programming (OOP). We normally use it to initialize all the variables.
|