answersLogoWhite

0


Best Answer

Virtual classes are useful when you have a derived class that has two base classes which are themselves derived from a common base class. That is, if class A is derived from classes B and C, and classes B and C are both derived from class D, then class A would inherit two instances of class D (one from B, the other from C). This introduces an ambiguity when referring directly to D from A, because there is no way to determine which instance of D you are referring to. By declaring class D to be a virtual base class of both B and C, they will both share the same instance of D, virtually, thus eliminating the ambiguity.

User Avatar

Wiki User

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

Wiki User

10y ago

A virtual base class is a class that can act as a common base class for two or more base classes in multiple inheritance hierarchies, without the need for multiple instances of that common class. For instance, if class A is a common base class of B and C, and class D inherits from both B and C, then class D inherits two instances of A, one for B, the other from C. This introduces ambiguity when referring directly to A from D. However, if B and C both inherit from A virtually, then they can both share the same instance of A, thus eliminating the ambiguity and reducing the overall memory footprint of D.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

In multiple inheritance, two or more base classes may share a common base class. If those base classes do not inherit the common base class virtually, then you end up with multiple instances of the common base class. With virtual inheritance, there is only one instance. In effect, the virtual base class becomes a direct ancestor of the most derived class, and all base classes that inherit virtually from that base class will share that one instance.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Virtual base classes are useful in multiple inheritance cases where two or more classes share a common base class. For instance, if class A is a common base class of classes B and C, and class D is derived from both B and C, then class D inherits two instances of class A (one from B, the other from C). This introduces an ambiguity to class D insofar as you cannot access A implicitly; you must explicitly access A via B or D. By declaring A to be virtual in both B and C, they will both share the same instance of A, thus class D inherits only one instance of A, thus eliminating the ambiguity, and reducing the memory footprint of class D.

Ideally, virtual base classes are abstract data types with little or no implementation and few if any member variables. Even so, not all base classes can be declared virtual in derived classes.

This answer is:
User Avatar

Add your answer:

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

What is true when a derivation inherits both a virtual and non-virtual instance of a base class?

Each derived class object has base objects only from the non virtual instance


Difference between base class and derived class?

The derived class derives, or inherits, from the base class. The derived class is like a "child", and the base class, the "parent". The child class has the attributes of the parent class - its variables (those defined at the class level) and methods. Changes done to the parent class (the base class) will affect the child class (the derived class).


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 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.


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.

Related questions

What is true when a derivation inherits both a virtual and non-virtual instance of a base class?

Each derived class object has base objects only from the non virtual instance


Difference between base class and derived class?

The derived class derives, or inherits, from the base class. The derived class is like a "child", and the base class, the "parent". The child class has the attributes of the parent class - its variables (those defined at the class level) and methods. Changes done to the parent class (the base class) will affect the child class (the derived class).


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 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.


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.


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 }};


What should be structure of class when it has to be a base for other classes?

If a class is intended to be used purely as a base class then it must have one or more pure-virtual functions (a function that may or may not have an implementation). Such a class is regarded as being an abstract base class because no instances of the class can be instantiated other than through derivation, and the derivative must provide an implementation or it too becomes an abstract base class. The abstract base class need not declare a pure-virtual method if it inherits one or more pure-virtual methods from another base class but does not provide a complete implementation. Only classes that provide a complete implementation can actually be instantiated. Implementations may be inherited from lower base classes other than the one that declared the function pure-virtual in the first place. Once a derivative implements a pure-virtual function, that implementation may be inherited by subsequent derivatives. If the base class may be instantiated in its own right then it must not declare any pure-virtual methods, nor must it inherit any pure-virtual methods that have no implementations. In this case the base class may be derived from, but doesn't have to be derived from, whereas an abstract base class must be derived from.


What is the Practical application of a derived class object stored in base class pointer?

If you have base class derived object pointing by base class pointer, then you have the power of run time polymorphism in your hand, which gives you the ability to call the derived class implementation of the virtual member function. If we declare the member function as virtual in base class which needs to overridden in derived class, then you can decide at run time which implementation will be called at run time.


Can you have virtual constructor in cpp?

