Virtual class

This article is about virtual classes. For virtual base classes, see Virtual inheritance. For pure virtual classes, see Abstract type. For internet-based teaching, see Virtual education.

In object-oriented programming, a virtual class is a nested inner class whose functions and member variables can be overridden and redefined by subclasses of the outer class.[1] Virtual classes are analogous to virtual functions.

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 problem of extending data abstraction with new functions and representations. Like virtual functions, virtual classes follow the same rules of definition, overriding, and reference.[2]

When a derived class inherits from a base class, it must define or override 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.

class Machine
{
    void run () {}

    virtual class Parts
    {
    };
};

// The inner class "Parts" of the class "Machine" may return the number of wheels the machine has.
class Car: public Machine
{
    void run() { 
        cout << "The car is running." << endl; 
    }
    class Parts
    {
        int get_Wheels () {
            cout << "A car has 4 wheels." << endl;
            return 4;
        }
        string get_Fuel_Type () {
            cout << "A car uses gasoline for fuel." << endl;
            return "gasoline";
        }
    };
};

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

References

  1. "Virtual Classes". CaesarJ Programming Guide. Technische Universität Darmstadt. 2006-02-10. Retrieved 2007-07-30.
  2. "Overview of Virtual Classes" (PDF). A Virtual Class Calculus. University of Aarhus, Denmark. 2006. Retrieved 2014-04-30.

External links

This article is issued from Wikipedia - version of the Monday, July 20, 2015. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.