+2 votes
in Class 12 by kratos

What is a constructor? Give the rules of writing a constructor function.

1 Answer

+1 vote
by kratos
 
Best answer

Constructor is a special member function that is used to initialize the data members of an object.

The rules for writing a constructor function are:

  • They should be declared in the public section
  • They are invoked automatically when the objects are created
  • They should not have return types, therefore they cannot return values
  • They cannot be inherited
  • They can have default arguments
  • These cannot be static

An object of a class with a constructor cannot be used as a member of a union.

...