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.
- [#1] – Introduction to C++ – Questions
- [#2] – Preparing and Running C++ Program – Questions
- [#3] – C++ Fundamentals – Questions
- [#4] – Data Input and Output – Questions
- [#5] – Control Structures – Questions
- [#6] – Functions – Questions
- [#7] – Arrays and Strings – Questions
- [#8] – Structures and Unions – Questions
- [#9] – Pointers – Questions
- [#10] – Object and Classes – Questions
- [#11] – Constructors and Destructors – Questions
- [#12] – Operator Overloading and Type Conversion – Questions
- [#13] – Inheritance – Questions
- [#14] – Virtual Functions – Questions
- [#15] – C++ Streams and Files – Questions
- [#16] – Templates – Questions
- [#17] – Exception Handling – Questions
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 –
- 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.
- Polymorphism reduces the complexity by allowing same name to perform different tasks.
- Depending on the type of binding, polymorphism can be classified into two types
- Compile time polymorphism
- Run time polymorphism
- 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.
- 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.
- 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.
- Addresses of different derived class objects can be stored in an array of base class pointers, to call functions dynamically.
- 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.
- 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:
- 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.
- How compile time polymorphism is implemented in C++ ?
- Compile time polymorphism is implemented using function overloading and operator overloading.
- How run time polymorphism is implemented in C++ ?
- It is implemented using virtual functions.
- 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.
- 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.
- Can a pointer to the base class point to an object of the derived class ?
- 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.
- Does a virtual function declared in the base class becomes virtual in the derived class when it is overridden ?
- 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.
- How many arguments are passed in the following member function call ?
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 –
- Define Polymorphism ? What are the different types of polymorphism .
- Define binding ? Distinguish between static and dynamic binding?
- What are the benefits of Polymorphism ?
- Can the pointer of base class point to the object of its derived class ? If yes. why C++ compiler supports this type incompatibility ?
- What is virtual function ? Describe the situations in which virtual functions are needed.
- What are pure virtual functions ? How are they declared .
- How do the pure virtual functions differ from normal virtual functions ?
- What are abstract classes ? Describe their importance.
- What are virtual destructors ? How are they distinguished from normal destructors.
- What are the rules that need to be kept in mind while making virtual functions ?
- What is ‘this’ pointer ? What are its applications ?
- When do we use ‘this’ pointer ?
- 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 ?