What does polymorphism mean in C++ language?
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.
How is polymorphism achieved at (a) compile time, and (b) run time?
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.
Discuss the different ways by which we can access public member functions of an object.
- (i) Object name and dot membership operator.
- (ii) Pointer to object and function name.
Explain, with an example, how you would create space for an array of objects using pointers.
We can also create an array of objects using pointers. For example, the statement
item *ptr = new item ; // array of 10 objects.
creates memory space for an array of 10 objects of item.
What does this pointer point to?
‘this’ pointer point to invoking object.
What are the applications of this pointer?
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.
What is a virtual junction?
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.
Why do we need virtual functions?
It we need same function name at base class and derived class then, we need virtual function.
When do we make a virtual function “pure”? What are the implications of making a function a pure virtual function?
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.
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.
- (a) TRUE
- (b) TRUE
- (c) FALSE
- (d) TRUE
- (e) TRUE
- (f) TRUE
- (g) TRUE