Object Oriented Programming (C++) : Virtual Function Pure Virtual Function Abstract Classes
Object Oriented Programming (C++) : Virtual Function Pure Virtual Function Abstract Classes
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
ShapeObject.Draw();
Compiler implements static binding
Function determined during compile-time
3
江西财经大学信息管理学院
Lecture 11: Virtual Function and Pure Virtual function
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
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
objects
Provide specifics to make real objects , i.e.
Square, Circle
7
江西财经大学信息管理学院
Lecture 11: Virtual Function and 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
// ...
};
9
江西财经大学信息管理学院
Lecture 11: Virtual Function and Pure Virtual function
Example 1:
#include <iostream.h>
class B0 {
public:
virtual void display( )=0;
};
};
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; }
The end!
16
江西财经大学信息管理学院