True or False Question about Inheritance

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.

I don't know what 'override' means so I can't answer this question.

Can someone please explain this to me?
test it yourself.
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.
@ ForTheReallys,
Thank you! Your explanation was very clear.
No problem! Glad to help
Are you sure?

I think the answer is False.

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.
Last edited on
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.
Last edited on
Might be relevant for the OP:

http://stackoverflow.com/a/19737267

The answer should be true. In order for a function to be overriden and not just hidden, it must be declared virtual in the base class.
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

class A {
public:
    virtual ~A() {} // ignore

    void print() { std::cout << "A" << std::endl; }
    virtual void printv() { std::cout << "Av" << std::endl; }
};
class B : public A {
public:
    void print() { std::cout << "B" << std::endl; }
    void printv() { std::cout << "Bv" << std::endl; }
};

int main()
{
// 1. no polymorphism
    B b;
    b.print(); // uses B::print()
    b.A::print(); // uses A::print()

    b.printv(); // uses B::printv
    b.A::printv(); // uses A::printv
    
// 2. polymorphism
    std::cout << std::endl;
    A* a = new B();
    a->print(); // A::print because non-virtual
    a->A::print(); // A::print because of direct access
    
    a->printv(); // B::printv because virtual
    a->A::printv(); // A::printv because of direct access
    delete a;
}
Last edited on
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.

Any standards expert types about??

Andy
PS From http://www.stroustrup.com/glossary.html

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.)
Last edited on
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
Last edited on
closed account (z05DSL3A)
andywestken wrote:
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:
    virtual void vf(int);
    virtual void vf(char);
};

class Derived : public Base
{
public:
    void vf(int);  // overrides `Base::vf(int)` also hides `Base::vf(double)`
};
andywestken, can't you you just say the function is overloaded?
Last edited on
Not really : overloaded functions have different signatures to each other (and have to be in the same scope.)

Back to http://www.stroustrup.com/glossary.html

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."
But that's not completely correct, if you use polymorphism then a non-virtual function is not overridden >.<

But I don't want to cause a commotion here :)
I hope your teacher gives you a better question next time ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

class A {
public:
    virtual ~A() {} // ignore

    void print() { std::cout << "A" << std::endl; }
    virtual void printv() { std::cout << "Av" << std::endl; }
};
class B : public A {
public:
    void print() { std::cout << "B" << std::endl; }
    void printv() { std::cout << "Bv" << std::endl; }
};

int main()
{
    A* a = new B();
    a->print(); // A::print because non-virtual
    a->A::print(); // A::print because of direct access
    
    a->printv(); // B::printv because virtual (OVERRIDDEN!)
    a->A::printv(); // A::printv because of direct access
    delete a;
}
closed account (z05DSL3A)
newbiee999 wrote:
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."
Oh dear, what are they teaching these days.
Topic archived. No new replies allowed.