The difference between overloading, overwriting, and hiding of member functions in C + +

Source: Internet
Author: User

Reprint please specify, original address: Click to open the link

http://blog.csdn.net/u010587274/article/details/38928561

Personal public Number: tanzi_888 (Tam zi technology circle)

The difference between overloading, overwriting, and hiding of member functions in C + +:


1 overloaded (overload): Is the function name is the same, the argument list of different overloads only exist within the class. But it cannot be judged by the return value type.
1.1) The same range (in the same class)
1.2) function with the same name
1.3) Different parameters

1.4) virtual keyword is optional

2 overrides: Also called overrides, subclasses redefine virtual functions of the same name and parameters in the parent class, function features are the same, but the implementation differs mainly in the inheritance relationship.
2.1) Different scopes, respectively, in the base class and the derived class
2.2) The name of the function is the same as

2.3) Same parameters

2.4) The base class function must have a virtual keyword

3 Hidden (redefining): Also known as redefinition, refers to a function of a derived class that masks a base class function with the same name as the following rule:

3.1) If the function of the derived class and the function of the base class have the same name, but the arguments are different, at this point, the function of the base class is hidden, regardless of whether it is virtual or not.
3.2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, but the base class function does not have the virtual keyword, the function of the base class is hidden


Here are 3 examples to deepen the difference:
Example 1
#include <iostream.h>
using namespace Std;
Class Base
{
Public
void f (int x) {cout << base::f (int) << x << Endl;}
void f (float x) {cout << base::f (float) << x << Endl;}
virtual void g (void) {cout << "base::g (void)" << Endl;}
};
Class Derived:public Base
{
Public
virtual void g (void) {cout << "derived::g (void)" << Endl;}
};
void Main (void)
{
Derived D;
Base *PB = &d;
Pb->f (42); Base::f (int) 42
Pb->f (3.14f); Base::f (float) 3.14
Pb->g (); Derived::g (void)
}
In Example 1, base::f (int) is overloaded with base::f (float), while base::g (void) is overridden by derived::g (void).


Example 2
#include <iostream.h>
using namespace Std;
Class Base
{
Public
virtual void F (float x) {cout << base::f (float) << x << Endl;}
void g (float x) {cout << base::g (float) << x << Endl;}
void h (float x) {cout << base::h (float) << x << Endl;}
};
Class Derived:public Base
{
Public
virtual void F (float x) {cout << derived::f (float) << x << Endl;}
void g (int x) {cout << "derived::g (int)" << x << Endl;}
void h (float x) {cout << derived::h (float) << x << Endl;}
};
void Main (void)
{
Derived D;
Base *PB = &d;
Derived *PD = &d;
Good:behavior depends solely on type of the object
Pb->f (3.14f); Derived::f (float) 3.14
Pd->f (3.14f); Derived::f (float) 3.14
Bad:behavior depends on type of the pointer
Pb->g (3.14f); Base::g (float) 3.14
Pd->g (3.14f); Derived::g (int) 3 (surprise!)
Bad:behavior depends on type of the pointer
Pb->h (3.14f); Base::h (float) 3.14 (surprise!)
Pd->h (3.14f); Derived::h (float) 3.14
}
In Sample program 2:
(1) The function derived::f (float) is covered with base::f (float).
(2) the function derived::g (int) hides base::g (float) instead of overloading.
(3) The function derived::h (float) hides the base::h (float) instead of the overlay.
According to the authors, many C + + programmers are unaware of the "hidden" thing. Because the understanding is not deep enough,
"Concealment" is a shadowy occurrence, often producing confusing results.
In Example 2, BP and DP point to the same address, which is supposed to be the same as the operating result.
This is not the case.


Example 3
Class Base
{
Public
void f (int x);
};
Class Derived:public Base
{
Public
void f (char *str);
};
int main ()
{
Derived *pd = new Derived;
Pd->f (10); Error caused by hidden errors
return 0;
}
Hiding the rules caused a lot of trouble. In the example 3 program, the statement pd->f (10) is intended to invoke the letter
Number Base::f (int), but base::f (int) is unfortunately hidden by derived::f (char *). Since the Number 10
cannot be implicitly converted to a string, so there is a compile-time error. So for the presence of hidden, be sure to pay attention!

The difference between overloading, overwriting, and hiding of member functions in C + +

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.