answersLogoWhite

0


Best Answer

Virtual functions are advantageous in that they allow us to write code in a more generic fashion more easily than would otherwise be possible. More importantly, without virtual functions, polymorphic behaviour would be impossible to achieve.

Consider the following example:

#include<iostream>

struct instrument

{

virtual ~instrument() {}

virtual void play () =0;

};

void foo (instrument& a, instrument& b)

{

a.play();

b.play();

}

struct drum : instrument

{

void play () override { std::cout << "Playing a drum\n"; }

};

struct guitar : instrument

{

void play () override { std::cout << "Playing a guitar\n"; }

};

int main()

{

drum d;

guitar g;

foo(d, g);

}

Output:

Playing a drum

Playing a guitar

There are several things to note about this program. As you know, in C++, all types must be declared before they can be used. However, note that the foo function was defined before the drum and guitar classes were declared and yet it was perfectly able to invoke their specific methods even without a forward declaration. This is made possible because the function knows about the instrument class and that's all it really needs to know about; the instrument class exposes the precise interface required by the foo function.

Note also that the instrument class has a virtual destructor. Unless a class is marked final, a virtual destructor tells us that we may derive a new class from this class. In other words, it tells us that this class can be used as a base class. It is important to note that every class that declares one or more virtual methods MUST also have a virtual destructor. I shall explain why shortly.

The instrument::play() method is a virtual method. The '=0' suffix denotes that the function is pure-virtual. Not all base classes require pure-virtual methods, but in this case we do because the instrument class is a conceptual class rather than a concrete class. That is, it is an abstract object; one we can imagine exists but cannot physically exist in reality. By declaring at least one pure-virtual method, we prevent end-users from constructing objects from the class.

A pure-virtual method may or may not be implemented by its base class, but it must be implemented by its derivatives otherwise they become abstract themselves. By contrast, virtual methods must be implemented in the base class but need not be implemented by the derived classes.

Whenever we declare a method virtual, we guarantee that the most-derived override for that method will always be invoked. Thus when we construct a drum object, its play() method becomes the most-derived override. This is how the foo function was able to invoke drum::play() despite the fact it only knew of the existence of the instrument::play() method. Indeed, the foo function doesn't even know that a refers to a drum any more than b refers to a guitar. All it needs to know is that they are both types of instrument and that is guaranteed by the compiler because both drum and guitar are derived from instrument.

While it is true that every drum is an instrument, it is not true that every instrument is a drum (it could be a guitar). How does the foo function know which of the two overrides to invoke? That's precisely the type of problem virtual functions help to resolve. Every class in a hierarchy has a virtual function table (or v-table) which maps the virtual functions of the base class to the most-derived overrides of those functions. Thus when play() is invoked against an object, the class v-table determines the correct function address according to the actual runtime type of the object.

While the v-table does consume additional memory over and above the size of the class members, the cost is minimal (approximately two words per virtual method). However, sometimes it becomes necessary to "switch on" the runtime type of an object in order to evoke specific behaviour. For instance, suppose our drum object has the following interface:

struct drum : instrument

{

void play () override { std::cout << "Playing a drum\n"; }

void play_rimshot () { std::cout << "Playing a rimshot\n"; }

void play_side_stick () { std::cout << "Playing a sidestick\n"; }

};

The play_rimshot and play_sidestick methods are too specialised to be placed in the instrument class. The instrument class exists to provide a common interface to all instruments but you cannot play a rimshot or sidestick upon a guitar. If these methods were part of the instrument interface then they'd also be part of the guitar interface.

One way to invoke these specific behaviours is to dynamically cast the instrument to a drum. If the cast succeeds, you can definitely invoke these methods. If not, then you cannot. But runtime type information is expensive and is often the result of poor design. It is not that there's anything wrong with having such specialised methods, it is only that accessing them from a generic interface is generally the wrong way to go about invoking them. In this particular case all we really need to do is derive a new class from the drum class to cater for these more specific methods:

struct rimshot : drum

{

void play() override { "Playing a rimshot\n"; }

};

struct sidestick : drum

{

void play() override { "Playing a sidestick\n"; }

};

Even though these classes did not exist when we first wrote our foo function, the function can still cater for them:

rimshot r;

sidestick s;

foo (r, s);

In other words, virtual functions have not only enabled runtime polymorphism without the need for runtime type information, they have also provided some measure of future-proofing our code; we do not need to continually maintain the foo function in order to cater for new behaviours.

