user707549
user707549

Reputation:

questions about virtual function in c++

I am a beginner of C++, I am studying virtual functions these days. There are some questions confuse me a lot.

for example:

class A {
  public:
  virtual void f() {
      //do something; 
  }
}

class B: public A {
   public:
   virtual void f() {
//do something;
}
}
  1. class A contains a virtual function f(), and class B inherits it. Inside class B, the function f() is also declared as virtual, so does this mean f() in class B overloads f() in class A? Does it allow classes which inherit B to overload f()? Or does B define a new virtual function which is different from f() in class A?

  2. Virtual functions provide a way of overloading methods. If B inherits A and does not declare the f() as virtual, then can a class C which inherits B overload the f() and achieve polymorphism?

Upvotes: 6

Views: 356

Answers (4)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361482

inside class B, the function f() also be declared as virtual, so does this mean f() in class B overload f() in class A

No, it doesn't overload. It overrides. Also the keyword virtual is optional in class B. B::f() will always be a virtual function, whether you write virtual or not.

The term overload is used when you define a function with same name but different parameter(s). In your case, the signature of the function f is exactly same in both classes, that means it isn't overloading; the derived class basically overrides the base class definition of f().

Upvotes: 4

Etienne de Martel
Etienne de Martel

Reputation: 36851

Since A::f is virtual, and B::f has the same signature, it is said that B::f overrides A::f.

Which means:

A * p = new B;
p->f(); // invokes B::f

EDIT: The following is just plain wrong (see the comments):

Since B::f is also virtual, then it would be possible for a child class of B to override it again. If B:f wasn't virtual, than any method with the same signature in a child class would simply shadow it (that is, it would be a different method).

So, the behaviors depends on the parent.

Upvotes: 0

Alok Save
Alok Save

Reputation: 206546

Virtual keyword allows you to override functions not overload them.

Also, the virtual attribute is inherited so virtual keyword is optional for f() in class B.

Upvotes: 2

wheaties
wheaties

Reputation: 35980

When you declare a function virtual what you are really saying to the compiler is that you want this function to behave in a polymorphic manner. That is, from your example if we have the following:

A* foo = new B();
foo->f();

it will call B's "f" function and not A's "f" function. To take it further, if we have a C which inherits from B like you've said:

class C : public B{}

B* foo = new C();
foo->f():

this calls B's "f". If you had defined it within C, it would have called C's method.

To explain the different behavior between virtual and non-virtual let's take this example:

struct Foo{
    virtual void f();
    void g();
};

struct Bar{
    virtual void f();
    void g();
};

Foo* var = new Bar();
var->f(); //calls Bar's f
var->g(); //calls Foo's g, it's not virtual

make sense?

Upvotes: 1

Related Questions