A virtual function is a method defined and implemented in a base class that we are expected to override in our derived classes.When we derive a class from a base class, we automatically inherit all the public and protected members of the base class, but we are free to override any and all the methods, including private methods (we can even change the access type if we wish).However, when we override a method in the base class, we can only call that override when we actually have a reference or pointer to the derived class itself. If we cast the reference or pointer to the base class we will end up calling the base class method. This is expected behaviour and is normally fine, but what happens if we have a pointer to a base class but we actually want to call the derived class method? This is particularly important when that call comes from the base class itself. How will it determine its actual type and make the correct call?We could use runtime information within the base class to determine the actual type, but this would break a fundamental rule of encapsulation: a base class should NEVER be concerned about the inner workings of any of its derived classes. If we allow this, we'd be forced to update the base class every time we derived a new class from it. Apart from the performance penalty incurred with accessing runtime information, maintaining the base class code will quickly become unmanageable.That is where virtual functions come in. Even though we are pointing at a base class, when we call a virtual function we actually call the derived class method instead. This is extremely powerful: we are no longer concerned with the actual type of object any more. We can treat the base class as if it were a generic data type, and the compiler will know exactly which version of a function to call regardless of whether the call was made from the base class or not.But what if we want to call the base class method? Simple: make an explicit call to it. We don't need runtime information for this since every derived class is also a base class as well. The only exception is when the virtual function is declared private in the base class. In this case, we are not expected to call the base class method at all (only the base class and friends of the base class can make an explicit call to its private methods).


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.


How you can override base class member in derived classexplain with example?

To override a base class method you simply need to declare the base class method as being virtual. As well as creating a v-table, this also gives a visual hint to other developers that you expect the function to be overridden. The v-table ensures that all calls to the base class method are routed to the derived class method, thus ensuring objects behave polymorphically, according to their actual type, and not what we're actually pointing at. Consider the following example: #include <iostream> class base { public: virtual ~base(); virtual void PrintMe() const { std::cout << "I am a base class!" << std::endl; } }; class derived: public base { public: void PrintMe() const { std::cout << "I am a derived class!" << std::endl; } }; int main() { base b; derived d; base* pb = &d; b.PrintMe(); d.PrintMe(); pb->PrintMe(); return( 0 ); } Output: I am a base class! I am a derived class! I am a derived class! Note that although pb points to the base class instance of d, it still knows that it really is a derived class, as can be seen from the third line of output. Now try removing the virtual keyword from the base class method. The output will change as follows: Output: I am a base class! I am a derived class! I am a base class! Now your derived class thinks it is a base class. This is because the v-table no longer has no entry for that method, and therefore the call cannot be routed to the overridden derived class method. The base class method is called because that's what we're actually pointing at and the object no longer behaves polymorphically according to its actual type. Note also that if any method is declared virtual in a class, the class constructor must also be declared virtual. If you fail to do this, your classes will not be destroyed properly. The virtual keyword ensures that the most-derived class is always destroyed first, before working up the hierarchy of destructors to eventually destroy the least-derived class, the base class itself. Consider the following example without a virtual destructor: #include <iostream> class base { public: base(){ std::cout << "Base class created" << std::endl; } ~base(){ std::cout << "Base class destroyed" << std::endl; } }; class derived: public base { public: derived(){ std::cout << "Derived class created" << std::endl; } ~derived(){ std::cout << "Derived class destroyed" << std::endl; } }; int main() { derived* d = new derived(); base* b = d; delete( b ); return( 0 ); } Output: Base class created Derived class created Base class destroyed As you can see, the derived class was created but was not destroyed. We've created a memory leak: that memory cannot be recovered until the program ends. Now add the virtual keyword to the base class destructor: #include <iostream> class base { public: base(){ std::cout << "Base class created" << std::endl; } virtual ~base(){ std::cout << "Base class destroyed" << std::endl; } }; class derived: public base { public: derived(){ std::cout << "Derived class created" << std::endl; } ~derived(){ std::cout << "Derived class destroyed" << std::endl; } }; int main() { derived* d = new derived(); base* b = d; delete( b ); return( 0 ); } Output: Base class created Derived class created Derived class destroyed Base class destroyed Now we have the expected behaviour and have resolved the memory leak. Remember, if ANY method of a class is declared virtual, the destructor must also be declared virtual. Note that although derived classes need not use the virtual keyword in front of overrides (it is implied by the base class), there is no harm in explicitly declaring them as such, if only to give a visual hint that the methods are expected to be overridden by derivatives of the derivative (multi-level inheritance).


Can you have virtual constructor?

The short answer is no, you cannot. The long answer is that constructors are not functions that can be called directly, and overriding a base class constructor would have no practical meaning since the derived class is itself responsible for calling its own base class constructors (whether implied by omission or explicitly via the derived class' initialisation list). Even so, the derived class isn't calling the base class constructor directly (that's why constructors have no return value; the actual call is made behind the scenes). The base class itself may be derived in which case its base class must be constructed before it can be constructed. This is the complete reverse of how a virtual function behaves, and is the reason that destructors can be virtual but constructors cannot. When a base class is destroyed, all its derivatives must be destroyed first, starting with the most-derived class of object. This can only be achieved through virtual destruction.