answersLogoWhite

0

Why is ios a virtual base class?

Updated: 10/27/2022
User Avatar

Wiki User

7y ago

Best Answer

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.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why is ios a 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 &lt;iostream&gt; class base { public: virtual ~base(); virtual void PrintMe() const { std::cout &lt;&lt; "I am a base class!" &lt;&lt; std::endl; } }; class derived: public base { public: void PrintMe() const { std::cout &lt;&lt; "I am a derived class!" &lt;&lt; std::endl; } }; int main() { base b; derived d; base* pb = &amp;d; b.PrintMe(); d.PrintMe(); pb-&gt;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 &lt;iostream&gt; class base { public: base(){ std::cout &lt;&lt; "Base class created" &lt;&lt; std::endl; } ~base(){ std::cout &lt;&lt; "Base class destroyed" &lt;&lt; std::endl; } }; class derived: public base { public: derived(){ std::cout &lt;&lt; "Derived class created" &lt;&lt; std::endl; } ~derived(){ std::cout &lt;&lt; "Derived class destroyed" &lt;&lt; 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 &lt;iostream&gt; class base { public: base(){ std::cout &lt;&lt; "Base class created" &lt;&lt; std::endl; } virtual ~base(){ std::cout &lt;&lt; "Base class destroyed" &lt;&lt; std::endl; } }; class derived: public base { public: derived(){ std::cout &lt;&lt; "Derived class created" &lt;&lt; std::endl; } ~derived(){ std::cout &lt;&lt; "Derived class destroyed" &lt;&lt; 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.