Object Oriented Programming Using
C++
Abstract Classes and pure Virtual Functions
Abstract Classes
u Some classes exist logically but not physically.
u Sometimes, we need a class only to provide a
base class for later inheritance. i.e no objects of
the base class are required for such purpose we
create ABSTRACT CLASS
Pure virtual function
u If you want to make two separate classes related like this,
but the parent would make no sense to ever be instantiated,
you can use pure virtual functions
u A pure virtual function is a function that has no definition,
and requires that any child must overload it.
u In the declaration of a class, a pure virtual function is
declared by typing:
virtual void print() = 0;
Pure virtual function
u You might define base class shape, from which you would
derive classes defining specific shapes, such as circle,
ellipse, rectangle, curve, etc.
u The shape class might include virtual function draw() that
you would call to draw particular shape,
u But the shape class itself is abstract: there is no
meaningful implementation of the draw() function for the
shape class. This is a job for a pure virtual function.
Pure virtual function
u The primary purpose of the pure virtual function is to
enable the derive class versions of the function to be
called polymorphically.
u A pure virtual function has no implementation.
Example
Class shape
{
Protected:
int xpos, int ypos;
public:
shape(int x, int y):xpos(x),ypos(y){
}
virtual void draw() const=0;
virtaul void move( int x,int y)=0;
};
Abstract Classes
u Any class that contains at least one pure virtual function is
called an Abstract Class.
u Even though it has constructor and data member still it is
incomplete because draw() and move() are not defined.
u We are not allowed to create instances of the shape class.
u It exists purely for the purpose of defining any classes that
are derived from it.
Abstract Classes
u Since we cant create objects of the abstract class we
cannot use it as function parameter type or return
type.
u But pointer or references to an abstract class can be
used as parameter or return types
Constructors and Abstract Classes
u If you cant instantiate abstract class than why does it
contain the constructor???.
u The constructor for abstract class is there to initialize its
data members.
u To make this work its constructor will be called through
derived class constructor.
u If you try to call the constructor for abstract class from
anywhere else you get an error message from the
compiler
Abstract Classes
u Constructor for the abstract class must not call
pure virtual function the effect of calling a pure
virtual function would be undefined.
Examples
Class shape
{
Protected:
int xpos, int ypos;
public:
shape(int x, int y):xpos(x), ypos(y)
{
greetings(); Error
}
virtual void greetings( ) const=0;
virtual void draw() const=0;
virtual void move( int x, int y)=0;
};
Examples
class shape class circle: public shape
{ {
public:
protected: circle( int x, int y) : shape(x, y) { };
int xp; int yp; virtual void draw()const
{
public: cout<<"circle center"
shape(int x, int y):xp(x), yp(y) { } <<xp<<" "<<yp;
virtual void draw() const=0; }
virtual void move( int x, int y)=0; private:
double radius;
};
};
Main()
{ shape s(2,3); [Error] cannot declare variable 's' to be of abstract type 'shape'
circle c(2,2); Error: virtual move also needs to be override in derived class
shape *s;
}
Abstract Classes
u Any class that derives from Shape class must define
both draw() and move() functions if it is not also the
abstract class.
u If pure virtual function is not defined than it is inherited
as such in the derived class. And the derived class will
also be an abstract class.
Examples
class Shape class Circle: public Shape
{ {
public:
protected: Circle( int x, int y) : Shape(x, y) { };
int xp; int yp;
virtual void draw()const
public: {
Shape(int x, int y):xp(x), yp(y) cout<<"circle center" <<xp<<"
{ "<<yp;
} }
virtual void draw() const=0; virtual void move(int a, int b)
virtual void move( int x, int y)=0; {
cout<<"move circle to new position";
}; }
private:
double radius;
};
Main()
{ shape s(2,3); [Error]
circle c(2,2);
}
u The abstract class fixes the interface but the
implementation via derived class is flexible.
Abstract Classes
u Sometimes a base class is just a conceptual entity
A category, or umbrella for related classes.
You won’t instantiate any objects of that type.
u Abstract classes are an organizational tool: useful in
organizing inheritance hierarchies
Vehicle
Provides an interface
(separates interface from implementation)
Pure Virtual Functions
class A {
public: u A is an Abstract Base
virtual void x() = 0; Class
virtual void y() = 0;
};
Declares pure virtual
class B : public A { functions (=0)
public:
virtual void x(){};
u Derived classes override
}; pure virtual methods
class C : public A { B overrides x(), C overrides
public: x() & y()
virtual void x(){“x function
called”;}; u Can’t instantiate class with
virtual void y(){“y function
called”;}; declared or inherited pure
}; virtual functions
int main () {
A and B are abstract, can
A * ap = new C; create a C object
ap->x ();
ap->y ();
u Can still have a pointer of
delete ap; an abstract class type
return 0; Useful for polymorphism
};
Where Polymorphism is
Helpful
u Arrays
u Passing parameters
u Returning values from a method
Abstract Classes
u Can still use abstract classes as a reference variable, for
the purposes of polymorphism.
u An abstract class has no use until it is extended!
u A class that is not abstract is called concrete.