Solved

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

Posted on 2006-11-19
3
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

733 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