Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling the default constructor

Tags:

c++

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!

like image 427
Vijay Avatar asked Mar 14 '11 14:03

Vijay


People also ask

How do you call a default constructor?

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.

How do you call a default constructor in C++?

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 .

What happens when default constructor is called?

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.

Why is it called a default constructor?

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() .


1 Answers

The second one is declaring a function a() that returns a base object. :-)

like image 57
Bo Persson Avatar answered Sep 28 '22 08:09

Bo Persson