User Avatar

Wiki User

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

Wiki User

12y ago

A pure vitual function is a function declared in base class that has no definition relative to base class .In that cases compiler requires each derived class either to define the function or to redeclare it as a pure virtual function.The class with pure virtual functions cannot be used to declare any objects any objects of its own.

A pure virtual function is a "do-nothing function"

declaration :

virtual void funtionname()=0;

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

To ensure the base class cannot be instantiated. Any class with a pure-virtual function becomes abstract; you are expectedto derive new classes from an abstract class and you mustprovide an implementation for the pure-virtual method, either in the derived class itself or in one of its derivatives. A derived class that does not implement a pure-virtual function inherited from its base class becomes abstract itself. Derived classes may also declare their own pure-virtual methods, which also renders them abstract.

Abstract classes merely provide the basis upon which to create fully-implemented classes. The least-derived class is the least-specialised. As classes becomes more derived, they become more specialised. When a specialised class fully implements all the pure-virtual functions below it (either through inheritance or by providing the implementation itself), that class can finally be instantiated as an object.

Note that although the class that declares a pure-virtual function can also provide an implementation for it, the derived class (or one of its derivatives) must override that implementation. Default pure-virtual implementations cannot be inherited by derived classes, but the overrides can call them, just as they can with non-pure virtual functions.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A virtual base class is a class, much like any other, but one that can be shared amongst two or more derivatives.

Consider the following:

class A{};

class B: public A{};

class C: public A{};

class D: public B, public C{};

In the above hierarchy, class D indirectly inherits two instances of A, one form B and the other from C. Not only does this introduce an ambiguity within D (you must explicitly state which instance of A you are referring to), it increases the memory footprint of D because all members of A are effectively duplicated, but will be out of sync with each other. Sometimes this may actually be desirable, but more often than not you will want both instances to be in sync. However, since B and C will typically have no knowledge of each other's existence, any changes made to A through either B or C will not be propagated to the other instance of A. This situation can be resolved by inheriting A virtually, rather than directly, in both B and C. Thus:

class A{};

class B: public virtual A{};

class C: virtual public A{};

class D: public B, public C{};

Note that the virtual keyword can be placed before or after the inheritance access specifier, it makes no difference.

Now D will directly inherit just one instance of A, which is then shared by both B and C. Not only does this reduce the memory footprint accordingly, it eliminates all ambiguity.

Moreover, any class that subsequently inherits from D will itself inherit A directly, and share that one instance with D, B and C.

You will often hear talk of the "dreaded diamond" in relation to virtual base classes. The term comes from the fact that the hierarchy of classes A, B, C and D form the four corners of a kite (a diamond shape) with A at the top corner, and D at the bottom, with B and C on either side. While the diamond formation is fairly accurate in terms of representing the class hierarchy with virtual inheritance enabled, it cannot be described as "dreaded" in any way shape or form. If anything, it is desirable.

The original configuration forms a three-tiered 'V' shape with two A's at the top, followed by B and C in the middle tier and D at the bottom. But even this configuration cannot be described as dreaded. For instance, consider the following complex structure that employs both virtual and non-virtual base classes A:

class A{};

class B1: public virtual A{}

class B2: public virtual A{}

class B3: private A{}

class C: public B1, public B2, public B3{}

Here we have class C which forms both a diamond (with B1, B2 and virtual A) which itself forms one side of a V formation (with B3 and non-virtual A on the other side). While an unusual structure, it is by no means an uncommon structure. B1 and B2 share a common instance of A, while B3 has a completely separate instance of A. Note that B3 also inherits A privately, thus eliminating any ambiguity with respect to C. But ambiguity is not to be dreaded either -- it's simply a matter of being explicit about which instance of A you refer to with respect to C.

Ultimately, virtual base classes simply permit the most-derived class to inherit directly from a lesser-derived class that it would otherwise inherit indirectly. Although they are mainly used in multiple inheritance "diamond" formations to eliminate ambiguous multiple instances, they can also be used in single inheritance hierarchies as well:

class A{};

class B: virtual public A{};

class C: public B{};

