answersLogoWhite

0


Best Answer

Virtual methods are member methods of a base class that can be overridden by derived classes. Classes that can act as base classes should contain all the functionality that is common to all their derived classes. However, the derived classes may need to alter that functionality in some way by overriding the methods in the base class.

While this is fine when we actually hold pointers or references to a derived object, what happens when we hold pointers or references to the base class of a derived object? We cannot call the overridden methods explicitly unless we expose the runtime class of the derived object, which would incur an unacceptable processing overhead.

Virtual methods allow us to get around this problem in a more elegant fashion. By declaring a method as virtual, calling the method implicitly on a base class will automatically and explicitly call the most-derived implementation in the class hierarchy. If no override exists, the base class method is called explicitly.

While this mechanism incurs a memory penalty in creating the v-table (virtual table), the bulk of that cost is paid with the first virtual method declared. Subsequent methods add very little overhead, so it is not uncommon for programmers to declare all methods virtual. However, if there is at least one virtual method declared, the class destructor must also be declared virtual. Thereafter, you should only declare other methods to be virtual if there is an actual need to do so. After all, there's no point in consuming more memory than is actually required.

Sometimes it is not possible for a base class to provide the implementation for a virtual method, In this case the method should be declared pure-virtual. The same rules apply as for virtual functions, however you can no longer instantiate objects from the base class itself -- it becomes an abstract class -- so you must derive from it and you must provide an implementation for all the pure-virtual methods (otherwise it becomes abstract itself).

Abstract classes will generally have few, if any, member variables, and all member methods will generally be declared as pure-virtual. Abstract classes are primarily intended to provide a common interface to their derivatives. The base class can still provide default implementations for pure-virtual methods, however they must be explicitly called. In some cases, a derived class will simply augment the default implementation, rather than override it completely.

Be aware that base classes that are common to derived classes that can be used by multiple inheritance classes will introduce ambiguities. The multiple inheritance class will inherit two or more instances of the common base class, and therefore must be called explicitly from the multiple inheritance class. However, it is possible to remove the ambiguity altogether, by declaring the common base class to be virtual in its immediate derivatives, in which case all derivatives share the same common base class when multiply inherited. Ideally, the common base class should have very little in the way of implementation, and few, if any, member variables.

User Avatar

Wiki User

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

Wiki User

12y ago

To ensure a derived class method is called whenever a call is made implicitly on the base class method. Base classes have no knowledge of their derivatives (if they did, the base class would have to be modified every time a new derivative was created, which would be impossible to maintain), so base classes cannot call derived class overrides without making costly calls to obtain the runtime information of the derived class.

But derived classes already know their own base classes. If the base class has a virtual method, the derived class will also have a virtual table. The virtual table is essentially a list of function pointers arranged in pairs, one pointing at the base class virtual method and the other pointing at the derived class override. When you implicitly call the base class method, the virtual table diverts the call to the derived class override.

On those occasions where the base class method must be called, it can be called explicitly, which bypasses the virtual table.

In this way there is no need to know what class of object you're actually dealing with, so long as you know the base class. You simply call the virtual method and the actual object "does the right thing".

Pure-virtual methods work exactly the same except the base class does not require an implementation, but the derived class must provide an override. Derived classes do not need to override a normal virtual function; it is expected, but not demanded.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the primary value in using virtual functions within C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the merit and demerit of using friend function in c?

disadvantage of friend functions is that they require an extra lineof code when you want dynamic binding. To get the effect of a virtual friend,the friend function should call a hidden (usually protected:) virtual[20]member function.Read more: Demerits_of_friend_function


Why Member functions are not virtual by default?

It depends which language you are using. Java member functions are virtual by default but C++ member functions are not. Java takes the viewpoint that if any member function is declared virtual then all member functions should be declared virtual, so they may as well be virtual by default. However, C++ takes the view that a member function should only be declared virtual if there's a specific reason to declare it virtual. Not all functions are meant to be overridden. Indeed, not all classes are meant to act as base classes. So all member functions are non-virtual by default. Purists will argue that the C++ method is the correct method. After all, there's no point in having a virtual-table if it's never going to be used. Java places the onus on the programmer to eliminate an unused virtual-table, whereas C++ simply doesn't provide one unless you explicitly declare one. However, the real reason C++ uses non-virtual methods by default is because it has to maintain compatibility with a C struct. A C struct is not a class so it has no methods (and therefore no virtual methods). It is a "plain-old-data" or POD structure. In C++, however, a struct is a class. As such, by default, it has a compiler-generated default constructor, default copy and move constructors, default copy and move assignment operators and a default destructor. It also has public access by default. However, because the compiler-generated methods are all trivial member-wise implementations, a C++ struct is backwardly compatible with a POD. Thus C code can use a C++ struct just as if it were a C struct, because both use POD structures by default. If C++ used virtual member functions by default, a struct would not be a POD by default, it would be a base class by default.


