Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
3.9k views
in Computer by (64.2k points)

Explain the role of a default constructor? When is it considered equivalent to a parameterized constructor? Support your answer with examples.

1 Answer

0 votes
by (69.4k points)
selected by
 
Best 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)

}

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...