Re: Pure Virtual Function declaration
On Tuesday, September 11, 2012 7:54:47 AM UTC+4:30, (unknown) wrote:
What is the difference between two forms of following virtual functions :-
virtual void foo const =0;
virtual void foo {}
Hi
At first I assume you meant:
virtual void foo1() =0;
virtual void foo2();
for more clarity, I use foo1 and foo2.
Also, please note at the moment the const
qualifier doesn't affect on our discussion
Both are virtual functions, and
virtual means: may be redefined later in a class derived
from this one.
More precisely the first one is "pure" virtual function.
Using pure virtual function, we can make an abstract
class like this:
class C1 {
public:
virtual void foo1() =0;
};
Please note you "can" define foo1 but
you can't create an object of C1, because C1
has one pure virtual function. You can derive
a class like D from C1 and -re-define foo1:
class D : public C1 {
public:
void foo1() {/* ... */ }
};
foo2 isn't pure virtual. It's just virtual:
class C2 {
virtual void foo2() { /* ... */ }
};
You "have to" define foo2 at class C1 because it's a C++ rule:
A virtual function must be defined in the class in which
they are first declared.
Also a derived class like D can -re-define foo2.
It's obvious C2 isn't abstract and you can make object of
that.
HTH,
-- Saeed Amrollahi Boyouki