Here, C inherits A directly, which is then shared with B. Although this type of inheritance might seem unusual, the implication is that B can also be used in multiple inheritance hierarchies with other classes that also inherit A virtually. Indeed, the only way to eliminate the virtual base class in a single-inheritance hierarchy is to completely duplicate class B without the virtual keyword, which only serves to increase maintenance whenever either version of B is altered. If anything, that would be far more "dreaded" than any misplaced notion of a "dreaded diamond".

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Virtual functions are base class functions that are expected to be overridden by derived classes. The base class essentially provides a generic interface to all its derivatives, while the derived classes themselves provide the more specific implementations of those methods. Calling the base class method therefore invokes the derived class method, automatically, without any need to know the specifics of that derivative. This is how polymorphism works, and is one of the fundamental principals of object-oriented programming.

For instance, a base class named shape could have a virtual draw method which can be overridden by derived classes such as circle and square. Thus if you have a collection of derived shapes, it is not necessary to know the exact type of the shapes in the collection in order to invoke their specific draw methods, you simply call the base class method, the only class and method your function needs to know about. Runtime polymorphism takes care of the specifics because virtual calls are routed to the appropriate instance method via the v-table, or virtual table.

To appreciate the benefit, imagine what you would do if virtual functions were not permitted. The only way for the base class to invoke derived class behaviour is if the base class knows all about its derivatives. While that is certainly possible, it also means that every time you develop a new derivative you must update the base class in order to handle it. Aside from the increased maintenance cost this incurs, this would make it impossible for 3rd party developers to derive new classes from your base classes because the base class won't know anything about the specifics of those derivatives.

With virtual functions you don't need to predict what derivatives might exist in the future. The base class only needs to know that a generic interface exists while the derivatives themselves handle the more specific implementation of that interface.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the need of virtual base classes in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is a virtual base class in C plus plus when the different methods in base and derived classes have the same name true or false?

False. A virtual base class is one that is common to two or more derived classes that are themselves base classes, and that may be combined through multiple inheritance. By declaring the common base class to be virtual in its direct derivatives, only one instance of the common base class exists in the multiple-inheritance classes.


How do you implement inheritance in c plus plus?

You implement inheritance by deriving a new class of object from an existing class of object. The existing class is known as the base class of the derived class.Classes declared final cannot be used as bases classes and classes without a virtual destructor (or a virtual destructor override) cannot be used as polymorphic base classes.


What is inheritance in visual c plus plus?

When you derive one class from another, the derived class inherits the sum of all the public and protected members exposed by the class it derives from. The underlying class is known as a base class and in the class hierarchy is an ancestor of the derived class. It is not necessary for the base class to know any of the details regarding its derivatives, as prudent use of virtual methods ensures the derived class acts correctly even when calling methods in the base class.


What is the difference between base class and derived class in c plus plus?

There is no difference other than that a derived class inherits from a base class. That is, the derived class inherits all the public and protected members of its base class, and is a more specialised form of its base class. Thus both can be treated as if they really were base classes. The derived class can also override the virtual methods of the base class, thus allowing polymorphic behaviour without the need to know the exact type of the derived class.


What is the primary value in using virtual functions within C plus plus?

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.

Related questions

What is a virtual base class in C plus plus when the different methods in base and derived classes have the same name true or false?

False. A virtual base class is one that is common to two or more derived classes that are themselves base classes, and that may be combined through multiple inheritance. By declaring the common base class to be virtual in its direct derivatives, only one instance of the common base class exists in the multiple-inheritance classes.


How do you implement inheritance in c plus plus?

You implement inheritance by deriving a new class of object from an existing class of object. The existing class is known as the base class of the derived class.Classes declared final cannot be used as bases classes and classes without a virtual destructor (or a virtual destructor override) cannot be used as polymorphic base classes.


What is inheritance in visual c plus plus?

When you derive one class from another, the derived class inherits the sum of all the public and protected members exposed by the class it derives from. The underlying class is known as a base class and in the class hierarchy is an ancestor of the derived class. It is not necessary for the base class to know any of the details regarding its derivatives, as prudent use of virtual methods ensures the derived class acts correctly even when calling methods in the base class.


What is meant by function overriding in c plus plus?

Function overriding applies to class member functions (methods), where a derived class provides a more specialised implementation of its generic base class method. An override can still call the base class method and augment it with other instructions (before or after the call to the base class method), or it can provide a complete implementation of its own without calling the base class method. Classes that are intended to act as generic base classes will generally declare their methods to be virtual, meaning they are intended to be overridden, if required. Abstract base classes will contain one or more pure-virtual methods (which may or may not provide a generic implementation), meaning classes must be derived from the abstract base class and all the pure-virtual methods must be overridden in the derived class (otherwise they, too, become abstract). Only classes that fully implement all the pure-virtual methods they inherit from their base classes can actually be instantiated. That is, you cannot instantiate an instance of an abstract base class, even if it provides generic implementations for all its pure-virtual methods. They can only be instantiated by deriving classes from them.


