We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What is a Virtual Class?

By Rodney A. Crater
Updated: May 16, 2024
References

A virtual class is a class that is allocated only once in memory, such that children of that class use only the one copy of that parent class which is established in memory. A class is a group of objects that share common traits or attributes. Making a class virtual gives object-orientated programming languages the capability of using multiple inheritance when second or more generation parent classes are derived from common ancestral base classes. Depending on the computer programming language referenced, the "virtual" keyword, similar keyword, or syntax and semantics of a class are used to imply that a particular class is to be considered virtual.

Not all object-orientated programming languages allow multiple inheritance, which is when a child class can be derived from multiple parent classes at the same time. In a programming language like C++, a class can inherit all of the member data and member functions from both parent classes at once and have access to both sets of parent data and functions. Other languages which support multiple inheritance include Perl, Python, Tcl, and Eiffel.

A problem occurs in multiple inheritance hierarchies when two or more parents classes used to create a child class are originally derived from only one or the same grandparent class. This is called the diamond problem, the name stemming from how the hierarchy would look in this situation. When a compiler program tries to instantiate a class, such as a grandchild class made from two parent classes with a common grandparent, two copies of the grandparent are made in memory, one for each parent.

Due to the ambiguity caused by multiple copies of the same grandparent in memory, the compiler is unable to ascertain which copy of the grandparent the grandchild should use to access grandparent data or functions. To remedy this situation, in C++ the grandparent is made into a virtual class when declaring the parents. This causes the compiler to only make one copy of the grandparent in memory that both parents share. Once the grandparent has become a virtual class, the compiler has no problem deciding how the grandchild should access the grandparent because there is only one copy of the grandparent.

Great care should be taken when structuring class hierarchies, especially when they will be included in or become the base for much larger hierarchies in the future. The proper utilization of a virtual class gives class hierarchies greater latitude in development; however, they have the potential to cause errors that are difficult to locate. The study of solid computer programming engineering principles will help to ensure that unexpected side effects from improper program organization do not occur.

EasyTechJunkie is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
Link to Sources
Discussion Comments
Share
EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.

EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.