user297850
user297850

Reputation: 8015

Will the following program cause any problem during compiling and execution process?

Will the following program cause any problem during compiling and execution process?

class A{
  public: virtual void foo(){}
};

class B:public A{};

int main(){
    B b;
    b.foo();
}

Upvotes: 0

Views: 145

Answers (4)

Paul
Paul

Reputation: 349

There will be no problems compiling or running this program.

virtual functions can be overridden, but they don't have to be. If an object's class does not implement the virtual function, then the superclass will be checked for an implementation.

Upvotes: 1

kervin
kervin

Reputation: 11858

Maybe, I'm guessing, they were testing if you knew the difference between virtual and abstract?

Upvotes: 1

Alex Weinstein
Alex Weinstein

Reputation: 9891

Why would it have a problem? You're calling a virtual function that IS defined on the parent class. B inherits it.

Upvotes: 0

Jon-Eric
Jon-Eric

Reputation: 17275

Looks fine to me. What problem do you think it causes?

Upvotes: 0

Related Questions