Hello,
I'm reading a chapter about virtual function. So what I know is that virtual function is equal to abstract functions.

It is enough to declare one function virtual to make the class into an abstract class. E.g.

class Base {
public:
Base();
virtual set();
}

Now I'm wondering about pure virtual function, and "normal" virtual function. I don't get what the book says about it. Seems to be that pure virtual function is equal to any virtual function you declare in the Base class. Or am I wrong?

Recommended Answers

All 3 Replies

When a class has one or more pure virtual methods, the class is called an abstract class, but you cannot just create an object from it, you first have to derive a class from your abstract class and then you've to override the pure virtual methods before you can use it :)

A "normal" virtual method has to do with early and late binding (runtime polymorphism)

>So what I know is that virtual function is equal to abstract functions.
No. An abstract function is a placeholder that has no definition and requires derived classes to implement it. A virtual function can have a definition, and in such a case will be silently inherited without any action on the derived class' part.

The equivalent of an abstract function is a pure virtual function:

virtual void foo() = 0;

>It is enough to declare one function virtual
>to make the class into an abstract class.
No, one can still instantiate a class if it has no pure virtual functions. Your statement would be more correct if you said it's enough to declare one function virtual to make the class polymorphic.

Congratulations, all statements in your post are wrong.
1. No such animals as abstract functions in C++. There are abstract classes... feel the difference.
2. It's enough to declare one member function pure virtual or don't define inherited pure virtual function to make (to keep) the class into an abstract class. E.g.

class Base 
{
public:
    Base();
    virtual ~Base() {} // add virual destructor!
    virtual void set() = 0;
};
class Bang(): public Base // yet another abstract class
{
public:
    void* get();
};

3. Pure virtual function has not a body, that's why this class called abstract: it's impossible to instantiate it, i.e create an object of abstract class. This class descendant(s) must define all pure virtual functions to be concrete class(es).
4. There are tons of links about virtual functions. Didn't you hear about "Google search"?! For example, start from
http://en.wikipedia.org/wiki/Virtual_function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.