The comparison of abstract virtual in C + + and C #

Source: Internet
Author: User
Tags abstract comparison contains inheritance multiple inheritance in c

More than a year ago, I went to interview an Indian company, do C + +, the treatment is good. One of the topics is let me introduce vitual base class, I haven't read the C + + textbook for a long time, suddenly Gedeng, this is a what dongdong, seems very familiar, but never used, anyway, can not think of it. In the words of Uncle Alexander, when did I ever give you eyes? Yes, what's vitual base class?

Go home a book, that's it. Now summarize and record, "Lest You Forget" (Horse language)

The virtual keyword can declare virtual methods in C + + and C #, and these methods can be overridden in a derived class. And the abstract keyword is introduced in C #, it is used to declare the virtual method without function body, in C + +, this work is also done by the virtual keyword, we call pure virtual methods. The class that contains the pure virtual method is called the abstract class. In addition, there are several places to note for these two keywords.


In C + +, all the things related to polymorphism are specified by virtual. In addition to specifying polymorphism, virtual has two special usages:

Virtual base class

Ambiguity for the resolution of multiple inheritance. Simply put, base class A has two derived classes: B and C, then D inherits from B and C. There is a problem here, D has two parts A, not only waste, but also a lot of problems. The virtual base class is for D to have only one copy of a. The use method is:

Class B:virtual Public A

Class C:virtual Public A

Class D:public B, public C

Abstract class

Any class that contains pure virtual function is called the abstract class. Pure virtual Funciton means that there is no function implementation, and that the function has a ' = 0 ' tag, similar to the following function.

void virtual foo () = 0;

Note that, unlike C #, abstract is not a keyword in C + +.


The Virtual keyword is used to modify properties and methods, indicating that an inheriting class can be overridden. This keyword has a much weaker role in C # than C + +. In C + +, any declarations related to polymorphism are virtual. In C #, most of this authoritarian power was interface, and the abstract was split away.

The abstract keyword can be decorated with more than classes, methods, properties, indexers, and events. This keyword is more fierce. This is understandable, because object-oriented thinking is mainly defined as a class of façade, which can be seen from 23 design patterns, and abstract is to do this, the so-called born in the time.

In summary, in C + + only use virtual as a keyword, abstract only contains a part of the pure virtual methods class collectively: abstract class. In C #, virtual,abstract are all keywords, abstract is clear, do things like C + +. There is no multiple inheritance in C #, and virtual base class is unnecessary.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.