Inheritance, Virtual Functions and Polymorphism-2

Estudies4you
Q5. Write about static and dynamic binding.
Answer:
Static Binding
Static binding or early binding is achieved through function overloading or operator overloading.
Members with same name possessing different arguments are known to the compiler during compilation time. They help the compiler to find the function definition with appropriate function call. This type of binding is also called compile time binding.

Dynamic Binding
Dynamic binding or late binding binds a function definition to an appropriate function call during run time. This type of binding is also called run time binding. Dynamic binding of member functions is achieved using virtual keyword.

Q6. Write a short note on virtual functions.
Answer:
Virtual functions are the functions whose declarations precede with a keyword ‘virtual’ to determine which version of a function must be invoked at the runtime. The choice of the function’s version depends upon the type of the object to which the base class pointer points. It doesn’t depend on the type of the pointer. Hence, various virtual function versions can be executed by pointing the base pointer to different objects. Virtual functions can be accessed through the base class pointers.

Q7. Define pure virtual functions.
Answer:
Pure virtual functions can be defined as the functions that are declared inside a base class without any relative definition. Hence, each derived class must re-declare it as a pure virtual function or redefine it.
They are also called as “do-nothing” functions as their definitions are empty and they are of the form,
virtual void display() = 0;
The display() in the above declaration is a pure virtual function with no definition relative to the base class. The assigned operator does not specify that zero is assigned to this function, instead it is used to tell the compiler that the declared function is a pure virtual function and that it will not have a definition.

Q8. What are Abstract classes?
Answer:
Abstract classes are the classes that contain only function declaration, but not its definition. That is, it provides just the skeleton of the class hiding the implementation details.
These classes are extendable and can even contain virtual functions which help the programmer to debug the bugs.
Abstract classes are defined in a header file called abstract.h.

Q9. What is polymorphism?
Answer:
Polymorphism is an important concept in OOP. It is derived from the Greek word poly (multiple) and morphism (forms) which together mean multiple forms. It is a method through which an operation/function can take several forms based on the of objects. An individual operator/function can be used in multiple ways. Consider the addition of two integer variables a and b stored in the function ‘sum’
sum = a + b
Let,a = 3, b = 4
Then,
sum =3+4
Then by the principle of polymorphism, addition of float variables with same function name is possible. This can be done as follows,
sum = 3.0 + 4.0
This is similar to function overloading where the same name is used in multiple functions that perform multiple operations.

10. Write short notes on virtual destructors.
Answer:
Virtual destructor is created by placing virtual keyword before a destructor. Its implementation is similar to the implementation of virtual constructors. The hierarchy of base and derived classes are created in constructors and destructors. From this hierarchy, “derived and base class object reference by base class pointer when a derived class object is constructed using a new operator. Its address is stored in base class pointer object. This base class pointer object is destructed by using delete operator by invoking this operator.

To Top