twitter
    The place where CEO's meet !

Friday, July 24, 2009

25 points about c++ constructors

For many students including engineering and any bachelor degree in computers, C++ is an academic subject for their studies. Few people may need to attend for job interviews on C++ platforms. For all the above people this material would help to get comprehensive knowledge about C++ classes. Though I used to provide this material to my C++ students since past five years, I have decided to keep it in a global location, which is main intention behind keeping it here. 

  • Constructor is a member function of a class having its name as class name. 
  • Constructor can initialize the object (its member), and may build memory for the object members if required. 
  • Constructor is a special member function, and hence it can’t be defined with any return type. Not even void or int is allowed. 
  • Constructors can be defined with or without arguments.
  • Constructors can not be overridden.
  • Commonly constructors are defined as public or protected members.
  • In case if no constructor is explicitly defined for a class, then compiler supplies a constructor with no arguments and no code. It is commonly referred to as default constructor or implicit constructor.
  • In case if no constructor is explicitly defined for a class, then compiler supplies a constructor with no arguments and no code. It is commonly referred to as default constructor or implicit constructor.
  • Constructor acts like-cycle function for every object.
  • Constructor is automatically invoked during the creation of each object of its class. Constructors can be defined with default argument values. Such constructors are known as default argument constructors. 
    The class, whose objects are serialized or managed in object pool, must contain one constructor without any arguments. 
  • Constructors are classified into three types: • Default constructor • Copy constructor • Parameterized constructor.
  • A default constructor is either implicit or explicit constructor defined with no parameters(arguments)
  • Having a default constructor, a class can be instantiated (object can be created) implicitly or explicitly.
  • A copy constructor takes an object reference of same class type as arguments and copies the content of the passed object into created object.
  • Creating an object-clone with copy constructor differs from object assignment in one issue : Suppose the class contains one or few pointer members , and those are allocated with memory in the object that has to be copied into a new object .Assignment done as ‘new-object=old-object’ simply copies the address of those pointers from the old object to new object, without allocating memory and then performing content copy. In contrast we define a copy constructor to perform a copy after memory allocation to those pointers. 
  • Parameterized constructors are defined with one or few parameters (arguments) of any data types. There can be many parameterized constructors exist for a class, where prototypes and arguments differ.
  • A parameterized constructor defined with a single argument behaves like a type conversion routine to convert from any basic type to class type. It is automatically invoked during the assignment or explicit typecast from any type to this class.
  • Unlike ordinary member functions, constructors can not invoked directly without creating objects.
  • For the life-time of an object , its constructor is called only once i.e., when the object is created
  • Constructor can be written with code for initializing object members to their natural default values need not be mandatory with zeros.
  • If a base class contains one or few parameterized constructor without any default constructor ,then all its derived classes must define explicit constructors that call any one of its base class parameterized constructor with below syntax : 

     
     class BaseClass  
     { 
      public : 
      BaseClass(base args ......) {....} 
      // Base Class Constructor 
      ....... 
      } 
      class DerivedClass : public BaseClass  
      { 
      public : 
      DerivedClass(derived and base class .....) 
      : BaseClass(base args ......) 
      {
      ..... 
      }  
      ..... 
      }  
     

     
    Every derived class constructor implicitly or explicitly calls its base class constructor since, creating base class object is vital operation while creating derived class object. If explicitly no call is made to the base class constructor, its default constructor is called. 

     

No comments:

Post a Comment