Clause 12: Determine your public inheritance plastic film out is-a relationship:
This article mainly will be a few special cases: penguins are birds, penguins can inherit from birds, but birds can fly, penguins can not fly, and allow the square to inherit the rectangle may also cause this embarrassment!
This problem has been thought about before, but do not know how to solve, if real life is really to use: such as the Cat Pike pig and so many animals inherit animal class, but cats and dogs can not swim,
If there are many animals here, can not use the fish inside a special method! This has not been figured out yet, clause 12 does not say that if you want to use the way to deal with it is going to avoid this.
Is-a;
In object-oriented programming, IA-A refers to the parent-child inheritance relationship of a class;
Public inheritance means is-a, and everything that applies to base-class must also be adapted to derived class. Therefore, each child class object is also a parent class object.
Article 13: Avoid Hiding the inherited name:
This article is mainly about the cover-up inheritance, in fact, we often say that the overload rewrite (overwrite) hidden; Let's take a look at the difference between overloaded overrides hiding:
1, overloading characteristics: In the same class, the function name is the same, the parameters are different, the virtual keyword is optional.
2. Override (overwrite) characteristics are: in the derived class and base class, the function name is the same, the parameters are the same, the base class function must have the virtual keyword (this is very important to note).
About Hide:
1. If the function of the derived class has the same name as the function of the base class, 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 (note that it is not confused with rewriting).
06.cpp: Defines the entry point of the console application. * Purpose: Find out the difference between overloading, rewriting, and hiding time: December 1, 2014 19:06:03*//************************************************************************/#include "stdafx.h" #include <iostream>using namespace Std;class base{private:int x;public:virtual void mf1 () = 0;virtual void mf1 (int)//overload, in the same class, or within the same working range. {cout<< "Call the MF1 function of the base class" <<ENDL;} virtual void Mf2 () {cout<< "calls the MF2 function of the base class" <<ENDL; void Mf3 () {cout<< "calls the MF3 function of the base class" <<ENDL; void Mf3 (Double) {cout<< "Call the mf3 overloaded function of the base class" <<endl;}}; Class derived:public base{public:virtual void Mf1 () {cout<< "calls the MF1 function of the subclass <<endl;} void Mf3 () {cout<< "calls the MF3 function of the subclass" <<ENDL; void Mf4 () {cout<< "Call mf4 function of subclass" <<endl;}}; int _tmain (int argc, _tchar* argv[]) {Derived d;int x;d.mf1 ();//Call the subclass's MF1 function This is overriding (overriding) this is the most fundamental cause of dynamic polymorphism//d.mf1 (x);//error C2660: "DERIVED::MF1": the function does not accept 1 parameters;//Although there is a mf1 (int) function in the base class base, there is a mf1 () function in the subclass itself//So there is a hidden d.mf2 ();//This has no problem, son.The class inherits the function of the parent class, and the subclass does not have its own mf2 () function, so it calls the function of the parent class D.MF3 ();//invokes the MF3 () function of the subclass, the MF3 () function in the base class is not virtual, but the function of the subclass is called, and the function of the subclass is hidden. If a function of a derived class has the same name as a 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 (note that it is not confused with rewriting). D.MF3 (x);//error C2660: "DERIVED::MF3": the function does not accept 1 parameters//Although the base class has a mf3 (double) function, but because there are also its own mf3 () function in the subclass//So there is a hidden/* from D.MF1 (x ) and D.MF3 (x) can be seen in virtual void mf1 (int) with void mf3 (double), one with virtual one without virtual but they all happen to be hidden. */getchar (); return 0;}
The above code should be a good explanation of the difference between the three; in clause 33, if you do not want the subclass to hide the function of the parent class, then you achieve the goal with the using declaration in the subclass:
Class derived:public base{public:virtual void Mf1 () {cout<< "calls the MF1 function of the subclass <<endl;} Using Base::mf1;void mf3 () {cout<< "calls the MF3 function of the subclass" <<ENDL;} void Mf4 () {cout<< "Call mf4 function of subclass" <<endl;}};
Effective C + + reading notes clause 12 and clause 13