answersLogoWhite

0


Best Answer

Whenever two or more classes are derived from the same base class, and another class is derived from both derivatives via multiple inheritance, and the common base class can be shared amongst all derivatives.

For instance, if a base class, V, is a common base class of two derived classes, A and B, and class D is derived from both A and B, then class D will inherit two separate instances of class V (one from A, the other from B). By declaring V to be virtual in both A and B, class D will only inherit one instance of V, which is then shared between A and B. Any changes made to the member variables of the shared V will be automatically reflected in A, B and D.

Bear in mind that not all base classes can be declared virtual. If V contains any member variable that must be unique to A and B, then A and B cannot share the same instance of V.

It would therefore follow that D probably cannot share V with either A or B. If D requires its own instance of V, the only solution is for D to inherit from A, B and V, thus A, B and D will each have their own separate instances of V.

However, if D can share an instance of V from either A or B, then there is no need for D to inherit its own copy of V. In this case, you can use the scope resolution operator to refer to a specific instance of V, whether it belongs to A or B. In other words, A::V or B::V.

Ultimately, the decision to make V virtual in direct derivatives will depend on whether a single instance of V can be shared between those derivatives and that is entirely dependant upon the actual purpose of V. To be a virtual base class, V should ideally be an abstract data type with no unique member variables whatsoever. And provided classes A, B and D override all the pure-virtual methods inherited from V, there will be no ambiguity whatsoever. Invoking a pure-virtual method on V will call the most-derived method in D, which can, optionally, call the overridden methods in either A or B, or both, via their scope resolution operators.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When and why is it required to inherit virtual base class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why is ios a virtual base class?

There is no class named "ios" anywhere in the C++ standard library. <ios> is simply the header file. The actual class is called basic_ios and this serves as the base class for the basic_istream and basic_ostream classes (declared in <istream> and <ostream> respectively). Although most streams are used for either input or output and therefore inherit from either basic_istream or basic_ostream, some streams are used for both input and output and therefore inherit both base classes. However, because both base classes share a common base class in basic_ios, the derived stream would inherit two instances of this class where only one is required. Thus basic_istream and basic_ostream both declare basic_ios as a virtual base class, thus ensuring the most-derived object in the hierarchy inherits just one instance of basic_ios.


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.


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 is the advantages of virtual base class?

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.


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.

Related questions

Why is ios a virtual base class?

There is no class named "ios" anywhere in the C++ standard library. <ios> is simply the header file. The actual class is called basic_ios and this serves as the base class for the basic_istream and basic_ostream classes (declared in <istream> and <ostream> respectively). Although most streams are used for either input or output and therefore inherit from either basic_istream or basic_ostream, some streams are used for both input and output and therefore inherit both base classes. However, because both base classes share a common base class in basic_ios, the derived stream would inherit two instances of this class where only one is required. Thus basic_istream and basic_ostream both declare basic_ios as a virtual base class, thus ensuring the most-derived object in the hierarchy inherits just one instance of basic_ios.


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.


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 is the advantages of virtual base class?

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.


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.


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


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.


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


What are the applications of An Abstract Class in c plus plus?

An abstract class is a class that has a pure virtual function. A pure virtual function can be used in a base class if the function that is virtualised contains parameters that can and should be interchangeable, but the function should remain intact. For example, there is a base class Object in a game that will contain movement for simple objects, but the movement function may need to use different parameters per object.