Page 228 - DCAP404 _Object Oriented Programming
P. 228
Unit 10: Virtual Functions and Polymorphism
//pure virtual functions ‘ Notes
#include <iostream.h>
class base {
private :
int x, y;
pubic:
virtual void getdata ( ) =0;
virtual void display ( )=0;
};
class derivedB : public base {
Data Members
Member Functions
};
Self Assessment
Fill in the blanks:
6. Since pure virtual function has no body, the programmer must add the notation
………………………. for declaration of the pure virtual function in the base class.
7. A pure virtual function simply acts as a placeholder that is meant to be redefined by
…………………….. classes.
8. A pure virtual function is declared, but not necessarily defined, by a …………………..
class.
10.3 Abstract Classes
An abstract class is a class containing a pure virtual function. They cannot be instantiated into an
object directly. Abstract classes are designed to be specifically used as base classes. An abstract
class contains at least one pure virtual function. Only a subclass of an abstract class can be
instantiated directly if all inherited pure virtual methods have been implemented by that class
or a parent class.
Abstract classes provide a mechanism to implement multiple inheritance. In other OOP languages
the concept of interface does the same thing. Pure virtual functions are also used where the
method declarations are being used to define an interface for which derived classes will supply
all implementations.
An interface can be constructed with an abstract class having only pure virtual functions, and no
data members or ordinary methods. Use of purely abstract classes as interfaces works in C++ as
it supports multiple inheritance.
Notes Many OOP languages including Java do not support multiple inheritance and
therefore they provide a separate interface mechanism.
LOVELY PROFESSIONAL UNIVERSITY 221