Solved

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

Posted on 2006-11-19
3
213 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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