Constructors In C++
In C++, constructors are special member functions that are automatically called when an object of a class is created. They are used to initialize the object's data members. They have the same name as the class. Constructors can be classified into several types, including default constructors, parameterized constructors. For example,
Default Constructor
A default constructor is a constructor that doesn't take any arguments. It initializes the object's data members to default values or performs any necessary setup. If you don't provide a constructor explicitly, the compiler generates a default constructor for you.
Output:
Here,
- The default constructor is called when an object of the
MyClass
class is created using the statementMyClass obj;
in the main function.
Parameterized Constructor
A parameterized constructor is a constructor that accepts parameters to initialize the object's data members. It allows you to provide initial values to the object during its creation.
Output:
Here,
- The parameterized constructor is called when an object of the
MyClass
class is created with the values 7 and 5.7. - The display function is called on the
obj
object to print its data members.
Constructors with Default Arguments
Constructors can also have default arguments, allowing you to create objects without providing values for all parameters. Default arguments are specified in the constructor declaration. Here's an example:
Output:
In this example:
- We have defined a class MyClass with a constructor that takes two parameters, x and y, with default values 0 and 0.0, respectively.
- In the main function, we create three objects of type MyClass using different ways:
- obj1: Created without any arguments, so default values are used.
- obj2: Created with only one argument, so the default value for num2 is used.
- obj3: Created with both arguments provided, so the provided values are used.
- We then display the values of each object using the display function.
Destructor In C++
In C++, a destructor is a special member function that is automatically called when an object goes out of scope or is explicitly deleted.
Its primary purpose is to release any resources allocated by the object after it is destroyed.
Destructors have the same name as the class preceded by a tilde (~)
.
For Example:
Output:
In this example:
- The
MyClass
class has a constructor that prints "Constructor called" when an object is created. - The destructor of
MyClass
prints "Destructor called" when the object is destroyed, which happens when the object goes out of scope at the end of the main() function.
Difference Between Constructor And Destructor
Constructor | Destructor |
---|---|
Automatically called when an object is created | Automatically called when an object is destroyed |
Helps to initialize the object of a class | Used to deallocate resources |
There can be multiple constructors in a class | There is always a single destructor in a class |
May accept arguments | Does not accept arguments |
Can be overloaded | Can't be overloaded |
Last updated on -