Introduction to C ++ programming skills

Source: Internet
Author: User

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:

 
 
  1. "class1.cs(54,24): warning CS0114: 'ListBox.Sort()' hides  
  2. 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:

 
 
  1. public class ListBox : Window {  
  2. 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.

 
 
  1. Employee: Employee (int theAge, int theSalaryLevel ):
  2. Person (theAge) // initialize the basic class
  3. SalaryLevel (theSalaryLevel) // initialize the member variable
  4. {
  5. // Constructor code
  6. }

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:

 
 
  1. Class Employee: public Person
  2. {// Member variable definition
  3. PrivateSalaryLevel=3; // Initialization
  4. }

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.

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: info-contact@alibabacloud.com 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.