C++ Object-Oriented Programming Supplement (1) The difference between overloading and covering hiding

Overloading, overriding, and hiding
member functions Overloading, overriding, and hiding member functions are easy to confuse. C++ programmers must figure out the
concepts, otherwise mistakes will be hard to prevent.
8.2.1 Overloading and overriding The characteristics of overloaded
member functions : (1) The same scope (in the same class); (2) The function name is the same; (3) The parameters are different; (4) The virtual keyword may have Optional. Override is to assign a function of a new class to cover a function of a base class . The characteristics are: (1) Different scopes (located in the derived class and base class respectively); (2) The function name is the same; (3) The parameters are the same; (4) The base class function must There is a virtual keyword. In Example 8-2-1, the functions Base::f(int) and Base::f(float) are mutually overloaded, and Base::g(void) is covered by Derived::g(void). #include <iostream.h> 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)
}
Example 8-2-1 Overloading and Overriding of Member Functions
8.2.2 Confusing Hiding Rules
It is not difficult to distinguish between overloading and overriding. , But the hidden rules of C++ make the problem more complicated.
Here "hidden" means that the function of the assigned class shields the function of the base class 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 parameters are different. At this time, regardless of the virtual
keyword, the function of the base class will be hidden (be careful not to be confused with overloading).
(2) If the function of the derived class has the same name as the function of the base class, and the parameters are also the same, but the base class function does not have the virtual
keyword. At this time, the function of the base class is hidden (be careful not to confuse with coverage).
In the sample program 8-2-2 (a):
(1) Function Derived::f(float) covers Base::f(float).
(2) Function Derived::g(int) hides Base::g(float) instead of overloading.
(3) The function Derived::h(float) hides Base::h(float) instead of covering it.
#include <iostream.h>
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;}
};
Example 8-2-2 (a) Overloading, covering and hiding of member functions
According to the author's investigation , Many C++ programmers don't realize there is such a thing as "hidden". Due to the lack of deep understanding,
the occurrence of "hidden" can be described as extraordinary, often producing confusing results.
In Example 8-2-2(b), bp and dp point to the same address. It stands to reason that the results of the operation should be the same, but this
is not the case.


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
}
Example 8-2-2 (b) Comparison of overloading, covering and hiding
8.2.3 Get rid of hiding The
hiding rule has caused a lot of trouble. Example 8-2-3 program, the statement pd-> f (10) The intention is to call the function
number Base :: f (int), but the Base :: f (int) unfortunately Derived :: f (char *) Hidden. Since the number 10
cannot be implicitly converted to a string, an error occurs at compile time.
class Base
{ public:

void f(int x);
};
class Derived : public Base
{
public:
void f(char *str);
};
void Test(void)
{
Derived *pd = new Derived;
pd->f(10); // error
}
Example 8-2-3 error due to hiding



Reprinted from: http://www.cnblogs.com/qlee/archive/2011/07/04/2097055.html

Guess you like

Origin blog.csdn.net/hgz_gs/article/details/51841994
Recommended