Implications of Polymorphic use of Classes

Estudies4you
Q23. Discuss about the implications of polymorphic use of classes.
Answer:
The programmer must be aware of some of the problems encountered while programming because the polymorphic use of class instances.will force to manipulate the objects either through pointers or references. Some of the problems are discussed as follows,

Declaring Destructor as Virtual
Manipulation of objects through pointers require them to be created dynamically by using new operator and later on delete them through delete operator. Consider the below example,

class A
{
public A()
cout << “BaseClass Constructor” <endl;
}
~A()
{
cout << “Base Class Destructor” < endl;
};
class B: public A
{
public:
B()
{
cout << “Derived class constructor” << endl;
}
~B()
{
cout << “Derived class destructor” <<endl;
}
};
main()
{
A* a=new B;
delete a;
}
In the above example, a copy of derived class is created in main() by using the new operator and destroyed later on by using the delete operator. The output of this program is as shown below,
Baseclass constructor
Derived class constructor
Baseclass destructor.
The output is displayed based on the order in which the constructors are called.

The output does not show the destructor of derived class: This is because the derived class name is used with new operator to call the constructor, so the compiler created the specified object. The variable ‘a’ holds the pointer to base class. When this variable is defined with operator delete, the compiler could not predict that the programmer wants to destroy the derived class instance. This problem can be solved by declaring the destructor of base class as virtual. Now the destructor gets invoked through the virtual function table and the respective destructor will be called.

virtual ~A()
{
cout << “Baseclass Destructor” <<endl;
}
The output of the program when the above code is executed will be as shown below,
Base class constructor
Derived class constructor
Derived class destructor
Base class destructor.

Calling Virtual Functions in a Base class Constructor
The virtual functions may not work as expected some times. The undesired output of the virtual function call might result from the base class constructor. This problem is depicted by the below code,
class A
{
public:
A()
{
cout << “Base class constructor calls clone()” <<endl;
clone();
}
virtual void clone()
{
cout<< “Base class clone() called” <<endl;
}
};
class B: public A
{
public:
B()
{
cout << "Derived class constructor calls clone()" <<endl;
}
void clone()
{
cout << “Derived class clone() called” <<endl;
}
};
main()
{
Bb;
A*a=&b;
cout << “Call to clone() through instance pointer” <<endl;
a→clone();
}
The output of above code is as follows:
Baseclass constructor calls clone()
Base class clone() called
Derived class constructor calls clone()
Call to clone() through instance pointer
Derived class clone() called

As shown in the above output, the base class constructor is called first when a derived class instance is created. The derived class instance gets initialized partially. So, the compiler cannot bind the call to clone for derived class when virtual function clone() is called. Therefore, the compiler calls the clone() from base class.
The last two lines of output indicate that clone() function for derived class instance can be invoked using base class pointer upon object creation. This behavior is common in polymorphic use of virtual functions. The last line indicates that the compiler invokes base class version of function rather than that of derived class when virtual function is called in class constructor.

To Top