answersLogoWhite

0


Best Answer

A conventional base class is a class that has a virtual destructor. Virtual destructors are important in most class hierarchies because while we may not always know the exact type of the most-derived class in a hierarchy, we will always know at least one of its base classes. If we destroy that base, it is reasonable to expect the entire object to be destroyed, not just the base, and this can only be achieved with a virtual destructor.

Note that a virtual destructor must be declared in the least-derived class in the hierarchy to ensure correct polymorphic behaviour.

Classes not intended to be used as base classes do not have virtual destructors. This does not mean they cannot be used as base classes, but if you choose to use them as base classes you must be aware of the "unconventional" problems that can arise, particularly when destroying objects through pointers or references to their base classes as will lead to resource leaks.

If you do not wish a class to be used as a base class, then the class should be declared final (since C++11). All other classes can still be used as base classes, however only classes with virtual destructors behave polymorphically.

Typically, if we define at least one virtual function in a class, we should also define a virtual destructor for that class. However, derived classes implicitly inherit virtual destructors from their bases thus the absence of a virtual destructor in a derived class should not be taken to mean the derived class is not intended to be used as a base class; always look at the least-derived class definition to determine if a virtual destructor exists or not.

Along with the final specifier, the overridespecifier was also introduced in C++11. This makes it much easier to document virtual functions and their overrides. That is, we can use the virtual specifier solely to introduce a new virtual function into the class hierarchy, and use the overridespecifier to show that we are explicitly overriding a virtual function rather than simply introducing a new virtual function. We can also use the final specifier to indicate that an individual virtual function cannot be overridden any further:

struct X {

virtual void f ();

virtual ~X ();

};

struct Y : X {

void f () override; // implies X::f() is either virtual or is itself an override

~Y () override; // implies ~X() is virtual or is itself an override

};

struct Z : Y {

void f () override; // implies Y::f() is either virtual or is itself an override

~Z () override; // implies ~Y() is virtual or is itself an override

};

The override and final keywords are only useful if used consistently. However, being part of the language itself means we can now enlist the help of the compiler to catch subtle errors that couldn't be easily caught before:

struct X {

void f () override; // error! X did not inherit an f () thus it cannot be an override!

virtual void g (); // ok

virtual ~X(); // ok

};

struct Y : X {

void g () override final; // ok

~Y () override; // ok

};

struct Z : Y {

void g () override; // error! Y::f() is declared final -- it cannot be overridden!

~Z () override; // ok

};

struct Z2 : Z final {}; // ok

struct Z3 : Z2 {}; // error: Z2 is final -- cannot be used as a base class!

User Avatar

Wiki User

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

Wiki User

6y ago

Base classes allow one class to derive from another. A derived class inherits all of the public and protected members of its base classes, thus a base class provides a common interface to all of its derivatives. Circles and squares are both types of shape, thus a shape class can be used as a common base class for both. This makes it possible to create collections of different types of objects, provided we hold references or pointers to the common base class of those objects.

Classes intended to be used as base classes usually have one or more virtual methods, thus enabling polymorphic behaviour. That is, whenever we invoke a virtual method, the most-derived override of that method is always executed regardless of which object in the hierarchy was used to invoke the method. In this way, base classes need not know anything about their derivatives; we obtain the correct behaviour by invoking the virtual methods we know about. This is important when we know the base class interface of an object at compile time but do not know the object's actual runtime-type. We could use a dynamic cast to obtain the actual runtime type, but a dynamic cast is much more expensive than invoking a virtual method and is often indicative of a design error. However, dynamic casts are useful when we don't have access to the base class source code and therefore cannot provide a suitable virtual interface.

Given that any class within a class hierarchy can be destroyed at any time, the least-derived base class or classes must have a virtual destructor to ensure the most-derived class is always destroyed first. Object construction, on the other hand, is always bottom-up, starting with the least-derived base class in the hierarchy. During construction of any given class, its direct base classes (if any) are constructed first, followed by its member objects. That is; a derived object cannot be constructed until all its base classes are constructed. Virtual destructors ensure object destruction is the exact reverse of object construction.

Abstract base classes are base classes that declare one or more pure-virtual methods. Unlike "ordinary" virtual methods which may be overridden by a derived class, pure-virtual methods must be overridden. Moreover, while a class that declares a virtual method must provide an implementation for that method, a pure-virtual method does not require an implementation. Once overridden by a derived class, a pure-virtual method becomes a virtual method with respect to any other derivatives of that class.

Abstract base classes differ from "ordinary" base classes in that they cannot be instantiated by themselves; they can only be instantiated through derivation. This mirrors real-life abstractions; we cannot instantiate a shape object by itself, we can only instantiate a specific type of shape, such as a square or circle. Thus the shape class is an abstraction; we can certainly imagine such an object exists, but it cannot physically exist by itself.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Conventional base class in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Represent a stack in c plus plus?

Use a vector with a base class type. Any objects derived from the base class can be pushed and popped from the vector just as you would from a stack.


What is meant by inheritance in c plus plus language?

Inheritance in C++ and in other Object Oriented languages is the creation of a class that incorporates a different class. The child (or derived) class "inherits" all of the elements (attributes and methods) of the parent (or base) class. Depending on the design of the base class, the derived class can use methods and attributes of the base class as if they were its own. Typically, however, attributes of the base class are private to the base class and inaccessible to the derived class so as to maintain class hierarchy and data encapsulation.


Write a c plus plus programme to illustrate single inheritance?

struct A {}; // base class struct B : A {} // derived class (single inheritance).


When you define a c plus plus class what items are considered part of the interface?

The interface of a C++ class is the public methods and attributes that are exposed by the class. In a pure abstract base class, the interface is enforced by the compiler in each of the derived child classes.


Can a c plus plus class be derived from a Java class?

No.


How do you implement inheritance in c plus plus?

You implement inheritance by deriving a new class of object from an existing class of object. The existing class is known as the base class of the derived class.Classes declared final cannot be used as bases classes and classes without a virtual destructor (or a virtual destructor override) cannot be used as polymorphic base classes.


What is multilevel inheritance in C plus plus?

Multi-level inheritance involves at least 3 classes, a, b and c, such that a is derived from b, and b is derived from c. c is therefore the least-derived base class, b is an intermediate base class and a is the most-derived class.


What is single inheritance in c plus plus?

Multiple inheritance occurs when a class is derived directly from two or more base classes. class b1 {}; class b2 {}; class d: public b1, public b2 {}; // multiple inheritance class


When does c plus plus use generic function implicitly?

C++ uses the generic function implicitly whenever the base class implementation (the generic method) is also the most-derived implementation.


What is the role of object in c plus plus?

An object in C++ is an instance of a C++ 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.


What is taxonomy in c plus plus?

Taxonomy is just another word for class hierarchy. E.g., an animal class can serve as a base class for both mammals and reptiles, while the mammal class can then be a base class for both dogs and apes, and so on. In other words, it is a method of classifying and organising your classes such that common properties are percolated up into generic base classes, reducing the need to duplicate code.