In C ++, the Sort method in the Windows class will become the basic method of the Sort method in The ListBox class. When you expect to call the Sort method in the Windows class, the Sort method in The ListBox class is called. In C #, virtual functions are always considered as the root of virtual scheduling. That is to say, once C # discovers a virtual method, it will no longer find other virtual methods in the virtual chain. If the ListBox is compiled again, the compiler generates a warning message:
- "class1.cs(54,24): warning CS0114: 'ListBox.Sort()' hides
- inherited member 'Window.Sort()'.
To overwrite the current member to the original method, you need to add the override or the new one. To eliminate the warning information, the programmer must understand what he wants. You can add new before the Sort method in The ListBox class to indicate that it should not overwrite the virtual method in the Window:
- public class ListBox : Window {
- public new virtual void Sort() {"}
In this way, the warning information can be cleared. If programmers really want to overwrite the methods in the Window, they must use override to explicitly express their intent.
- Employee: Employee (int theAge, int theSalaryLevel ):
- Person (theAge) // initialize the basic class
- SalaryLevel (theSalaryLevel) // initialize the member variable
- {
- // Constructor code
- }
The initialization in C # is different from that in C ++. Assume that there is a Person class with a private member variable age. The Employee is generated by continuing the Person class, and it has a private salaryLevel member variable. In C ++, We can initialize salaryLevel in the initialization part of the constructor of the Employee, as shown in the following code:
- Class Employee: public Person
- {// Member variable definition
- PrivateSalaryLevel=3; // Initialization
- }
This method is invalid in C. Although the basic class can still be initialized, initialization of member variables like the above Code will cause compilation errors. In C #, We can initialize a member variable while defining it: the access permission for each variable must be clearly defined.
I have read countless articles describing the complexity of C ++, and I have discussed the complexity of C ++. I wrote two articles about C ++ ). I have always felt like a blind person. As a reader of "Why C ++" criticized, I did not specify in my article what is C ++'s "non-essential complexity ".
- Explore the rich and colorful C ++ Technology
- In-depth analysis of C ++ code compilation and statement Sequence
- Programming in the Visual C ++ Environment
- Senior Scholars Talk About C ++ programming skills
- Exploring flexible C/C ++ language commands
Of course, I can know it by myself, and even if I have been in contact with C ++ for a while, however, new users or even non-beginners have no specific understanding of what I call "non-essential complexity, this leaves the article "Why C ++" out of its original intent-oriented to all C ++ users and learners.