True. Overriding member functions are typically used when using pointers and polymorphism. Overriding a function can be thought of as writing different versions of an inherited member function. If you are creating a direct object, the virtual keyword will not make a difference. However, when working with pointers to objects, the virtual keyword is necessary or else it would call the base class version of the function.
Overriding means defining a function in the derived class with same name and signature (i.e. set of parameters) as in the parent class (cf. overloading where the signatures differ.) This can be done for either virtual or non-virtual functions.
Of course, if you want to call the correct version of the function through a base class pointer or reference (i.e. you're using the classes polymorphically) then the function must be declared as virtual.
Andy
PS Reading ForTheReallys' post, the only thing I disagree with is the True. I see no requirement for the function to be virtual for overriding to be possible.
Sorry you're right. I said true in the beginning but I basically said no when I wrote about using objects rather than pointers to objects. Sorry about the confusion.
You can only hide the method of the parent class, there is no way to override it.
So the answer is false, you can NEVER override it.
(well, you can use protected or private inheritance to prevent the user from accessing the parents methods)
1. You can make it look like both virutal and non-virtual methods are overritten when not using polymorphism. (first part in my example)
2. You can make it look like only the virtual function is overwritten when using polymorphism. (second part in my example
In both cases you can still access the method of the parent class directly as shown below.
Well, after checking the C++ standard, I see that it only uses the term "overriding" in the context of virtual functions. So it may well that the use of this term implies the function is virtual. (So the answer would be True after all...)
At some point I've picked up the habit of using override when a derived class provides a replacement for a base class method (same name and signature), irrespective of whether it is virtual or not.
overriding - declaring a function in a derived class with the same name and a matching type as a virtual function in a base class. The argument types must match exactly. The return types must match exactly or be co-variant. The overriding function will be invoked when the virtual function is called. TC++PL 15.6.2, 6.2, D&E 3.5.2-3, 13.7.
So I got it wrong... :-(
The answer is True, by definition.
(Sorry to ForTheReallys for doubting him!)
Andy
PS But now I need a way to say that a derived class has replaced a non-virtual method (But perferably not the term 'hidden', as that could confuse things elsewhere.)
It seems like the term overriding only applies for virtual functions when using polymorphism but it's still possible to invoke the method of the parent class.
So that the standard team might have though "nobody would access a function like a->A::print() and so it looks like it's overwritten and therefor we say it's overridden"
It get's complicated if you explain all the details and in most of the times the function looks like it is overwritten so they say it's overwritten, which makes it easier for novices to get into the language.
So when using polymorphism then only virtual methods are overridden.
The sad thing is that when not using polymorphism the method of the base class also looks like it's overridden when it's not virtual :o
So the answer to the question is still undefined IMO :o
PS But now I need a way to say that a derived class has replaced a non-virtual method (But perferably not the term 'hidden', as that could confuse things elsewhere.)
It is still a form of name hiding.
1 2 3 4 5 6 7 8 9 10 11 12
class Base
{
private:
virtualvoid vf(int);
virtualvoid vf(char);
};
class Derived : public Base
{
public:
void vf(int); // overrides `Base::vf(int)` also hides `Base::vf(double)`
};
overloading - having more than one function with the same name in the same scope or having more than one operator with the same name in the same scope. It is not possible to overload across different scopes. See also: using-declaration. TC++PL 6.2, D&E 3.6, 11.2.
Thank you everybody for your time and effort!
I asked my professor and he said that the correct answer is "False".
He said, "It does not have to be virtual in order to override it, however the functionality will be different."
True or False:- For a C++ Derived Class to override an inherited member function, the base class is required to declare the function to be virtual.
and wrote:
I asked my professor and he said that the correct answer is "False".
He said, "It does not have to be virtual in order to override it, however the functionality will be different."