Home » C++ » The difference analysis of several concepts of rew...

The difference analysis of several concepts of rewriting, covering, overloading, hiding and polymorphism

reprinted from: http://blog.csdn.net/yukin_xue/article/details/7437742






Override-> rewrite (= cover) overload-> heavy, polymorphism,
, polymorphism





Override is a method of rewriting (overwriting) to implement different functions. It is generally used for subclasses to override (re implement) the method.
in the parent class when inheriting a parent class
Overloaded member function (overload), coverage (override) and hide easily confused, C++ programmers must clear the concept, otherwise the error will be impossible to guard against.



1 overloading and covering

The member function is overloaded with features:

(1) the same range (in the same class);

(2) the function name is the same;

(3) the parameters are different;

(4) the virtual keyword is optional,.



Coverage refers to the derived class function that overrides the base class function and is characterized by:

(1) different ranges (in the derived class and the base class);

(2) the function name is the same;

(3) the parameters are the same;

(4) the base class function must have the virtual keyword.

In example 1, the function Base:: F (int) and Base:: F (float) are overloaded with each other, while Base:: G (void) is covered by Derived:: G (void).



#include



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 1 overloading and override of member functions



2 confusing hidden rules

Originally, only difference between heavy and cover is not difficult, but the hidden rules of C++ make the problem complexity increase suddenly. This "hidden" refers to the function of shielding the derived class base class function with the same name, the rules are as follows:



(1) if the function of a derived class has the same name as the function of the base class, but the parameter is different, then the function of the base class will be hidden, whether or not there is a virtual keyword (note not to be confused with the heavy load)..

(2) if the derived class functions the same name as the function of the base class and has the same parameters, the base class function does not have the virtual keyword. At this point, the base class function is hidden (note not to be confused with the cover).



Sample program 2 (a):

(1) function Derived:: F (float) covers Base:: F (float).

(2) function Derived:: G (int) hides Base:: G (float) instead of overloading.

(3) function Derived:: H (float) hides Base:: H (float) instead of covering.





#include



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 2 (a) overloading of member functions, covering and hiding



According to the investigation, many C++ programmers not aware of it. Because of the "hidden" understanding "is the elusive hidden", often produce confusing results.



In example 2 (b), BP and DP point to the same address. By definition, the running results should be the same, but that's 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 2 (b) overloading, covering and hiding comparison



3 get rid of hidden

The hidden rules caused a lot of trouble. 3 sample program, the statement pd-> f (10)'s intention is to call the function Base:: F (int), Base: f (int), but unfortunately Derived:: F (char *) hidden. Because the number 10 can be implicitly converted to a string, so wrong 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 3 causes error
due to concealment


From example 3, it seems silly to hide the rules, but there are at least two reasons for hiding the rules:

Write statement pd-> f (10) people may really want to call Derived:: F (char *) function, but he mistook the wrong parameters. With the hidden rules, the compiler can clearly point out mistakes, this is not necessarily a good thing. Otherwise, the compiler will quietly leave, programmers will be very difficult to find this mistake.
, flow under the curse
If Derived has multiple base classes (multiple inheritance), sometimes do not know what defines the function F. if there are no hidden rules, then pd-> f (10) may be called an unexpected F. despite the hidden rules of the base class function did not seem to make sense, but it can eliminate these accidents.

In example 3, if the statement pd-> f (10) must call the function Base:: F (int), then modify the class Derived to as follows,.



Class Derived: public Base

... {

Public:

Void f (char *str);

Void, f (int, x)... {Base:: F (x);}

};

Latest