The differences between overload, overwrite, and hide of class member functions

Source: Internet
Author: User

The differences between overload, overwrite, and hide of class member functions

Overload:
Features of member functions being overloaded:
(1) the same range (in the same class );
(2) The function name is the same;
(3) parameters are different;
(4) virtual keywords are optional.

1 # include <iostream> 2 3 using std: cin; 4 using std: cout; 5 using std: endl; 6 7 class A 8 {9 public: 10 void show (int val) {cout <val;} 11 void show (double val) {cout <val ;}12}; 13 14 int main (void) 15 {16 A; 17. show (4); 18 cout <endl; 19. show (4.2); 20 cin. get (); 21}View Code

 

Overwrite:
Override refers to the function of a derived class that overwrites the base class function. The features are as follows:
(1) different scopes (located in the derived class and the base class respectively );
(2) The function name is the same;
(3) The parameters are the same;
(4) basic functions must have virtual keywords.

1 # include <iostream> 2 3 using std: cin; 4 using std: cout; 5 using std: endl; 6 7 class A 8 {9 public: 10 virtual void show (int val) {cout <val ;}11}; 12 13 class B: public A14 {15 public: 16 void show (int val) {cout <"--" <val <"--" ;}17 }; 18 19 int main (void) 20 {21 A; 22. show (4); 23 cout <endl; 24 25 A * p = new B; 26 p-> show (5); 27 28 cin. get (); 29}View Code

 

Hide:
Hiding is a function in a derived class that shields base class functions with the same name. 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. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with 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. At this time, the base class functions are hidden (Be sure not to confuse with overwrite)

 

Note: When Tom sees other people's documents, he adds the code to record them. Don't sue me! I have no money. O (∩ _ ∩) O Haha ~


Reload, overwrite, and hide c ++ member functions

The relationship between the basic class and the derived class is like that between the parent and the child. I can ask the Son to be the officer, but the son cannot always let the father go to the officer, right .. Therefore, the pointer of a derived class cannot direct to the base class, but the base class pointer can point to the derived class. When the base class Pointer Points to the derived class, you want to use a function of a derived class with the same name, it must be defined as a virtual function in the base class. When the program is running, it can dynamically point to the function of the derived class. Therefore, these concepts should be clear, we recommend that you read the Turing series published by People's post and telecommunications publishing house in c ++ primer (version 4 ).
Good luck ~~~~~~~

What is the difference between the override and overload of the base class member methods of a derived class?

Function coverage and overloading should not be compared. When two independent functions have the same function name but have different function signatures (that is, the type, quantity, and order of parameters ), its relationship is an overload. the override of the base class member functions by the derived class is strictly a redefinition (that is, rewriting) of the base class member functions. Their function signatures can be the same or different, in this case, it does not conform to the definition of function overload.

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.