answersLogoWhite

0


Best Answer

Virtual functions (or virtual methods as they are also known), are methods declared in a class that are expected to be overridden by a derived class. That is, the method is a generic method that applies to the class in which it is declared (the base class), but that may require modification by a more-specialised class (the derived class). When you implicitly call the base class method, the overridden method is called instead, thus ensure the correct behaviour. The overridden can still call the base class method explicitly, as can any other code that has access to the base class method.

If you do not declare generic methods to be virtual, overriding those methods in the derived class may have unexpected side-effects, particularly if the method is overloaded in the base class. For instance, a derived class' override will effectively hide all the overloads of the base class.

Note that if you declare any virtual function, then you are essentially notifying the end-user that the class is intended to act as a generic class and may be derived from, if required. However, if the function is declared pure-virtual, you are notifying the end-user that the class is abstract and mustbe derived from (it is not optional).

Also, if there are any virtual methods then the destructor must be virtual as well, to ensure the derived object is destroyed before the base class is destroyed.

It is often good practice to declare all methods as virtual if there's at least one virtual method, however this only applies to methods that would require alternative handling by more-specialised classes. If the base class can provide a complete implementation that requires no further specialisation, it needn't be declared virtual.

Although there is a memory cost involved in declaring virtual methods, the bulk of that cost is paid with the first virtual function which establishes the virtual table (v-table). Thereafter, the cost is minimal. The v-table ensures the most-derived method is called, thus ensuring correct behaviour of derived classes, even when working with the generic base class.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Virtual keyword in C++ is used to make a method virtual.In C++ Virtual method is is used to accomplish run time polymorphism. Virtual methods can be overridden in its derived classes.

For example:

class A

{

private:

int x;

public:

virtual void abc()

{ }

};

Class B : public A

{

private:

int y;

public:

void abc() //abc is virtual here by default and can be overridden

{ }

};

Regards,

Rajneesh

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Virtual Keyword in C++

The virtual keyword has two purposes in C++. The first is when declaring generic methods in a base class (virtual and pure-virtual functions). The second relates to multiple inheritance (virtual base classes).

Virtual and Pure-virtual Functions

Virtual and pure-virtual functions are generic class member methods that are prefixed with the virtual keyword. A virtual method must have a definition (a generic implementation) but a pure-virtual method may or may not be defined (the implementation is optional). Typically, if you declare any method virtual then all methods should be declared virtual unless there is a specific reason not to. This includes pure-virtual methods since base classes may have a combination of virtual and pure-virtual methods.

Virtual methods are expected to be overridden by derived classes. That is, the derivative may or may not provide a more specialised implementation of the generic method it inherits from the base class. When you call the base class method implicitly, the override is called instead. This is achieved through the derived class' virtual table, which maps the base class virtual functions to the derived class' overrides.

By contrast, pure-virtual methods must be overridden. That is, even if the base class provides a generic implementation, that implementation cannot be inherited by the derived class. Thus the derived class must provide a specific implementation. Once implemented, that method reverts to being a virtual function which can then be inherited by derivatives of the derivative. If the base class provides a generic implementation, that implementation cannot be inherited and can only be called explicitly via a derived class override.

Virtual and pure-virtual methods are used to declare a generic interface to more specialised versions of the base class. If a base class has one or more virtual methods (including pure-virtual methods), it must also declare a virtual (non-pure) destructor. This ensures that all objects in the hierarchy are destroyed in the correct sequence. That is, if you delete a pointer to the base class, the most-derived destructor is called first, before working up the hierarchy chain to the least-derived destructor(s).

Note that any class that declares one or more pure-virtual methods is an abstract data type (ADT), also known as an abstract base class (ABC). ADTs cannot be instantiated directly, they must be derived from. Any class that inherits a pure-virtual method only inherits the interface, not the implemation (if one is provided). The derived class must provide its own implementation otherwise it too becomes an ADT, but any implementations that it does provide can then be inherited by subsequent derivatives. Only classes that provide and/or inherit a complete implementation can be instantiated directly.

Classes that implement virtual or pure-virtual methods need not include the virtual keyword since it forms no part of the function signature. However, it is good practice to include it anyway, if only as a reminder that the function can be overridden by subsequent derivatives.

Virtual Base Classes

Virtual base classes are used in multi-inheritance hierarchies where two or more bases classes share a common base class of their own. For instance, if classes A and B both inherit from class C while class D inherits from both A and B, then class D contains two instances of class C (A::C and B::C). This introduces ambiguity whenever you implicitly refer to members of D::C because the compiler doesn't know which instance of C you are referring to. But if A and B both inherit C virtually, then the most-derived class, D, will inherit directly from C, while A and B will both share this one virtual instance. This effectively eleminates the ambiguity allowing you to implicitly refer to D::C directly.

Not all base classes are ideal candidates for virtual inheritance. Ideally, a virtual base class should have no member data whatsoever. This is primarily because mutating any data in A::C would mutate B as well. If that would be undesirable, virtual inheritance should be avoided.

Virtual base classes are often referred to as the "dreaded diamond". However, the term is meaningless since it is not the diamond that is dreaded, but rather the lack of a diamond. The so-called diamond is formed by the "family-tree" of four classes, A, B, C, D (as per the example above) with C at the root, A and B as children of C, and D as a child of both A and B. Without virtual inheritance there would be two C's at the root, A as a child of one and B as a child of the other, with D as a child of both. This creates a V shape which is often what you do not want or expect. If anything, this should really be called the "dreaded V".

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the virtual keyword used for in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is it possible to declare a keyword as an identifier in c plus plus?

No. Keywords are reserved and cannot be used as identifiers. However, C/C++ is case-sensitive. So although register is a reserved keyword, Register is not.


How java use extern key word which is used in C plus plus?

No extern keyword in Java.


What is meant by println in c plus plus?

println is not a C++ keyword.


What keyword is used to find derivative in C plus plus?

The lazy way is to use a dynamic cast. The correct way is to use virtual functions in the base class. That way you don't ever need to know the derived type -- it simply does what is expected of it.


What is the Difference between override and new keyword in C?

An override is the specialisation of a virtual function. The new keyword instantiates an instance of an object in dynamic memory and returns a reference to that object (or null if the object could be instantiated). Both are used in C++, but not C.


Is in is used as a keyword in C language?

Neither "in" nor "is" is a keyword in C.


What is use of keyword extra in c plus plus programming?

Nothing.


How does the use of the 'void' keyword differ in C plus plus vs. C?

It doesn't. Void has the same meaning in both.


Is there any keyword in c or c plus plus like the function inkey in qbasic?

Using TurboC? kbhit and getch are your friends


Instantiation of objects in c plus plus?

Objects are instantiated when statically declared or dynamically created with the new keyword.


Why you use zero instead of NULL in c plus plus?

In C++ NULL is defined as 0. It's a design failure, will be fixed with a new 'nullptr' keyword.


What is friendly in c plus plus?

The keyword "friend" allows a function or variable to have access to a protected member inside a class.