Classof1 logo
Fax: 1- 425- 458- 9358 | Toll free: 1- 877- 252 - 7763
Bookmark and Share
Forgot Password? Click Here
Register  |  Account

Need help with Computer Science assignment?

Get customized homework help now!

Constructors and Destructors

Constructors:  It is a special member function. This is used to give initial values to member variables. General form is

Constructor name (parameters)

{

statements;

}

Where,

constructor name – same as name of the class.

Parameters – it can be void or can have a list of arguments.

Rules:

  • It should be declared as public
  • No return type needed.

There are three types of constructors. They are

  • constructors without parameters
  • constructor with parameters
  • multiple constructors

a)constructors without parameters:

If a constructor has no parameters or arguments then it is called constructor without parameters.

b)constructor with parameters:

If a constructor has arguments then it is called constructor with parameters.

 The value of the parameters should be given along the object declaration.

General form:

Class_name object = constructor_name (argument_value_list);

Class_name – name of the defined class.

Constructor_name -  name of the defined class.

It can be used to initialize the member variables. This is called explicit call.

c) Multiple constructors:

If a class has more than one constructor then it is said to be Multiple constructors. This technique is called overloading constructors.

Constructors with default argument:

If we give initial values to the member variables inside the argument list of constructor then it is called constructor with default arguments.

General form:

a)Declaration:

Constructor_name (argument_type argument_variable = value1,    argument_type argument variable = value 2, ……..);

b)Definition:

Constructor_name (argument_type argument_variable = value1,    argument_type argument variable = value 2, ……..);

{

member_variable1 = argument_variable1;

member_variable2 = argument_variable2;

-  - - - - - - - - - -- - -- -- - - -- - - -- - - - - -

- - - - -- - -- - -- - -- -- - - - -- - - - -- -- -- --

}

At the time of object declaration, depending upon the value, the constructor automatically assigns the values to member variables. If any value is missing, this will be taken from the argument list values in the declaration or definition of constructor.

Dynamic initialization of objects:

Dynamic initialization of objects means giving initial values to the member variables during run time. With the help of this facility, we can give different type of values to the member variables depending upon the situation.

Rules:

  • More than one constructor needed.
  • It requires explicit constructor calling.
  • Depending upon the arguments type in the calling statement, appropriate constructor will be executed.

Copy constructors:

  • Copy constructors are used to declare and initialize an object with the values from another object.
  • Copy constructor declaration and definition:

General form:

Constructor_name (class_name & object_name)

{

 ----- - - -- - -- -- - -- - - -- - ----- - --

- - -- - - -- - -- - -- - - - - - - - - - - - -

- - - - -- - - - - - - - - - - - - - - - - - - --

}

where

&object_name – valid C++ object with values

Copy constructor calling:

General form:

Class_name new_object_name = old_object_name;

Where,

New_object_name –   object to be initialized

Old_object _name -     object already having classes.

When this is executed the values in the old object name are copied to new object name.

Rules:

  • Constructor name and class name should be same.
  • Constructor calling is possible only during object declaration.

Dynamic constructors:

If the memory space for the constructor is allocated during the constructor calling, then it is called dynamic constructor. This is done with the help of the operator new. Depending on the size of the values in the variable the memory occupation of the object varies.

Destructors:

Destructor is a member function used to deallocate the memory space allocated by the constructor. General form:

~ destructor name( )

{

- - - - - - - - - - -

- - - -- - - - - -- - -

-  -- - - -- - - - - - --     

 }

Where

~ (tilde) – destructor operator

destructor name – name of class

The compiler automatically deallocates the memory space when the program ends.

Rules:

  • It won’t take any arguments
  • It won’t return any value
  • If new operator is used inside the constructor for allocating memory, delete operator should be used inside the destructor to free the memory.
Computer Science Homework Help
Name* :
Email* :
Country* :
Phone* :
Subject* :
Upload Homework :
Upload another homework (upto 5 uploads max.)
Due Date
Time
AM/PM
Timezone
Instructions
(Type Security Code - case sensitive)