| | |
Q. virtual functions
![]() |
•
•
Join Date: Mar 2009
Posts: 37
Reputation:
Solved Threads: 0
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.
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?
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.
cpp Syntax (Toggle Plain Text)
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?
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)

A "normal" virtual method has to do with early and late binding (runtime polymorphism)
Last edited by tux4life; May 4th, 2009 at 2:07 pm.
"You can't build a reputation on what you are going to do."
>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:
>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.
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:
C++ Syntax (Toggle Plain Text)
virtual void foo() = 0;
>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.
In case you were wondering, yes, I do hate you.
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.
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
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.
C++ Syntax (Toggle Plain Text)
class Base { public: Base(); virtual ~Base() {} // add virual destructor! virtual void set() = 0; }; class Bang(): public Base // yet another abstract class { public: void* get(); };
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
![]() |
Similar Threads
- Why inheritance requires virtual functions? (C++)
- Static virtual functions - Reasons (C++)
- Virtual functions (C++)
- Virtual functions, function objects, or what? (C)
- C++ derived class virtual functions (C++)
- Dealing w/ virtual functions (C++)
Other Threads in the C++ Forum
- Previous Thread: how to make my own "setw" function ?
- Next Thread: Beginner Question
Views: 530 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment basic beginner binary browser c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count delete desktop dll dynamic encryption error file files form fstream function functions game givemetehcodez graph gui homework i/o iamthwee input int lazy library linker list loop loops math matrix member memory network newbie number numbers object objects opengl operator output parameter pointer pointers problem program programming project random read recursion recursive reference sort sorting spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual visualstudio void win32 window windows winsock






