| 1. |
Explain the role of a default constructor? When is it considered equivalent to a parameterized constructor? Support your answer with examples. |
|
Answer» A default constructor is the one that takes no argument. It is automatically invoked when an object is created without providing any initial values. In case, the programmer has not defined a default constructor, the compiler automatically generates it. For example, class A {...........}; A ob1; // uses default constructor for creating ob1. A parameterized constructor with default argument is equivalent to a default constructor. For example, class A { int i; float j; public: A(int a = 0,float b = 1000.0); //constructor with default argument }; A::A(int a,float b) //constructor definition { i = a; j = b; } int main() { A o1(23,27.50); // argument value passed for o A o2; // takes default argument to o2(0,1000.0) } |
|