A virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. It is one that is declared as virtual in the base class using the virtual keyword. The virtual nature is inherited in the subsequent derived classes and the virtual keyword need not be re-stated there. The whole function body can be replaced with a new set of implementation in the derived class
A virtual method is a method that you should override when inheriting that class It uses virtual as a keyword. Virtual functions allow polymorphism. If a function is not declared virtual it will be impossible to override it in a child class. Example:
class Foobar
{ public:
int i;
Foobar() {i = -1;}
virtual void FuncA() {i = 0;}
void FuncB() {i = 10;}
};
class Foo: public Foobar
{
public:
virtual void FuncA() {i = 1;}
void FuncB() {i = 11;}
};
class Bar: public Foobar
{
public:
virtual void FuncA() {i = 2;}
void FuncB() {i = 12;}
};
void FoobarFunc(Foobar* fb)
{
cout << fb->i << endl;
fb->FuncA();
cout << fb->i << endl;
fb->FuncB();
cout << fb->i << endl << endl;
}
void FooFunc(Foo* f)
{
cout << f->i << endl;
f->FuncA();
cout << f->i << endl;
f->FuncB();
cout << f->i << endl << endl;
}
When FoobarFunc is called with Foo, Bar, or Foobar, the line fb->FuncA() will call the the correct FuncA from each class according to polymorphism. However, in FoobarFunc, the line fb->FuncB() will ALWAYS call Foobar's FuncB no matter what type is actually passed in. To further illustrate this, in FooFunc, the line f->FuncB() will ALWAYS call Foo's FuncB.
A virtual function is a function defined in some class A that can be overridden by a class B that inherits from A. Example:
#include
// Base class.
class A {
public:
// Constructor.
A() {}
// Destructor. When a class includes virtual functions,
// always make the destructor virtual.
virtual ~A() {}
// A virtual function.
virtual void function() {
std::cout << "A::function()\n";
} // function()
}; // class A
// Derived class.
class B : public A {
public:
// Constructor.
B() {}
// Destructor. Virtual is unnecessary here,
// because it is implied if unspecified,
// but it is good practice to include it.
virtual ~B() {}
// Overridden function. Again, virtual is unnecessary,
// but good practice.
virtual void function() {
std::cout << "B::function()\n";
} // function()
}; // class B
int main(int, char**) {
// Instantiate new objects.
A* a = new A();
A* b = new B();
// Call functions.
a->function(); // Outputs "A::function()" because a is an A.
b->function(); // Outputs "B::function()" because b is a B.
// Dispose of objects.
delete b;
delete a;
// Return success status.
return 0;
} // main()
Note that, if class B does not provide an override for "function", then the statement
b->function();
produces the same output as
a->function();
which is to say that the default implementation provided by class A is used.
If "function" is declared in A as:
virtual void function() = 0;
then "function" is a pure virtual function, and class A becomes an abstract base class. Class A can now no longer be instantiated. Derived classes must provide an implementation of "function" in order to be instantiable. A function can be pure virtual but still provide an implementation:
virtual void A::function() = 0 {
std::cout << "A::function()\n";
} // function()
And, though class B must still define "function" because it is still pure virtual, it is allowed to explicitly call on the default implementation of "function" provided by A:
virtual void B::function() {
A::function();
} // function()
In this case, "B::function" outputs "A::function()", just as if "function" were declared plain virtual in class A and not overridden in class B.
When you declare a virtual function in a base class, you automatically generate a v-table, or virtual table. A virtual table is essentially an array of function pointers. The base class virtual table simply points to the base class methods, while the derived classes point to each of their equivalent overrides. Any methods not overridden by a derived class simply point back to the base class method. Thus when you invoke a base class method, the pointer returned by the v-table is the method that is actually invoked. In other words, it is not necessary to know the actual type of the derivative in order to invoke specific behaviour. The base class need only provide a generic interface with a generic implementation, while the derivative provides a more specific implementation. The virtual table makes it possible for your base class to act polymorphically, without the need for expensive runtime type information and dynamic down casts.
An abstract class is any class definition that contains at least one pure-virtual function. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };
All virtual functions (including pure-virtual functions) are represented in italics. All non-virtual functions are represented normally. There is no differentiation between pure and non-pure virtual functions, however some people append "=0" to distinguish the pure-virtual functions.
...a function call.
Every C plus plus program that is a main program must have the function 'main'.
There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.
An abstract class is any class definition that contains at least one pure-virtual function. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };
Dynamic binding is achieved via virtual functions and the virtual table that is associated with every class that declares or inherits a virtual function. The virtual table (or v-table) maps every virtual function (including pure-virtual functions) to a function pointer that points to the most-derived overload. This makes it possible to invoke specific behaviour even when the runtime type of the object is unknown to the caller.
There is no such term as "building function" in C++.
All virtual functions (including pure-virtual functions) are represented in italics. All non-virtual functions are represented normally. There is no differentiation between pure and non-pure virtual functions, however some people append "=0" to distinguish the pure-virtual functions.
extensible means to enhance the program with new capabilities. In c++, extensibility can be achieved by using virtual function. now first we discuss what is virtual function in c++. " 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." Through this u can be able to promote extensibility in your program by using Virtual function. Very simple concept.
...a function call.
yes,we can make function inline
Every C plus plus program that is a main program must have the function 'main'.
There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.
Control is returning to the caller of the function.
method
It is the first function that gets called when the program is executed.