Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
c++ how to tell if the progra is ctl or mfc atl ect 6 84
Getting IP address 8 89
GUI: DIalog Stacking and Popping in MS C++ 4 82
Beginner to Unreal Engine 4 5 94
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

861 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