e01.org

Virtual class

In object-oriented programming, a virtual base class is a nested inner class whose functions and member variables can be overridden and redefined by subclasses of an outer class. 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.

In object-oriented programming, a virtual base class is a nested inner class whose functions and member variables can be overridden and redefined by subclasses of an outer class. 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. include class Machine { public: void run() { } class Parts { public: virtual int get_wheels() = 0; virtual std::string get_fuel_type() = 0; }; }; // The inner class "Parts" of the class "Machine" may return the number of wheels the machine has. class Car: Machine { public: void run() { std::cout 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) Superclass (computer science) Virtual inheritance Virtual function ==References== ==External links==

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Virtual class". 1689464610. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1165573834

JSON record · API collection · citation policy