COMPUTER SCIENCE CONSTRUCTORS AND DESTRUCTORS

Constructor Overloading: The constructor of a class may also be overloaded so that even with different number and types of initial values, an object may still be initialized.

Default Arguments Versus Overloading: Using default arguments gives the appearance of overloading, because the function may be called with an optional number of arguments.

Eg: Prototype

float amount (float principal, int time=2, float rate=0.08);
Can be called as Amount(2000.0,4,0.10);
Amount(3520.5,3);
Amount(5500.0);

Special Chracteristics of Constructors:
  1. Constructor functions are invoked automatically when the objects are created.
  2. If a class has a constructor, each object of that class will be initialized before any use is made of the object.
  3. Constructor functions obey the usual access rules. Ie private and protected constructors are available only for member and friend functions, however, public constructors are available for all the functions. Only the functions that have access to the constructor of a class, can create an object of the class.
  4. No return type (not even void) can be specified for a constructor.
  5. They cannot be inherited, though a derived class can call the base class constructor.
  6. A constructor may not be static.
  7. Default constructors and copy constructors are generated(by the compiler) where needed. Generated constructors are public.
  8. Like other c++ functions, constructors can also have default arguments.
  9. It is not possible to take the address of a constructor.
  10. An object of a class with a constructor cannot be a member of a union.
  11. Member functions may be called from within a constructor.
  12. A constructor can be used explicitly to create new objects of its class type, using the syntax class-name(expression-list)

Eg: Sample obj1=Sample(13,22.42);

Destructor:A destructor is used to destroy the objects that have been created by a constructor. A destructor destroys the values of the object being destroyed. A destructor is also a member function whose name is the same as the class name but is preceded by tilde(~). A destructor takes no arguments, and no return types can be specified for it (not even void). It is automatically called by the compiler when an object is destroyed. A local object, local to a block, is destroyed when the block gets over; a global or static object is destroyed when the program terminates. A destructor cleans up the storage (memory area of the object) that is no longer accessible.

Eg:

class Sample
{ int i,j;
Public:
Sample(int a, int b) //Constructor
{ i=a; j=b; }
~Sample()
{ cout<<”Destructor at work\n”; }
------
------
};
void main( )
{
Sample s1(3,4); //Local object s1 constructed with values 3
// & 4 using Sample ( )
-----
----/*Automatically s1 is destructed at the end of the block using destructor ~Sample( )*/
}

Need for Destructors: During construction of any object by the constructor, resources may be allocated for use. (for example, a constructor may7 have opened a file and a memory area may be allotted to it). These allocated resources must be de allocated before the object is destroyed.A destructor performs these types of tasks.

Some Characteristics of Destructors:

  1. Destructor functions are invoked automatically when the objects are destroyed.
  2. If a class has a destructor, each object of that class will be deinitialized before the object goes out of scope.(Local objects at the end of the block defining them and global and static objects at the end of the program).
  3. Destructor functions also, obey the usual access rules as other member functions do.
  4. No argument can be provided to a destructor, neither does it return any value.
  5. They cannot be inherited.
  6. A destructor may not be static.
  7. It is not possible to take the address of a destructor.
  8. Member functions may be called from within a destructor.
  9. An object of a class with a destructor cannot be a member of a union.

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )