[#14] – Virtual Functions – Questions

C-plus-plus-programming-questions-techhyme

Polymorphism is a very powerful concept that allows the design of amazingly flexible applications. Polymorphism can be defined as one interface multiple methods which means that one interface can be used to perform different but related activities.

The concept of function overloading and operator overloading as they provide single interface for performing different but related tasks.

Index:

In case of function overloading, the single interface is the name of function and in operator overloading, the single interface is the operator symbol.

Key Points To Remember –

  1. Polymorphism is derived from two Greek words poly which means many and morphs which means forms. Polymorphism is defined as the ability of objects of different classes to respond to the same request in its own way.
  2. Polymorphism reduces the complexity by allowing same name to perform different tasks.
  3. Depending on the type of binding, polymorphism can be classified into two types
    • Compile time polymorphism
    • Run time polymorphism
  4. A pointer to a class can be used to access a member of the derived class, as long as that class member is inherited from the base class.
  5. A class is made abstract by declaring one or more of its virtual functions to be pure. An object of the abstract class cannot be created.
  6. If a class is derived from a class with virtual function and that derived class doesn’t redefine the pure virtual function then that virtual function remains pure in the derived class. As a result, a derived class also becomes an abstract class.
  7. Addresses of different derived class objects can be stored in an array of base class pointers, to call functions dynamically.
  8. When the base class destructor is declared virtual the if the object in the hierarchy is destroyed explicitly by applying a delete operator to a base class pointer pointing to a derive class object, the destructor for the appropriate class is called.
  9. this pointer is a special pointer which is automatically passed as hidden first argument to my member function whenever it is called in association with an object.

Viva Voce Questions – 

Here are commonly asked questions with answers:

  1. What are the benefits of polymorphism ?
    • The main benefit of polymorphism is that it reduces complexity by allowing same named member function to perform different tasks. Also, it is possible to design and implement systems which are easily extensible.
  2. How compile time polymorphism is implemented in C++ ?
    • Compile time polymorphism is implemented using function overloading and operator overloading.
  3. How run time polymorphism is implemented in C++ ?
    • It is implemented using virtual functions.
  4. How does abstract classes differ from concrete classes ?
    • A class which cannot be instantiated and contain at least one pure virtual function is called abstract class. However, a concrete class can be instantiated.
  5. How can compile time polymorphism be distinguished from run time polymorphism ?
    • In compile time polymorphism, static binding is performed in which decision regarding selection of appropriate function to be called in response to function call is made by compiler at compile time. However, in run time polymorphism, there is dynamic binding in which case compiler makes the binding decision at run time.
  6. Can a pointer to the base class point to an object of the derived class ?
    • Yes
  7. Can we declare a static function as virtual ?
    • No, because declaring static function as virtual does not in any way implement virtual function mechanism as static function is not related to objects and is invoked through the class name.
  8. Does a virtual function declared in the base class becomes virtual in the derived class when it is overridden ?
    • Yes
  9. What is the difference between overloaded and overridden functions ?
    • Overloading occurs when two or more functions have the same name but different parameter list but overriding occurs when a class and one of its derived classes both define a member function with the same name, parameter list and the member function is declared to be virtual in the base class.
  10. How many arguments are passed in the following member function call ?
    obj1.compute(i);
    where obj1 is object of class having member function compute().

    • There are two arguments passed in the compute() member function call. The first one is the address of the object that generates the member function call implicitly and other one is the argument i passed explicitly. The address of the passed object is received in the called function by this pointer which enables the function to understand which object generates the call.

Other Similar Questions – 

  1. Define Polymorphism ? What are the different types of polymorphism .
  2. Define binding ? Distinguish between static and dynamic binding?
  3. What are the benefits of Polymorphism ?
  4. Can the pointer of base class point to the object of its derived class ? If yes. why C++ compiler supports this type incompatibility ?
  5. What is virtual function ? Describe the situations in which virtual functions are needed.
  6. What are pure virtual functions ? How are they declared .
  7. How do the pure virtual functions differ from normal virtual functions ?
  8. What are abstract classes ? Describe their importance.
  9. What are virtual destructors ? How are they distinguished from normal destructors.
  10. What are the rules that need to be kept in mind while making virtual functions ?
  11. What is ‘this’ pointer ? What are its applications ?
  12. When do we use ‘this’ pointer ?
  13. State with reasons whether the following statements are TRUE or FALSE :
    • Pointer of a base class can point to objects of a derived class but reverse is not true.
    • A class with pure virtual function can be instantiated.
    • Abstract classes are normally at the top of class hierarchy.
    • It is necessary to override the pure virtual function in the derived class.
    • This pointer should be used when the parameter names are same as that of the data members.
    • Polymorphism is supported by C++ only at compile time.
    • Compile time polymorphism is achieved by function overloading and operator overloading.
    • When a pure virtual function is not redefined by a derived class, then that derived class must be a abstract class.
    • Abstract class may be instantiated.
    • Version of a virtual function executed is determined by the type of object pointed to at the time of call ?

Leave a Reply