Virtual functions

what is a virtual funtion? when it is used!!

C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. The whole function body can be replaced with a new set of implementation in the derived class.Whenever a program has a C++ virtual function declared, a v-table is constructed for the class. when we wan to select anything based on context at run time then we go for virtual functions.

Example: if we have one GUI application where you scroll mouse over various elements like buttons,links,navigation bar.you want to display some properties when your mouse points to buttons and you want to display other properties when your mouse points to links. so here you want to display the things based on your mouse points to at run time. in these kind of applications we will use virtual functions. In this example we will make display function as virtual.we can see which behaviour is varying and lets make it virtual

what is call-mechanism in late binding?

The v-table consists of addresses to the virtual functions for classes and pointers to the functions from each of the objects of the derived class. Whenever there is a function call made to the c++ virtual function, the v-table is used to resolve to the function address. This is how the Dynamic binding happens during a virtual function call.

Can virtual functions have inline declarations ?

Generally, compilers can't inline a virtual function call if the it's resolved dynamically. Therefore, declaring a virtual member function inline might seem pointless. However, not every call of a virtual function is resolved dynamically; in some cases, the compiler can resolve the call statically, as if the function weren't virtual. In situations like these, the compiler can also inline the call.

For example:

class Base

{

public:

inline virtual int f()

{

return 0;

}

};

int main()

{

Base b;

b.f(); // resolved statically; call can be inlined

}

The invocation of f() is resolved statically because b is not a pointer or a reference. The compiler can also expand the call inline, thereby optimizing the code even further.

Can you Instantiate object for this class?

#include"iostream"

using namespace std;

class A

{

public:virtual ~A()=0

};

int main()

{

A *a=new A();

return 0;

}

NO. destructor is also a member function. here it is pure virtual so the VTABLE is in-complete. we cant create the objects for classes whose VTABLES are incomplete.

Which class member function f1() will be invoked. A or B?

#include"iostream"

using namespace std;

class A

{

public:

virtual void f1()

{

cout < < "this is A class f1()" ;

}

void f2()

{

f1();

}

};

class B:public A

{

public:void f1()

{

cout< < "this is B class f1()" ;

};

int main()

{

A *a=new B();

a->f2();

return 0;

}

Here using the base class ponter we are calling the f2() function which is not virtual. hence A::f2() will be called, but in that body we are calling f1 again which is virtual, so the derived class B::f1() will be invoked

No comments: