Sunday 2 February 2014

C++ Constructor and Destructor

C++ Constructor:
Constructors are the special type of member function that initializes the object automatically when it is created Compiler identifies that the given member function is a constructor by its name and return type. 
  • It can have parameters like any member function.
  • Constructor functions are usually declared in the public section, but can also be declared in the protected and private sections, if the user wants to restrict access to them.
  • Constructor has same name as that of class and it does not have any return type and executed whenever we create new objects of that class.
  • The constructor has two parts:
      First is the initializer list which follows the parameter list and before the method body. It starts with a colon and entries are comma-separated. The initializer list is not required, but offers the opportunity to provide values for data members and avoid separate assignment statements. The initializer list is required if you have const or reference type data members, or members that do not have parameterless constructor logic. Assignments occur according to the order of the initializer list. 

    The second part is the body, which is a normal method body enclosed in curly brackets.


Constructor Types : means constructor can be over loaded like member function:

1. Default Constructor: A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.
Example:
class Cons{};

If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless 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 initializer and a null body.

The compiler first implicitly defines the implicitly declared constructors of the base classes and nonstatic data members of a class A before defining the implicitly declared constructor of A. No default constructor is created for a class that has any constant or reference type members. 

A constructor of a class A is trivial if all the following are true:

  • It is implicitly defined Cons has no virtual functions and no virtual base classes.
  • All the direct base classes of Cons have trivial constructors. 
  • The classes of all the nonstatic data members of Cons have trivial constructors
If any of the above are false, then the constructor is nontrivial.

2. Copy Constructor:  for a class Cons is a constructor whose first parameter is of type Cons&, const Cons&. Copy constructors are used to make a copy of one class object from another class object of the same class type. You cannot use a copy constructor with an argument of the same type as its class; you must use a reference.


3. Parametrized Constructor: A default constructor does not have any parameter, but if you need, a constructor can have parameters. 


The following code fragment shows two classes with constructors, default constructors, and copy constructors, initializer :

class Cons1

 {
int length, breadth;
public:
  // default constructor, no arguments
  Cons1();
  // Parametrized constructor, assign value using assignment operator
  Cons1(int x, int y)
 {
      length = x;
     breadth = y;
 }
// Parametrized constructor, assign value using Initializer list
//  Cons1(int x, int y) : length(x),  breadth(y) { }

  // copy constructor

  Cons(const Cons&);
};

class Cons2 {

public:
  // default constructor with one
  // default argument
  Cons2( int a= 0);

  // default argument

  // copy constructor
  Cons2(const Cons2&, int a = 0);

};


C++ Destructor: updated coming soon.

No comments: