answersLogoWhite

0


Best Answer

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

βˆ™ 15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 16y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago
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.
This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 17y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 8y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 11y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 9y ago

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

This answer is:
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
Related questions

Write abstract class in c plus plus?

An abstract class is any class definition that contains at least one pure-virtual function. class AbstractClass { public: virtual void DoSomething()=0; // Pure-virtual. };


How dynamic binding acheived in c plus plus?

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.


What are the building function in c plus plus?

There is no such term as "building function" in C++.


How do you represent a C plus plus non-pure virtual function in UML?

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.


What is meant by extension?

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 c plus plus statement that invokes a function is known as?

...a function call.


When will you make a function inline in c plus plus?

yes,we can make function inline


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


What is the difference between friend function and inheritance in c plus plus?

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.


In C plus plus when a function is executing what happens when the end of the function is reached?

Control is returning to the caller of the function.


What do you call an object function in c plus plus?

method


What is a main function in c plus plus?

It is the first function that gets called when the program is executed.