Machine-translated by Google. Questions are authored in English.
Top

Discussion

In C++, which function has the same name as the class and is called automatically when an object is created ?

  • A.Destructor
  • B.Main function
  • C.Constructor
  • D.Virtual function

Answer: C

The function is the constructor.

A C++ constructor has the same name as its class, has no return type, and is called automatically whenever an object of the class is created, to initialise its data members. A class may have several constructors with different parameters.

The destructor also takes the class name, but with a tilde (~ClassName), and it runs when an object is destroyed, not created. main is the program's entry point, and a virtual function is one that can be overridden in a derived class.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.