Three confusing concepts in C ++

Source: Internet
Author: User

Methods and functions in JAVA are called in two special forms:Heavy LoadAndRewriteAndC ++The virtual function keyword virtual is added, and variables are added to function calls: Besides overloading and rewritingOverwrite.

I believe that when most of us write code, in order to ensure the readability and speed of the program, we subconsciously avoid these syntaxes similar to brain teasers, try to use different function names to differentiate functions of the base class and the derived class. Of course, there are many functions that cannot be avoided ).

But sometimes, some interview examiners always like to embarrass you with these three concepts and check whether your C ++ foundation is solid. Therefore, we need to differentiate the three concepts for the interview.

To put it bluntly, you must be aware of the heavy load and rewriting, because it is too widely used. As for hiding it, C ++ is designed for the interviewer to wait for bricks ).

1. heavy-load features: In the same class, function names are the same; parameters are different; virtual keywords are dispensable.

2. Overwrite) FeaturesYes: It is located in the derived class and the base class respectively. The function name is the same; the parameter is the same; the base class function must have the virtual keyword ).

I personally think that the above two points are easy to remember, but the Hidden Rules of C ++ suddenly increase the complexity of the problem. The rules are as follows:

1. If the function of the derived class has the same name as the function of the base class, but the parameter is different. At this time, no matter whether there is a virtual keyword, the function of the base class will be hidden and do not confuse with the overload ).

2. If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. In this case, the base class functions are hidden and do not confuse them with rewriting ).

I'm afraid it's hard to understand these many tongue twisters. Let's look at a simple example:

 
 
  1. Class {
  2. Public:
  3. Void f (int );
  4. };
  5. Class B: public {
  6. Public:
  7. Void f (char * p)
  8. };
  9. Void main (){
  10. Int I = 1;
  11. B B;
  12. A * pa = & B;
  13. B * pb = & B;
  14. Pa-> f (I); // call the method of parent class;
  15. Pb-> f (I); // What about here? Where is the method called?
  16. };

According to the habitual thinking, I think the pointer pb in the main function will call the method of the parent class. In fact, it is not correct. Hiding has taken effect. Here it meets the hidden condition 1, therefore, the parent method is hidden, and the method of the subclass called does not match the parameter type. The Compiler directly reports an error !!

If we are developing, we can fully compile and find such errors, so it doesn't matter if we fix them in time. But the terrible thing is that during the interview, if you are not familiar with the concepts of rewriting, overloading, and hiding, errors may occur.

In fact, this example is a part of the actual questions I encountered during an interview a few years ago.) At that time, I did not know enough about this concept and did something wrong, so I still remember it. In this case, we hope to help you with the need to participate in the interview.

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.