CONSTRUCTORS AND DESTRUCTORS

 Constructors:
a) Constructors are class member functions which take the same as the class name with no return-type.
b) Constructors are used to assign values to the class data member variables.

Types of Constructors:
a) Default constructor
b) Parameterized constructor
c) copy constructor

a) Default Constructor:
                Default Constructor is called, when an object is created with no parameters.

b) Parameterized Constructor:
Parameterized Constructor is used to initialize values to data member variables during object creation.

c) Copy constructor:
An object can be initialized with an existing object during creation, using copy constructors.

Deep copy and Shallow copy:
    (i) The compiler provides a default copy constructor and an assignment operator.
   (ii) These built in functions provide shallow copy[each member value is assigned with the passed object's value].
  (iii) a) If a class has a pointer variable in it, the default copy constructor points to the same memory location of the passed object's data member.
b) When the passed object is deleted, the pointer in the new object will become a dangling pointer.
c) We need to override the default copy constructor with our own copy constructor to create a new memory location.
d) Assignment operator has to be overloaded, to initialize an object using an existing object.

class A
A a;
A b = a;  //Assignment operator is called
A c(a);   //copy constructor is called

Destructors:
a) The destructors are also a member function with the class name, but with a ~ preceding it.
b) Destructors are used to destruct or delete any memory allocated in the constructor.
c) The construction of a derived class object is like 
base constructor -> derived constructor
d) So the destruction of the object should be in the reverse order.
derived destructor -> base destructor
   For this, we need to declare the base class destructor as virtual.

Popular posts from this blog

OBJECT ORIENTED ANALYSIS AND DESIGN (OOAD)

OBJECT ORIENTED PROGRAMMING

STARTING A BUSINESS