What is the difference between base class and derived class in c plus plus?

There is no difference other than that a derived class inherits from a base class. That is, the derived class inherits all the public and protected members of its base class, and is a more specialised form of its base class. Thus both can be treated as if they really were base classes. The derived class can also override the virtual methods of the base class, thus allowing polymorphic behaviour without the need to know the exact type of the derived class.


What is the primary value in using virtual functions within C plus plus?

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.


Conventional base class in c plus plus?

A conventional base class is a class that has a virtual destructor. Virtual destructors are important in most class hierarchies because while we may not always know the exact type of the most-derived class in a hierarchy, we will always know at least one of its base classes. If we destroy that base, it is reasonable to expect the entire object to be destroyed, not just the base, and this can only be achieved with a virtual destructor.Note that a virtual destructor must be declared in the least-derived class in the hierarchy to ensure correct polymorphic behaviour.Classes not intended to be used as base classes do not have virtual destructors. This does not mean they cannot be used as base classes, but if you choose to use them as base classes you must be aware of the "unconventional" problems that can arise, particularly when destroying objects through pointers or references to their base classes as will lead to resource leaks.If you do not wish a class to be used as a base class, then the class should be declared final (since C++11). All other classes can still be used as base classes, however only classes with virtual destructors behave polymorphically.Typically, if we define at least one virtual function in a class, we should also define a virtual destructor for that class. However, derived classes implicitly inherit virtual destructors from their bases thus the absence of a virtual destructor in a derived class should not be taken to mean the derived class is not intended to be used as a base class; always look at the least-derived class definition to determine if a virtual destructor exists or not.Along with the final specifier, the overridespecifier was also introduced in C++11. This makes it much easier to document virtual functions and their overrides. That is, we can use the virtual specifier solely to introduce a new virtual function into the class hierarchy, and use the overridespecifier to show that we are explicitly overriding a virtual function rather than simply introducing a new virtual function. We can also use the final specifier to indicate that an individual virtual function cannot be overridden any further:struct X {virtual void f ();virtual ~X ();};struct Y : X {void f () override; // implies X::f() is either virtual or is itself an override~Y () override; // implies ~X() is virtual or is itself an override};struct Z : Y {void f () override; // implies Y::f() is either virtual or is itself an override~Z () override; // implies ~Y() is virtual or is itself an override};The override and final keywords are only useful if used consistently. However, being part of the language itself means we can now enlist the help of the compiler to catch subtle errors that couldn't be easily caught before:struct X {void f () override; // error! X did not inherit an f () thus it cannot be an override!virtual void g (); // okvirtual ~X(); // ok};struct Y : X {void g () override final; // ok~Y () override; // ok};struct Z : Y {void g () override; // error! Y::f() is declared final -- it cannot be overridden!~Z () override; // ok};struct Z2 : Z final {}; // okstruct Z3 : Z2 {}; // error: Z2 is final -- cannot be used as a base class!


What is the difference between Virtual Base Class and Abstract Class in c plus plus?

You might as well compare apples and oranges. The only thing they have in common is that they are both base classes (just as apples and oranges are both fruit). An abstract base class is an abstract data type (ADT). An ADT is defined as any class that declares one or more pure virtual functions. This prevents anyone from instantiating objects from the class other than through derivation. The derivative must implement all pure-virtual methods declared in its base class, otherwise it becomes an ADT itself. Once a derivative overrides a pure-virtual method, that method becomes virtual with respect to all subsequent derivatives. However, only derivatives that provide or inherit a complete implementation for the sum of all pure-virtual methods can physically be instantiated. A virtual base class is a base class that is inherited virtually. That is, if class A is declared a virtual base class of class B, then any derivative of B will automatically inherit directly from A. This is useful in multiple inheritance where two or more intermediate classes are themselves derived from a common base class. Normally, the most-derived class will inherit one instance of the common base class from each of the intermediate base classes. By declaring the common base class to be virtual in the intermediate classes, the most-derived class inherits just one instance of the common base class, which is then shared amongst the intermediate classes. Thus if class A inherits from classes B and C and they each inherit from D, then there will be two instances of D in the hierarchy (A::B::D and A::C::D). This introduces ambiguity when referring directly to A::D. But if B and C inherit from D virtually, then there is only one instance of D (A::D), and both B and C derive from this one, shared instance, virtually. This removes any ambiguity and reduces the overall footprint of the hierarchy by eliminating redundant instances of the shared base class. Ideally, virtual base classes should have little or no data members.


When is a virtual base class useful in c plus plus?

Consider the following class declarations: class Base{...}; class Intermediate1: public Base{...}; class Intermediate2: public Base{...}; class Derived: public Intermediate1, public Intermediate2{...}; Here, Derived indirectly inherits two instances of Base, one from Intermediate1 and the other from Intermediate2. This introduces an ambiguity when implicitly referring to Base from Derived. We must explicitly refer to Intermediate1::Base or Intermediate2::Base. Not only that but since there are two instances of Base, they consume twice as much memory. Clearly it would be better if there were only one instance of Base shared between Intermediate1 and Intermediate2. This isn't always possible, but if we assume that it is possible in this case, we need to allow Derived to inherit directly from Base and for Intermediate1 and Intermediate2 to both share that same instance, virtually. We do this by declaring Base to be virtually inherited by both Intermediate1 and Intermediate2: class Base{...}; class Intermediate1: public virtual Base{...}; class Intermediate2: virtual public Base{...}; class Derived: public Intermediate1, public Intermediate2{...}; Now there's only one instance of Base which is directly inherited by Derived. Thus when Derived, Intermediate1 or Intermediate2 implicitly refer to their Base class, they will implicitly refer to Derived::Base. Moreover, any objects we subsequently derive from Derived will inherit directly from Base themselves, but it remains the one and only instance of Base in the hierarchy. Note that the virtual keyword can be placed before or after the access specifier, it makes no difference (both methods are shown above). Note also that the virtual keyword is only of relevance to Derived and its derivatives. It does not affect any non-derived instances of Intermediate1 nor Intermediate2 -- they still inherit directly from Base. I mentioned earlier that it isn't always possible to make use of virtual base classes. It all depends on the purpose of the classes involved and what members they have. Generally, the fewer members in the base class the better, so abstract base classes are often good candidates for virtual base classes. Ultimately, all derivatives must be capable of sharing the same instance of the virtual base class.


Type of inheritances in c plus plus?

Single, multiple, multi-level, hierarchical and hybrid/virtual inheritance. Single inheritance applies when one class inherits from just one base class. Multiple inheritance applies when one class inherits from two or more base classes. Multi-level inheritance applies to a class that inherits from at least one base class that is itself derived from another base class. Hierarchical inheritance applies to a base class that is inherited by two or more separate derived classes. Hybrid inheritance combines multiple inheritance, multi-level inheritance and hierarchical inheritance. That is, where A is a common base class of derived classes B and C, and B and C are both base classes of derived class D. Hybrid inheritance is often used with virtual inheritance where B and C inherit from A virtually rather than directly. In these cases, the virtual base class is instantiated by the most-derived class in the hierarchy, D, and this instance is then shared by both B and C.


What is the order of construction and destruction in c plus plus?

The least-derived base classes are always constructed first, in the order specified by the derived class inheritance list. The most-derived class (the one you are actually instantiating) is always constructed last. Destruction is basically the reverse of construction. However, base class destructors must be declared virtual to ensure that the most-derived class destructor is called first, regardless of which class destructor is actually invoked. That is, if you hold a pointer to a base class that has no virtual destructor, deleting that pointer will only destroy the base class, not the derived class, leaving the derived class in an invalid state (because it no longer has an underlying base class) and with no way to recover the memory it consumes. It is important to remember that if you declare any virtual methods within a class, you must also declare the destructor virtual. A class without a virtual destructor is not intended to be derived from. If it has virtual methods, but no virtual destructor, it is not well-formed and must not be used as a base class.


Explain virtual functions in C plus plus?

A virtual function in C++ is a function that can have multiple definitions.For example:If you have a class which contains a virtual function:class Virtual{virtual void makesomething();};That function can be implemented when you inherit that class an implement the function. So:class Inherit : public Virtual{//this is the same function, but can be implemented to do something differentvoid makesomething() { //do something else }};