Default Constructor – Object Oriented Programming (OOP)

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

Default ConstructorDefault Constructor – Reference Note
Object Oriented Programming (OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

Default Constructor
A default constructor is a constructor that either has no parameter or it has parameter, 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 parameter less constructor A::A(). This constructor is an inline public member of its class. The compiler will implicitly define A::A() when the compiler uses this constructor to create an object of type A. The constructor will have no constructor initialize and a null body. For instance, consider the following example.

class A {
int i;
void getval(void);
void prnval(void);
//member function definitions
A ob1;
/* uses default constructor for creating ob1. Since user can use it, that means, this implicity defined constructor is public member of the class */

Having a default constructor simply means that an application can declare instances of the class. The compiler first implicitly defines the implicitly declared constructors of the base classes and non-static data members of a class A before defining the implicitly declared constructor of A. No default constructor is created for a class that has nay constant or reference type members.
A constructor of a class A is trivial if all the following statements are true;

  • It is implicitly defined
  • A has no virtual functions and no virtual base classes
  • All the direct base classes of A have trivial constructors
  • The classes of all the non-static data members of A have trivial constructors
  • The default constructor provided by the compiler does not do anything specific. It simply allocates memory to data members of the object.
(Visited 247 times, 1 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