Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now