A. Features that are overloaded with member functions:
(1) The same range (in the same class);
(2) The function has the same name;
(3) different parameters;
(4) The virtual keyword is optional.
Because the function parameters are different, it can be easily understood as: two overloaded functions are different functions, the caller can explicitly
Different functions are called according to different parameters. So what happens to the compiler if there are two of these functions?
Class A
{
Public
void Func (int a, int b=0) {printf ("This is func1/n");}
void Func (int a) {printf ("This is func2/n");}
};
int main ()
{
A;
A.func (5);
return 0;
}
Of course, for such two functions, the caller does not know which function should be called, so the compiler directly error.
We are looking at, what are the characteristics of covering and hiding separately? In the literal sense, covering and hiding have a different
One to cover up, that is only the problem of who cover who.
B. Overwrite refers to a derived class function overriding a base class function, characterized by:
(1) different ranges (in the derived and base classes, respectively);
(2) The function has the same name;
(3) the same parameters;
(4) The base class function must have the virtual keyword.
We found that the virtual function is used here, in fact, the function of virtual function is to achieve coverage.
C. " Hide refers to a function of a derived class that masks a base class function with the same name as the following rule:
(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 point, the function of the base class is hidden, regardless of the virtual keyword (Note that it is not 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 the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay)
See examples below:
#pragma once#include<iostream>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 test1 () {derived d; Base *pb = &d;derived *pd = &d;pb->f (3.14f);pd->f (3.14f);p b->g ( 3.14f);pd->g (3.14f);p b->h (3.14f);pd->h (3.14f);}
Operation Result:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/84/96/wKiom1eVqIWjWwAFAAAwROiGymc206.png "title=" QQ picture 20160725134837.png "alt=" Wkiom1evqiwjwwafaaawroigymc206.png "/>
One more thing to avoid, static member functions for static, is the method of the class, not the method of the object, the
The static method must never be overwritten or hidden.
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1829616
C + + overloading, overriding and hiding differences