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.
Chat with our AI personalities
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.
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.
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 }};
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.
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).