?
Solved

Can virtual function be defined and still requests derived classed to implement it

Posted on 2006-11-19
3
Medium Priority
?
216 Views
Last Modified: 2010-04-01
Hi Experts,

In the following example, I want functionOne( ) to be implemented by all of the derived classes of A including 'grand child' C.  So, If I leave the keyword 'virtual' in B's functionOne( ) then that means C does not have to implement it.  So, I want to keep the virtual keyword with B's functionOne( ).  Is it okay?  If we don't have class C, then we have A's functionOne( ), B's functionOne( ) both with the virtual keyword.  Will it cause any problem?

So, when I want derived classes to implement a function, no matter where they are in the class hierarchy then should I always keep the virtual keyword with the function.  

Thank you.

class A
{
   virtual void functionOne();
};

class B: public A
{

    virtual void functionOne() { cout << "B's functionOne" << endl;};
};

class C: public B
{
    void functionOne( );
};

void main()
{
    B bb;
   bb.functionOne( );
};
0
Comment
Question by:ambuli
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 17975791
Hi ambuli,
>     virtual void functionOne() { cout << "B's functionOne" << endl;};
The virtual keyword does not do anything here, and it's optional.  Many programmers add the keyword to the derive function, just to add extra clarification.
But it serves no function as far as the compiler is concern.  The code would be compile the same with it or without it.

David Maisonave (Axter)
Cheers!
0
 

Author Comment

by:ambuli
ID: 17975942
Thanks Axter, Yes that is the results I got after doing some experiment.  By the way, the virtual seems to play some role in the constructor below.  If I don't have the virtual keyword in the destructor for AA, then when deleting b, only BB's destructor is called.  But, having the virtual keyword calls both destructor.

#include <iostream>
#include <string>

using namespace std;



class AA{
public:
      ~AA( ){ cout << "A's destructor" << endl;};
};
class BB: public AA
{
public:
      ~BB( ){ cout << "B's destructor" << endl;};
};


void main()

{

      AA *b = new BB();
      delete b;


}


0
 
LVL 30

Expert Comment

by:Axter
ID: 17975999
virtual plays a role in the base class, and you should have at least one virtual method in the base class if you're going to delete a derived object using the base class pointer.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question