• As like standard data types, there must be a provision of initializing objects of a class during their definition itself.
• A class in C++ may contain two special member functions dealing with the internal working of a class.
• These functions are the constructors and the destructors.
• A constructor enables an object to initialize itself during creation and the destructor destroys the object when it is no longer required, by releasing all the resources allocated to it.
• A constructor is a special member function of a class whose task is to allocate the required memory for the object when they are instantiated(created), as well as initialize the objects of its class.
• A constructor has the same name as its class.
• A constructor has no any return type.
• Look at the definition syntax above and notice the name of constructor , it is same as class name i. e. sample() here and there is no return type like other functions
• The constructor is such a member function which is executed automatically when an object is created for that class.
• When an object is created, the following process will take place.
– The object occupies space at a particular time. Instantiation of an object always involves reserving enough memory space for the data of that object.
– The object instantiation does not reserve the memory for the methods. They exist only once for class, not once for every object.
• In addition to the reservation of space, the constructor may also be extended to other processes for initialization of data of that object.
• Constructor is a function (member) which is called automatically when object is created.
• In other words, the constructor is executed every time an object of that class is defined.
• It is of course possible to define a class which has no constructor at all, in such a case, the run-time system calls a default constructor that performs no action when its object is created.
Constructor Example
Uniform initialization
• The way of initialization objects(calling constructors by enclosing their arguments in parentheses), as shown above, is known as functional form. But constructors can also be called with other syntaxes:• First, constructors with a single parameter can be called using the variable initialization syntax (an equal sign followed by the argument) as below:
class_name object_name = initialization_value;
• More recently, C++ introduced the possibility of constructors to be called using uniform initialization, which essentially is the same as the functional form, but using braces ({}) instead of parentheses (()):
class_name object_name { value, value, value, ... }
• Optionally, this last syntax can include an equal sign before the braces.
Member initialization in constructor
• When a constructor is used to initialize other members, these other members can be initialized directly, without resorting to statements in its body.• This is done by inserting, before the constructor's body, a colon (:) and a list of initializations for class members.
• For example, consider a class with the following declaration:
• Note how in this last case, the constructor does nothing else than initialize its members, hence it has an empty function body.
Pointer to Class
• Objects can also be pointed to by pointers: Once declared, a class becomes a valid type, so it can be used as the type pointed to by a pointer. For example:Rectangle * prect; // definition of pointer to an object of class Rectangle
• Similarly as with plain data structures, the members of an object can be accessed directly from a pointer by using the arrow operator (->). Here is an example with some possible combinations:
Types of Constructor
• There are three types of constructors:–The default constructor
–User-defined constructor
• Parameterized and Non Parameterized Constructor
–Copy constructor
Default constructor
• This constructor is called implicit constructor.• The compiler provides a (hidden) default constructor that has no arguments.
• The default constructor takes no arguments and performs no processing other than reservation of memory for object.
• This constructor is always called by compiler if no user-defined constructor is provided.
• This constructor is automatically called while creating the object.
User-defined constructor
• If initialization of data of objects is required while creating an object, then the programmer has to define his own constructor for that purpose.• The name for constructor should always be same as class name.
• The code of a user-defined constructor does not actually cause memory to be reserved for the data and it is concern only for initialization of data.
• It is possible to have more than one constructor in a class providing that different versions are defined by different in the argument list.
• This gives more flexibility in the way when object of a class are instantiated. This is known as constructor overloading.
User-defined-Parameterized and Non-parameterized
• The user defined constructor can be– Parameterized constructor: Having arguments to pass value for initialization of object
– Non-parameterized Constructor: Having no any arguments for initialization
• Look at example below:
Copy constructor
• The copy constructor creates an object as an exact copy of another object in terms of its attributes.• In general, the parameter of a constructor can be of any of the data types except an object of its own class as value parameter.
• However, a class’s own object can be passed as a reference parameter. The constructor having a reference parameter is known as copy constructor.
• In copy constructor, one newly instantiated object equals another existing object of the same class.
• It uses the = (assignment) operator when creating new object or object can be passed as an argument.
• The newly created object inherit the attributes of existing object. The general syntax of copy constructor is as below
Copy Constructor: Example
Destructors
• When an object is created the constructed is called automatically.• Like wise a class can have another special member function called the destructor.
• When an object is no longer needed it can be destroyed. Destructor is invoked when an object is destroyed.
• This function complements the operation performed by any of the constructors.
• Like a constructor, the destructor is a special member function whose name is the same as the class name but is preceded by a tilde (~).
~class_name(); // destructor
• A destructor never takes any argument nor does it return any value. It will be invoked implicitly by the compiler upon exit from the program to clean up storage that is no longer accessible.
0 Comments