Hi Everyone,
I wanted to know as to what is the exact difference between a virtual
function and a pure virtual function?
Thanks in advance!!!
If you declare a virtual function in a class say for example B, you
actually say: The function may be redefined in derived classes.
class B {
public:
virtual void f() { /* ... */ }
};
You define the f() for B and you usually redefine the function in
derived class:
class D : public B {
public:
void f() {/* another defintion */ }
};
virtual function is a primary tool for polymorphic bevaviour. If you
offer no defintion for D::f(), you will use the definition of B::f(),
because as an Inheritance principle, D inherited the public members of
B. Also you can declare f() in D as a virtual function, and the
redefinition process is continued in the classes that derived form D.
pure virtual function is a kind of virtual functions with a specific
syntax:
class B {
public:
virtual void f() =0; // =0 means pure virtual
};
if a class has at least one pure virtual function, it will be abstract
class, so instance creation is impossible. B::f() says you should
implement f() in derived classes:
class D : public B {
void f() { /* ... */ }
};
If you do not implement f() in D, the D is abstract class by default,
because it inherits all the pure virtual functions of class B.
for more detailed (and of course better) description, please see the
following references:
. C++ Standard Draft: Chapter 10
. The C++ Programming Language (3rd edition) by Bjarne Stroustrup:
Chapter 12.
. Design and Evolution of C++ by Bjarne Stroustrup: Chapter under
the title of "Class Concepts Refinements"
Regards,