Previous Page C++ Interview Questions Home Next Page
Q.1. What is default constructor?
Default Constructor is a special function which if present automatically gets executed whenever an object of a class is declared without any parameters.
Q.2. Explain default constructor in detail?
Default Constructor has following properties:
- Name of the default constructor function is exactly same as that of the class.
- It has no return type not even void
- If present it automatically gets executed whenever an object of a class is declared without any parameters.
- It cannot be invoked using an object of the class.
- It is generally used to initialize the data elements of an object at the time of declaration by user defined values.
- We do not pass any arguments in default constructor.
- Default constructor cannot be overloaded.Default constructors gets called only once for an object when it is declared without parameters.
Q.3. What is Parameterized constructor ?
Parameterized Constructors are special functions which if present automatically get executed whenever an object of a class is declared with parameters.
Q.4. Explain Parameterized constructor in detail?
Parameterized Constructor has following properties:
- Name of the parameterized constructors function is exactly same as that of the class.
- It has no return type not even void.
- If present it automatically gets executed whenever an object of a class is declared with parameters.
- It cannot be invoked using an object of the class.
- It is generally used to initialize the data elements of an object by the arguments passed at the time of declaration.
- There can be number of parameterized constructors in the class. Parameterized constructors can be overloaded.
Q.5. What are different ways in which parameterized constructor gets invoked?
Parameterized constructor gets invoked in two ways.
1. Implicit calling
2. Explicit calling
- Implicit calling is the default process. Whenever an object of a class is declared with parameters the parameterized constructor automatically gets called.
Example:
Student s(101,”hello”,90);
- In explicit calling we have to mention the name of the constructor to be invoked.
Example:
student s=student(101,”hello”,10);