9.1

What does polymorphism mean in C++ language?

Answer

In short, polymorphism means one thing with several district forms.
In details, using operators or functions in different ways, depending on what they are
Operating on, is called polymorphism.

9.2

How is polymorphism achieved at (a) compile time, and (b) run time?

Answer

Polymorphism can be achieved at compile time by early binding. Early binding means an object is bound to its function call at compile time.
And we can achieve run time polymorphism by a mechanism known as virtual function.

9.3

Discuss the different ways by which we can access public member functions of an object.

Answer
We can access public member functions of an object by
  • (i) Object name and dot membership operator.
  • (ii) Pointer to object and function name.
9.4

Explain, with an example, how you would create space for an array of objects using pointers.

Answer

We can also create an array of objects using pointers. For example, the statement
item *ptr = new item [10]; // array of 10 objects.
creates memory space for an array of 10 objects of item.

9.5

What does this pointer point to?

Answer

‘this’ pointer point to invoking object.

9.6

What are the applications of this pointer?

Answer

One important application of the pointer this is to return the object it points to. For example, the statement.
return * this;
inside a member function will return the object that invoked the function.

9.7

What is a virtual junction?

Answer

When we use the same function name in both the base and derived classes the function in the base class is declared as virtual using the keyword virtual preceding its normal declaration.

9.8

Why do we need virtual functions?

Answer

It we need same function name at base class and derived class then, we need virtual function.

9.9

When do we make a virtual function “pure”? What are the implications of making a function a pure virtual function?

Answer

When a function is defined as empty, then this function is called do nothing function.
The implications of making a function a pure virtual function is to achieve run time polymorphism.

9.10

State which of the following statements are TRUE or FALSE.

  • (a) Virtual functions are used to create pointers to base classes.
  • (b) Virtual functions allow us to use the same junction call to invoke member functions of objects of different classes.
  • (c) A pointer to a base class cannot be made to point to objects of derived class.
  • (d) this pointer points to the object that is currently used to invoke a function.
  • (e) this pointer can be used like any other pointer to access the members of the object it points to.
  • (f) this pointer can be made to point to any object by assigning the address of the object.
  • (g) Pure virtual functions force the programmer to redefine the virtual function inside the derived classes.
Answer
  • (a) TRUE
  • (b) TRUE
  • (c) FALSE
  • (d) TRUE
  • (e) TRUE
  • (f) TRUE
  • (g) TRUE

Next Previous