You are on page 1of 16

Object Oriented

Programming (C++)

Virtual Function
Pure Virtual Function
Abstract Classes
Lecture 11: Virtual Function and Pure Virtual function

Virtual functions
 Purpose of polymorphism
 Can access different implementations of a
function with the same function name.
 Design and implement systems that are
more easily extensible.
 For example: overloaded functions,
operator overloading, etc.
2
江西财经大学信息管理学院
Lecture 11: Virtual Function and Pure Virtual function

Virtual functions (cont.)


 For a class, if we have:
class Shape{
public:
void Draw();
//…}*ShapePtr,ShapeObject;

We can have two polymorphisms:
 ShapePtr->Draw();
 Compiler implements dynamic binding
 Function determined during execution time

 ShapeObject.Draw();
 Compiler implements static binding
 Function determined during compile-time

3
江西财经大学信息管理学院
Lecture 11: Virtual Function and Pure Virtual function

Role of virtual functions


 Improve the reuse of the code.
 Provide the uniform interface of the
same actions for the sub-classes.
 Eliminate the time consuming and error
prone.

4
江西财经大学信息管理学院
Lecture 11: Virtual Function and Pure Virtual function

Important notes 1:
 It cannot be a static member function.
 Add a keyword virtual before the function
prototype, and cannot be used for the
definition outside the class.
 Can be inherited, all the functions with the
same name in the derived class belong to
virtual function.
 Called be the pointer or reference of the base
class, and decided by the pointer to the class.

5
江西财经大学信息管理学院
Lecture 11: Virtual Function and Pure Virtual function

Important notes 2:
 Virtual functions can not be defined as
friend functions .
 Virtual functions can not be overloaded,
they must be defined as the same
declarations as in the base class with or
without virtual.
 Constructors can not be virtual functions,
but destructors can.
6
江西财经大学信息管理学院
Lecture 11: Virtual Function and Pure Virtual function

Pure virtual functions and Abstract Class


 Abstract classes
 Sole purpose is to provide a base class for other

classes
 No objects of an abstract base class can be

instantiated
 Too generic to define real objects, i.e.

TwoDimensionalShape
 Can have pointers and references

 Concrete classes - classes that can instantiate

objects
 Provide specifics to make real objects , i.e.

Square, Circle
7
江西财经大学信息管理学院
Lecture 11: Virtual Function and Pure Virtual function

Pure virtual functions and


Abstract Class (cont.)
 Making abstract class
 Declare one or more virtual functions as “pure”

by initializing the function to zero

virtual type func_name(args)= 0;


 Pure virtual function

8
江西财经大学信息管理学院
Lecture 11: Virtual Function and Pure Virtual function

class Shape
{
public:
virtual void rotate(int) = 0; // pure virtual functions
virtual void draw() = 0; // pure virtual functions
virtual bool is_closed() = 0; // pure virtual functions
// ...
};

Shape s; // error: variable of abstract class Shape

9
江西财经大学信息管理学院
Lecture 11: Virtual Function and Pure Virtual function

Example 1:
#include <iostream.h>
class B0 {
public:
virtual void display( )=0;
};

class B1: public B0


{
public:
void display ( ) {cout<<"B1::display ( ) "<<endl;}

};

10
江西财经大学信息管理学院
class D1: public B0
{
public:
void display ( ) {cout<<"D1::display ( ) "<<endl;}

};

void main()
{ B0 *p;
B1 b1; Result:
D1 d1;
p=&b1; B1::display()
p->display(); D1::display()
p=&d1;
p->display();
}
Lecture 11: Virtual Function and Pure Virtual function

Example 2:
class Shape{
public:
virtual void printShapeName() const = 0;
virtual void print() const = 0;
};
class Point : public Shape {
public:
Point( int a= 0, int b= 0 );
void setPoint( int, int );
int getX() const { return x; }
int getY() const { return y; }
void printShapeName() const { cout << "Point: "; }
void print() const;
private:
int x, y;
};

12
江西财经大学信息管理学院
Point::Point( int a, int b ) { setPoint( a, b ); }
void Point::setPoint( int a, int b )
{ x=a;
y=b;
}
void Point::print() const
{ cout << '[' << x << ", " << y << ']'<<endl; }

class Circle : public Point {


public:
Circle( double r = 0.0, int x = 0, int y = 0 );
void setRadius( double );
double getRadius() const;
virtual double area() const;
void printShapeName() const { cout << "Circle: "; }
void print() const;
private:
double radius; // radius of Circle
};
Circle::Circle( double r, int a, int b ): Point( a, b )
{
setRadius( r );
}
void Circle::setRadius( double r ) { radius = r > 0 ? r : 0; }
double Circle::getRadius() const { return radius; }
double Circle::area() const
{ return 3.14159 * radius * radius; }
void Circle::print() const
{
Point::print();
cout << "Radius = " << radius<<endl;
}
void main()
{
Point point( 7, 11 );
Circle circle( 3.5, 22, 8 );
point.printShapeName(); // static binding
point.print(); // static binding
circle.printShapeName(); // static binding
circle.print(); // static binding
cout<<"Dynamic binding test"<<endl;
Shape *arrayOfShapes[2];
arrayOfShapes[0]=&point; //dynamic binding
arrayOfShapes[1]=&circle; //dynamic binding
arrayOfShapes[0]->printShapeName();
arrayOfShapes[0]->print();
arrayOfShapes[1]->printShapeName();
arrayOfShapes[1]->print();
}
Lecture 11: Virtual Function and Pure Virtual function

The end!

16
江西财经大学信息管理学院

You might also like