Differences between Overloading, overwriting, and hiding of class member functions

Source: Internet
Author: User

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 a;17     a.show(4);18     cout << endl;19     a.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 a;22     a.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 ~

Differences between Overloading, overwriting, and hiding of class member functions

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.