class base { int i; public: base() { i = 10; cout << "in the constructor" << endl; } }; int main() { base a;// here is the point of doubt getch(); }
What is the difference between base a
and base a()
?
in the first case the constructor gets called but not in the second case!
You can just call default constructor with new operator (like this: new Test();) or this();. just Test() is forbidden because its not a method of class. Show activity on this post. In Java, the default constructor is the no-argument constructor that's implicitly provided by the compiler.
base a declares a variable a of type base and calls its default constructor (assuming it's not a builtin type). base a(); declares a function a that takes no parameters and returns type base .
In both Java and C#, a "default constructor" refers to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class. The default constructor implicitly calls the superclass's nullary constructor, then executes an empty body.
A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .
The second one is declaring a function a() that returns a base object. :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With