In
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
, a virtual base class is a nested
inner class In object-oriented programming (OOP), an inner class or nested class is a class declared entirely within the body of another class or interface. It is distinguished from a subclass.
Overview
An instance of a normal or top-level class can exist on ...
whose functions and member variables can be overridden and redefined by
subclasses of an outer class. Virtual classes are analogous to
virtual function
In object-oriented programming, in languages such as C++, and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method for which dynamic dispatch is facilitated. This concept is an important ...
s.
The run time type of a virtual class depends on the run time type of an object of the outer class. (Just like the run time type of an object decides which virtual function should be used.)
A run time instance type of the outer class object not only decides on the polymorphic type of its own type object, but also on a whole family tree of virtual class members.
Purpose
Virtual classes solve the
extensibility
Extensibility is a software engineering and systems design principle that provides for future growth. Extensibility is a measure of the ability to extend a system and the level of effort required to implement the extension. Extensions can be t ...
problem of extending data abstraction with new functions and representations. Like virtual functions, virtual classes follow the same rules of definition, overriding, and reference.
When a derived class inherits from a base class, it must define or
override
Override may refer to:
* Dr. Gregory Herd, a Marvel Comics character formerly named Override
* Manual override, a function where an automated system is placed under manual control
* Method overriding, a subclassing feature in Object Oriented pro ...
the virtual inner classes it inherited from the base class. An object of the child class may be referred to by a reference or pointer of the parent class type or the child class type. When the reference or pointer invoke the virtual inner classes, the derived class's implementation will be called if the object is of the derived class type. The type of the outer class determines the run time of the inner virtual class.
A method with an object argument has access to the object's virtual classes. The method can use the virtual classes of its arguments to create instances and declare variables. Virtual classes of different instances are not compatible.
Example
For example, a base class
Machine
could have a virtual class
Parts
. Subclass
Car
would implement
Parts
differently than the subclass
Bicycle
, but the programmer can call any methods in the virtual inner class
Parts
on any class
Machine
object, and get the
Parts
implementation of that specific derived class.
#include
class Machine ;
// The inner class "Parts" of the class "Machine" may return the number of wheels the machine has.
class Car: Machine ;
Any object of class type
Machine
can be accessed the same way. The programmer can ask for the number of wheels (by calling
get_wheels()
), without needing to know what kind of machine it is, how many wheels that machine has, or all the possible types of machines there are. Functions like
get_fuel_type()
can be added to the virtual class
Parts
by the derived class
Car
.
See also
*
Inheritance (object-oriented programming)
In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object ( prototype-based inheritance) or class ( class-based inheritance), retaining similar implementation. Also defined as deriving new cla ...
*
Superclass (computer science)
In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object ( prototype-based inheritance) or class ( class-based inheritance), retaining similar implementation. Also defined as deriving new clas ...
*
Virtual inheritance
Virtual inheritance is a C++ technique that ensures only one copy of a base classs member variables are inherited by grandchild derived classes. Without virtual inheritance, if two classes B and C inherit from a class A, and a class D inherits ...
*
Virtual function
In object-oriented programming, in languages such as C++, and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method for which dynamic dispatch is facilitated. This concept is an important ...
References
External links
*
*
{{DEFAULTSORT:Virtual Class
Object-oriented programming