Page 203 - DCAP404 _Object Oriented Programming
P. 203
Object-oriented Programming
Notes r.get (6);
r.show();
}
Self Assessment
Fill in the blanks:
13. …………………………… offer a way to save space and avoid ambiguities in class hierarchies
that use multiple inheritance.
14. Each non-virtual object contains a copy of the ……………………….. defined in the base
class.
9.5 Overriding Member Function
If there are two functions with the same name in the base class and derived class and even the
address of derived class is assigned to the pointer of base class; it executes the function of base
class. But by declaring the base class function as virtual, pointer to base class executes the
function of derived class. And in this way base class pointer behaves differently at different
situation and applies the concept of polymorphism. This is also called run time or dynamic
polymorphism because when a function made virtual, C++ determines which function to use at
run time based on the type of object pointed to by the base pointer, rather than the type of the
pointer.
class base
{
public:
virtual void display();
virtual void show();
};
void base::display()
{
cout<<“\nbase class display() function”;
}
void base::show()
{
cout<<“\nbase class show() function”;
}
196 LOVELY PROFESSIONAL UNIVERSITY