Recently MeroSpark is lunched with more features and services, now you can ask your question, sell your books, share your notes and many more. Visit now and create your account to take full advantage of MeroSpark.

Characteristics and Limitations of Constructors – Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

characteristics and limitations of constructorsCharacteristics and Limitations of Constructors
Object Oriented Programming (OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

Characteristics of Constructors
A constructor for a class is needed so that the compiler automatically initializes an object as soon as it it created. A class constructor if defined is called whenever a program creates an object of that class. The constructor functions have some special characteristics which are as follows;

  • They should be declared in the public section.
  • They are invoked directly when an object is created.
  • They don’t have return type, not even void and hence can’t return any values.
  • They can’t be inherited; through a derived class, can call the base class constructor.
  • Like other C++ functions, they can have default arguments.
  • Constructors can’t be virtual.
  • Constructors can be inside the class definition or outside the class definition.
  • Constructor can’t be friend function.
  • They can’t be used in union.
  • They make implicit calls to the operators new and delete when memory allocation is required.
  • When a constructor is declared for a class, initialization of the class objects becomes necessary after declaring the constructor.

Limitations of Constructor:
C++ constructors have the following limitations:

No return type
A constructor cannot return a result, which means that we cannot signal an error, during the object initialization. The only way of doing it is to throw an exception from a constructor.

A constructor should have the same name as the class, which means we cannot have two constructors that both take a single argument.

Compile time bound
At the time when we create an object, we must specify the name of a concrete class which is known as compiler time. There is no way of dynamic binding constructors at run time.

There is no virtual constructor
We cannot declare a virtual constructor. We should specify the exact type of the object at compile time, so that the compiler can allocate memory for that specific type. If we are constructing derived object, the compiler calls the base class constructor first and the derived class hasn’t been initialized yet. This is the reason why we cannot call virtual methods from the constructor.

(Visited 13,643 times, 5 visits today)

Posted By : Digvijay | Comment RSS | Category : Third Semester
Tag :

Post a Comment

Your email is never published nor shared. Required fields are marked *


Wordpress DMCA
Community | Toolbar | Android App | Founder/Developer : Hari Prasad Chaudhary | CSIT Portal Manager : Digvijay Chaudhary