Virtual class

Uses your system voice; voice and rate in settings.

Snapshot

≈ 2 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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==

Text derived from https://en.wikipedia.org/w/index.php?oldid=1165573834 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot enwiki-001165573834 · captured 1689464610.0 · version history

Cite this page as: encyclopegia.org, “Virtual class”, snapshot enwiki-001165573834. https://encyclopegia.org/a/virtual-class