text:
Virtual class
From Wikipedia, the free encyclopedia
Jump to: navigation, search
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.
This article provides insufficient context for those unfamiliar with the subject. Please help improve the article with a good introductory style. (October 2009) (Learn how and when to remove this template message)
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 an 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.
Contents
[hide]
1 Purpose
2 Example
3 See also
4 References
5 External links
Purpose[edit]
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[edit]
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[edit]
Inheritance
Superclass
Virtual inheritance
Virtual function
References[edit]
Jump up ^ "Virtual Classes". CaesarJ Programming Guide. Technische Universität Darmstadt. 2006-02-10. Retrieved 2007-07-30.
Jump up ^ "Overview of Virtual Classes" (PDF). A Virtual Class Calculus. University of Aarhus, Denmark. 2006. Retrieved 2014-04-30.
External links[edit]
Altherr, Philippe; Cremet, Vincent (2005). "Inner Classes and Virtual Types" (PDF). Ecole Polytechnique Fédérale de Lausanne (EPFL) Switzerland. Retrieved 2014-04-30.
Ernst, Erik; Ostermann, Klaus; Cook, William R. (2006). "A Virtual Class Calculus" (PDF). University of Aarhus. Retrieved 2007-07-30.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Virtual_class&oldid=831638233"
Categories:
Object-oriented programming
Hidden categories:
Wikipedia articles needing context from October 2009
All Wikipedia articles needing context
Wikipedia introduction cleanup from October 2009
All pages needing cleanup
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Article
Talk
Variants
Views
Read
Edit
View history
More
Search
Navigation
Main page
Contents
Featured content
Current events
Random article
Donate to Wikipedia
Wikipedia store
Interaction
Help
About Wikipedia
Community portal
Recent changes
Contact page
Tools
What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Wikidata item
Cite this page
Print/export
Create a book
Download as PDF
Printable version
Languages
Add links
This page was last edited on 21 March 2018, at 15:06.
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.
Privacy policy
About Wikipedia
Disclaimers
Contact Wikipedia
Developers
Cookie statement
Mobile view
Enable previews

key: html

type: local

value: div

attrs: id class

values: mw-page-base noprint

values: mw-head-base noprint

value: a

attrs: id class lang

values: firstHeading firstHeading en

values: siteSub noprint

values: jump-to-nav mw-jump

attrs: href title

attrs: href class title

values:
/wiki/Virtual_education
mw-redirect
Virtual education

values:
note
hatnote navigation-not-searchable

value: img

attrs: alt src width height srcset data-file-width data-file-height

value: b

attrs: class href

value: span

value: small

values: plainlinks metadata ambox ambox-style ambox-Context

values:
/wiki/Subclass_(computer_science)
mw-redirect
Subclass (computer science)

values:
/wiki/Inheritance_(computer_science)
mw-redirect
Inheritance (computer science)

values:
/wiki/Superclass_(computer_science)
mw-redirect
Superclass (computer science)

values:
reflist mw-references-wrap
list-style-type: decimal;

values: Category:Wikipedia articles needing context from October 2009

values: Category:All Wikipedia articles needing context

values: Category:Wikipedia introduction cleanup from October 2009

values: Discussion about edits from this IP address [ctrl-alt-n]

values: A list of edits made from this IP address [ctrl-alt-y]

values: You are encouraged to create an account and log in; however, it is not mandatory

values: You're encouraged to log in; however, it's not mandatory. [ctrl-alt-o]

values:
/wiki/Virtual_class
View the content page [ctrl-alt-c]
c

values: Discussion about the content page [ctrl-alt-t]

values: p-namespaces navigation vectorTabs p-namespaces-label

values:
p-variants
navigation
vectorMenu emptyPortlet
p-variants-label

values: Past revisions of this page [ctrl-alt-h]

values: p-views navigation vectorTabs p-views-label

values:
p-cactions
navigation
vectorMenu emptyPortlet
p-cactions-label

values:
mw-wiki-logo
/wiki/Main_Page
Visit the main page

values:
/wiki/Main_Page
Visit the main page [ctrl-alt-z]
z

values: Featured content – the best of Wikipedia

values: Find background information on current events

values:
/wiki/Special:Random
Load a random article [ctrl-alt-x]
x

values: portal navigation p-navigation p-navigation-label

values: Guidance on how to use and edit Wikipedia

values: About the project, what you can do, where to find things

values: A list of recent changes in the wiki [ctrl-alt-r]

values: portal navigation p-interaction p-interaction-label

values: List of all English Wikipedia pages containing links to this page [ctrl-alt-j]

values: Recent changes in pages linked from this page [ctrl-alt-k]

values:
/wiki/Wikipedia:File_Upload_Wizard
Upload files [ctrl-alt-u]
u

values: A list of all special pages [ctrl-alt-q]

values: Permanent link to this revision of the page

values: Link to connected data repository item [ctrl-alt-g]

values: Printable version of this page [ctrl-alt-p]

values: mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Virtual_class rootpage-Virtual_class skin-vector action-view