Coupon Accepted Successfully!


Calling Constructors and Destructors

The compiler automatically calls the constructor and destructor. We can also call the constructor and destructor in the same fashion as we call the normal user-defined function. The calling methods are different for constructors and destructors. In practice, it may not be useful; but for the sake of understanding, few examples are illustrated on this concept.

9.15 Write a program to invoke constructor and destructor.

Explanation: In the above program, the class byte is declared with two integer members. The class has constructors and destructors. In the function main(), x is an object of class byte. When an object is created, the compiler automatically invokes the constructor. The program also invokes constructor. Thus, constructor is executed two times, that is, implicitly and explicitly. The constructor is called without the help of object of that class. If we try to call the constructor with the help of object x as per the statement x.byte(), the compiler displays an error message.

The destructor cannot be invoked like constructor. Calling destructor requires the help of object and class name. Only object of the class alone is not able to invoke the destructor, the object needs the help of the class. The statement x.~byte() is invalid, the compiler displays the error message Member identifier expected. The destructor can be invoked using the statement x.byte::~byte(). Here, in addition, class name is also specified.

The constructor and the destructor can call each other. The statement byte::byte() is used for calling destructor ~byte() within the body of constructor byte(). The destructor ~byte() can call the constructor byte() using the statement byte(). The constructor and the destructor may contain condition statements; such destructors or constructors are called conditional destructor or conditional constructor, respectively. The conditional constructor can be created using if-else or switch() statements. The example given below illustrates this.


9.16 Write a program to define conditional constructor and destructor.

Explanation: In the above program, x and y are objects of class byte. The compiler automatically invokes the constructor. The constructor invokes the destructor. The variable c is declared and initialized with zero. It is a global variable declared before main() and can be used anywhere in the program. The destructor contains conditional statement if. The if block is executed only when c is zero. When destructor is called the first time, the if block is executed and c is turned to a non-zero value. Next time when destructor is called, the if condition evaluates false and the if block is not executed. Though destructor is called, no result is displayed on the screen. Remember that the constructors and destructors are executed for equal number of times. It is possible for the programmer to use the constructor and the destructor like other user-defined functions.

Test Your Skills Now!
Take a Quiz now
Reviewer Name