Classes and Objects with Constructors/Destructors: Memory Allocation for Object and Array of Object, Constructor

Get unlimited access to the best preparation resource for UGC : fully solved questions with step-by-step explanation- practice your way to success.

Download PDF of This Page (Size: 202K)

Image of Default Constructor

Image of Default Constructor

Memory Allocation for Object

  • The member function is created and placed in the memory space only once when they are defined in class specification.

  • All the objects belonging to that class use the same member functions.

  • Space for member variable is allocated separately for each object because member variable holds different value for different objects.

Array of Object

Consider the following program.


In the above program, the ten objects are created, namely, e [ 0], e [ 1] ---------- e [ 9].

The statement e [ i]. get data () will get the data of the ith element of the array e.


  • A constructor is a special member function that initializes the objects of its class.

  • It is special because its name is the same as the class name.

  • It is invoked automatically whenever an object is created.

  • It is called constructor because it constructs the values of data members of the class. It does not have any return type, not even void.

  • A constructor is declared and defined as follows:

  1. class student
  2. {
  3.     int rn;
  4.     int total ;
  5.     public
  6.     student ( )
  7.     {
  8.         rn = 0 ; total = 0 ;
  9.     }
  10. } ;

The declaration

student st.

Invokes the constructor, student () and assign run = 0 and total = 0.

Developed by: