Abstract base class question C++

I I have an abstract base class in c++ as follows:
	class IMyBaseClass
	{
	public :
		virtual ~IMyBaseClass() {}
		virtual void AddSomeInformation( int param1, int param2) = 0;
		virtual void AddSomeMoreInformation( int param1) = 0;
	};

Open in new window


When I go to implement a class with this interface as follows:

       class MyBaseClass : public IMyBaseClass
	{
	public :
		virtual ~IMyBaseClass() {}
		void AddSomeInformation( int param1, int param2);
		void AddSomeMoreInformation( int param1);
	};

Open in new window


Do I have to include the word virtual before the function name in this class definition??
e.g.
virtual void AddSomeInformation( int param1, int param2);

Open in new window


instead of just:
void AddSomeInformation( int param1, int param2);

Open in new window


Can you explain your answer please?
Wanting2LearnManAsked:
Who is Participating?
 
AndyAinscowFreelance programmer / ConsultantCommented:
Add the word virtual - that instructs the compiler there is a base version of that function being overwritten.  Without it then the wrong function could be called at times depending on what object is being processed.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Do I have to include the word virtual before the function name in this class definition??

In ANSI Standard C++, no. As long as the base class members are virtual all other derived classes will also inherit their "virtualness".
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> In ANSI Standard C++, no.
Although doing so will not cause any harm... ie, it's optional.

>> Can you explain your answer please?

Cos, that's what the C++ standard says :)

"If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or
indirectly from Base, a member function vf with the same name and same parameter list as Base::vf is
declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides
Base::vf."
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
A couple of points.

evilrix knows the standards inside out - so the 'correct' answer is no, you don't require the keyword virtual.

HOWEVER - that requires the compiler to adhere to the standards.  You often see information in the help files such as microsoft specific - so that compiler is not adhering to the standards in all cases.  I'd still strongly recommend you always use the virtual keyword for the function in the derived classes when it is virtual in the base class.  That way it will be treated as a virtual function - no danger of some wierd and difficult to trace behaviour.



The other part of my comment:  I am pretty certain I saw a question here some time ago where the wrong version of a function was being called.  Maybe I remember incorrectly but I think it was that in one class it had been declared virtual in and in another the virtual keyword had been left out. Once the virtual keyword was added then the problem disappeared.
0
 
sarabandeCommented:
Andy, i never have seen a compiler which was wrong in that case. it would be a fatal error which could not remain undetected for long as i know many libraries and software packages which have the virtual only in the very top baseclass.

when the compiler calls a wrong function, the error should be that name or argument types don't match exactly.  i mean to remember such a case recently.

Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.