Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if you don't call the base constructor from the derived constructor?

Is it always necessary to call base class constructor from derived class constructor? What happens when you don't call it?

like image 790
nafiz amin Avatar asked Dec 05 '10 20:12

nafiz amin


People also ask

What happens when a derived class constructor does not specify which base class constructor to call?

The appropriate Derived constructor is called. The Base object is constructed first using the appropriate Base constructor. If no base constructor is specified, the default constructor will be used. The member initializer list initializes variables.

What happens if you don't declare a default constructor?

If no constructors are explicitly declared in the class, a default constructor is provided automatically by the compiler.

Does base class constructor get called automatically?

Whenever the derived class's default constructor is called, the base class's default constructor is called automatically. To call the parameterized constructor of base class inside the parameterized constructor of sub class, we have to mention it explicitly.

What does derived class does not inherit from the base class constructor and?

In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.


1 Answers

Assuming you're talking about C++ (anyway, this should be similar in most other languages), if you don't call a constructor of the base class explicitly, its default constructor will be called automatically (if one exists; if not, the compiler would fire an error).

like image 161
Flinsch Avatar answered Nov 15 '22 07:11

Flinsch