Q. virtual functions

Reply

Join Date: Mar 2009
Posts: 37
Reputation: ganmo is on a distinguished road 
Solved Threads: 0
ganmo ganmo is offline Offline
Light Poster

Q. virtual functions

 
0
  #1
May 4th, 2009
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.

  1. class Base {
  2. public:
  3. Base();
  4. virtual set();
  5. }

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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2,034
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 225
tux4life's Avatar
tux4life tux4life is offline Offline
Postaholic

Re: Q. virtual functions

 
0
  #2
May 4th, 2009
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)
Last edited by tux4life; May 4th, 2009 at 2:07 pm.
"You can't build a reputation on what you are going to do."
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 825
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Q. virtual functions

 
2
  #3
May 4th, 2009
>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:
  1. 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.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 344
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Q. virtual functions

 
0
  #4
May 4th, 2009
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.
  1. class Base
  2. {
  3. public:
  4. Base();
  5. virtual ~Base() {} // add virual destructor!
  6. virtual void set() = 0;
  7. };
  8. class Bang(): public Base // yet another abstract class
  9. {
  10. public:
  11. void* get();
  12. };
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 530 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC