answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: 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?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is it necessary to provide a definition to the pure virtual function in the inherited derived class?

No, it is not necessary, but the derived class will be rendered abstract as well. Sometimes that can be desirable. Only derived classes that provide a complete implementation of all inherited pure-virtual methods can actually be instantiated. However, derived classes can also inherit pure-virtual implementations from any intermediate base classes, but not from the least-derived abstract base classes (where the pure-virtual methods originated). Abstract base classes can also provide their own default implementations of their own pure-virtual methods, but they cannot be called implicitly, even from the base class itself. These methods must still be overridden by derived classes, even if an override only needs to explicitly call the base class method.


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.


Importance of virtual functions?

Private virtual functions are useful when you expect a particular method to be overridden, but do not wish the override to be called from outside of the base class. That is, the base class implementation and its overrides remain private to the base class.Private virtual methods are particularly useful in implementing template method patterns, where certain algorithmic steps need to be deferred to subclasses, but where those steps need not be exposed to those subclasses. In many cases the private virtual methods will be declared pure-virtual, thus rendering the base class an abstract base 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.


What are the implecations of making a function pure virtual function?

Only class instance methods can be rendered as pure-virtual functions (or pure-virtual methods, to be precise). Non-member functions and static member methods cannot be declared pure-virtual.The implication of declaring a pure-virtual function is that the class to which it is a member becomes an abstract data type (or abstract base class). This means you cannot instantiate an object of that class, you can only derive classes from it. Moreover, the derived classes must also implement the pure-virtual functions or they, too, become abstract data types. Only concrete classes that contain a complete implementation can be instantiated, although they can inherit implementations from their base classes (but not from the class that initially declared the method).Pure-virtual functions ensure that you do not instantiate base class objects that are not intended to be instantiated (they are conceptual rather than actual objects) and that derived objects provide a specific implementation. Typically, all methods of an abstract base class will be declared pure-virtual to ensure correct behaviour in derived classes through a common interface. Abstract base classes will also have few member variables, preferably none at all.For example, a shape is a conceptual object whereas a rectangle, triangle or circle are actual objects. As such they can all be derived from shape. The shape base class need only declare the interface that is common to all shapes, such as Draw(), Rotate(), Mirror() and so on. But since a shape doesn't have enough information to implement these methods, they must be declared pure-virtual. This ensures that shape cannot be instantiated (since it would be meaningless) and that every object derived from shape not only has a common interface, but that each type of shape provides its own specific implementation of those methods. This is not unlike overriding standard virtual functions, however the difference is that you must override the pure-virtual methods; it is not optional unless the derived class is intended to become abstract itself.

Related questions

Is it necessary to provide a definition to the pure virtual function in the inherited derived class?

No, it is not necessary, but the derived class will be rendered abstract as well. Sometimes that can be desirable. Only derived classes that provide a complete implementation of all inherited pure-virtual methods can actually be instantiated. However, derived classes can also inherit pure-virtual implementations from any intermediate base classes, but not from the least-derived abstract base classes (where the pure-virtual methods originated). Abstract base classes can also provide their own default implementations of their own pure-virtual methods, but they cannot be called implicitly, even from the base class itself. These methods must still be overridden by derived classes, even if an override only needs to explicitly call the base class method.


What is binding in c plus plus?

Binding refers to classes and their member methods. A class that has no virtual methods can simply be bound to all its methods at compile time. This is known as static binding. However classes with virtual functions require special handling as they can be overridden by derived classes. If the derived class cannot be determined at compile time, then its overridden methods are dynamically bound at runtime, using the virtual table (or v-table) to determine which version of a method must be called.


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.


Importance of virtual functions?

Private virtual functions are useful when you expect a particular method to be overridden, but do not wish the override to be called from outside of the base class. That is, the base class implementation and its overrides remain private to the base class.Private virtual methods are particularly useful in implementing template method patterns, where certain algorithmic steps need to be deferred to subclasses, but where those steps need not be exposed to those subclasses. In many cases the private virtual methods will be declared pure-virtual, thus rendering the base class an abstract base 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.


What are the implecations of making a function pure virtual function?

Only class instance methods can be rendered as pure-virtual functions (or pure-virtual methods, to be precise). Non-member functions and static member methods cannot be declared pure-virtual.The implication of declaring a pure-virtual function is that the class to which it is a member becomes an abstract data type (or abstract base class). This means you cannot instantiate an object of that class, you can only derive classes from it. Moreover, the derived classes must also implement the pure-virtual functions or they, too, become abstract data types. Only concrete classes that contain a complete implementation can be instantiated, although they can inherit implementations from their base classes (but not from the class that initially declared the method).Pure-virtual functions ensure that you do not instantiate base class objects that are not intended to be instantiated (they are conceptual rather than actual objects) and that derived objects provide a specific implementation. Typically, all methods of an abstract base class will be declared pure-virtual to ensure correct behaviour in derived classes through a common interface. Abstract base classes will also have few member variables, preferably none at all.For example, a shape is a conceptual object whereas a rectangle, triangle or circle are actual objects. As such they can all be derived from shape. The shape base class need only declare the interface that is common to all shapes, such as Draw(), Rotate(), Mirror() and so on. But since a shape doesn't have enough information to implement these methods, they must be declared pure-virtual. This ensures that shape cannot be instantiated (since it would be meaningless) and that every object derived from shape not only has a common interface, but that each type of shape provides its own specific implementation of those methods. This is not unlike overriding standard virtual functions, however the difference is that you must override the pure-virtual methods; it is not optional unless the derived class is intended to become abstract itself.


What is the abstract feature in c plus plus?

Abstraction generally refers to abstract classes. An abstract class is one that cannot itself be instantiated, but can be derived from. Abstract classes are conceptual classes rather than concrete classes, and are intended to provide a common interface to the concrete classes derived from them. A class becomes abstract when it contains one or more pure-virtual methods, which must be implemented in the derived classes otherwise they become abstract classes themselves. A concrete class is one that provides a complete implementation for is abstract base classes, or inherits implementations from base classes other than those that originally declared the function to be pure-virtual. An example of an abstract class is Shape. All shapes can be drawn, therefore Shape will have a Draw() method. However, it cannot implement this method unless it knows what type of shape it actually is. Therefore the Draw() method should be declared pure-virtual, thus rendering the Shape class to be abstract. You can then derive concrete shapes from the Shape class, such as Circle and Square classes, which will each provide the actual implementation for the Draw() method. Since Square and Circle are types of Shape, you can create collections of Shape objects and call their Draw() methods without regard to their actual type. If the object is really a Square, then calling Shape.Draw() will actually invoke Square.Draw(). There is no need to determine the actual type of the shape because every shape has a common interface. This is really no different to overriding virtual methods in non-abstract classes. The main difference is that one would not be expected to instantiate a Shape (because it is abstract), other than through derivation, and the pure-virtual methods MUST be implemented by the derived classes, whereas normal virtual methods are simply expected to be overridden but it is not a requirement to do so unless you require an enhancement to the base class method. Abstract classes typically have few if any member variables and little or no implementation. Ultimately, the class exists purely to provide a common interface to all its derivatives, and only functionality and member variables that are common to all its derivatives should be placed within it. Even if the abstract class provides a complete implementation for all its pure-virtual methods, you cannot instantiate an abstract class -- it must be derived from -- and all pure-virtual methods must still be implemented in the derivatives, even if only to call the base class methods explicitly.


What is the virtual keyword used for in C plus plus?

Virtual functions (or virtual methods as they are also known), are methods declared in a class that are expected to be overridden by a derived class. That is, the method is a generic method that applies to the class in which it is declared (the base class), but that may require modification by a more-specialised class (the derived class). When you implicitly call the base class method, the overridden method is called instead, thus ensure the correct behaviour. The overridden can still call the base class method explicitly, as can any other code that has access to the base class method.If you do not declare generic methods to be virtual, overriding those methods in the derived class may have unexpected side-effects, particularly if the method is overloaded in the base class. For instance, a derived class' override will effectively hide all the overloads of the base class.Note that if you declare any virtual function, then you are essentially notifying the end-user that the class is intended to act as a generic class and may be derived from, if required. However, if the function is declared pure-virtual, you are notifying the end-user that the class is abstract and mustbe derived from (it is not optional).Also, if there are any virtual methods then the destructor must be virtual as well, to ensure the derived object is destroyed before the base class is destroyed.It is often good practice to declare all methods as virtual if there's at least one virtual method, however this only applies to methods that would require alternative handling by more-specialised classes. If the base class can provide a complete implementation that requires no further specialisation, it needn't be declared virtual.Although there is a memory cost involved in declaring virtual methods, the bulk of that cost is paid with the first virtual function which establishes the virtual table (v-table). Thereafter, the cost is minimal. The v-table ensures the most-derived method is called, thus ensuring correct behaviour of derived classes, even when working with the generic base class.


What is a pure virtual member function in c plus plus?

Functions in C++ are separate procedures or subroutines within a program, that can be called as often as required and which can return values back to their callers. That is, when you make a function call, execution passes to the function and then returns to the caller. Functions that are class members are also known as member functions, member methods, or simply methods. These work exactly the same as external functions except they are scoped to the class and have access to private members of the class.


What does it mean to be concrete derived class?

A concrete derivative is a class that fully implements all the pure-virtual methods it inherits from its abstract base classes. Some methods may be inherited rather than implemented, however only a concrete class can actually be instantiated.


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.