How do you write a program in C using the fprint and fscan functions?

By using those two functions in your code.


What is virtual function in c plus plus?

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 project report on demographic structure of a neighborhood of 10 families using primary data?

A project report on demographic structure of a neighborhood of 10 families using primary data makes up census data.

Related questions

How does commodity options trading work?

Commodity options trading are virtual transactions of purchasing and sales using raw or primary commodities. Examples of Primary commodities are oil, gold. Examples of raw commodities are cocoa and fruit.


Which hierarchical design model layer controls the flow of network traffic using policies and delineates broadcast domains by performing routing functions between virtual LANs?

distribution


What are the merits and demerits of using function in c?

disadvantage of friend functions is that they require an extra lineof code when you want dynamic binding. To get the effect of a virtual friend,the friend function should call a hidden (usually protected:) virtual[20]member function.Read more: Demerits_of_friend_function


What are the merit and demerit of using friend function in c?

disadvantage of friend functions is that they require an extra lineof code when you want dynamic binding. To get the effect of a virtual friend,the friend function should call a hidden (usually protected:) virtual[20]member function.Read more: Demerits_of_friend_function


Why Member functions are not virtual by default?

It depends which language you are using. Java member functions are virtual by default but C++ member functions are not. Java takes the viewpoint that if any member function is declared virtual then all member functions should be declared virtual, so they may as well be virtual by default. However, C++ takes the view that a member function should only be declared virtual if there's a specific reason to declare it virtual. Not all functions are meant to be overridden. Indeed, not all classes are meant to act as base classes. So all member functions are non-virtual by default. Purists will argue that the C++ method is the correct method. After all, there's no point in having a virtual-table if it's never going to be used. Java places the onus on the programmer to eliminate an unused virtual-table, whereas C++ simply doesn't provide one unless you explicitly declare one. However, the real reason C++ uses non-virtual methods by default is because it has to maintain compatibility with a C struct. A C struct is not a class so it has no methods (and therefore no virtual methods). It is a "plain-old-data" or POD structure. In C++, however, a struct is a class. As such, by default, it has a compiler-generated default constructor, default copy and move constructors, default copy and move assignment operators and a default destructor. It also has public access by default. However, because the compiler-generated methods are all trivial member-wise implementations, a C++ struct is backwardly compatible with a POD. Thus C code can use a C++ struct just as if it were a C struct, because both use POD structures by default. If C++ used virtual member functions by default, a struct would not be a POD by default, it would be a base class by default.


What is the primary purpose for using dashes within a sentence?

To set off a diversionary thought without incorporating it into the grammatical structure of the main sentence.


How do you write a program in C using the fprint and fscan functions?

By using those two functions in your code.


How do you make a virtual world?

Using VastPark Creator.


What is NVT Network Virtual Terminal?

Communication with the TCP/IP device over the Ethernet network can be extended to more functions using NVT (Network Virtual Terminal) commands. It can be used according to the RFC2217 standard to change baudrate on remote Virtual Serial Port for example. The simple NVT control commands are included in the data stream with the character "FF" used as the command prefix. If the "FF" character occurs within the normal data stream, it is simply doubled.You can finddetailed NVT and a complete TELNET description in the last section of this article, we shall begin with a detailed manual of used commands and examples.


Is using a plane mirror to form virtual image one of its uses?

A plane mirror forms a virtual image. If it's reflected, then the light does not come from the image, and it is virtual.


Is a virtual private server functionally equivalent to a separate physical computer?

A virtual private server is the exact same functionally as a completely separate computer due to the simple fact that both have the same security features and have the exact same functions. One difference is that a virtual server is able to be detected which having a separate physical computer would not be able to be known or located if somebody hacked into the main computer in which a person is using.


What are the two primary functions of the cerebellum?

The cerebellum is involved in the coordination of voluntary motor movement, balance and equilibrium and muscle tone.