answersLogoWhite

0

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

User Avatar

Wiki User

16y ago

Still curious? Ask our experts.

Chat with our AI personalities

BlakeBlake
As your older brother, I've been where you are—maybe not exactly, but close enough.
Chat with Blake
RossRoss
Every question is just a happy little opportunity.
Chat with Ross
ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
More answers

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.

User Avatar

Wiki User

17y ago
User Avatar

Virtual functions are not used in C, but in C++.In C++, we use virtual functions to ensure the correct function is called at runtime. If a derived class overrides a function from its base class, the base class' function will still be called unless it is a virtual function. The usual goal is to call the derived class' function, so all functions that are designed to be overridden should be declared as virtual.
User Avatar

Wiki User

15y ago
User Avatar

I don't think that Virtual Functions could be implemented in C language. Virtual Functions work with Inheritance (OOPS concept) and it is a run-time Polymorphism. Moreover V-tables cannot be constructed.

User Avatar

Wiki User

17y ago
User Avatar

Virtual functions are only available in object-oriented languages, allowing a derived object to override its base class interfaces. C is not an object-oriented language and therefore provides no native support for virtual functions.

User Avatar

Wiki User

9y ago
User Avatar

Basic purpose of Virtual function is same function name in child classes. Function in child classes has their own purpose and functionality.

For example you have drawing classes. Classes has child parent relationship. Each class has Draw function.

Note: All this staff is C++, not C.

User Avatar

Wiki User

16y ago
User Avatar

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.

User Avatar

Wiki User

15y ago
User Avatar

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.

User Avatar

Wiki User

11y ago
User Avatar

Non-existent. C is not an object oriented language.

User Avatar

Wiki User

10y ago
User Avatar

Add your answer:

Earn +20 pts
Q: What is virtual function in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp