1. conflict between father and son
(1) Subclasses can define members of the same name in a parent class
(2) A member in a subclass hides a member of the same name (overwrite with the same name) in the parent class
(3) A member of the same name in the parent class still exists in the child class
(4) Accessing members of the same name in the parent class through the scope resolution (::)
"Programming experiment" with the same name member variable depth analysis
#include <iostream>#include<string>using namespacestd;classparent{ Public: intmi; Parent () {cout<<"Parent (): &mi ="<< &mi <<Endl; } };classChild: Publicparent{ Public: intmi; Child () {cout<<"Child (): &mi ="<< &mi <<Endl; }};intMain () {child C; C.MI= -;//mi in sub-classC.parent::mi= +;//The mi in the parent class, hidden, must be added to the scope-resolved accesscout<<"&C.MI ="<< &C.MI <<Endl; cout<<"C.MI ="<< C.MI <<Endl; cout<<"&c.parent::mi ="<< &c.parent::mi <<Endl; cout<<"C.parent::mi ="<< C.parent::mi <<Endl; return 0;}/*output: Parent (): &mi = 0x23fea8child (): &mi = 0X23FEAC&C.MI = 0X23FEACC.MI = 100&c.parent::mi = 0x 23fea8c. Parent::mi = +*/
2. Re- discussion of overloading
(1) The nature of overloaded functions is a number of different functions
(2) function names and parameter lists are unique identifiers
(3) Function overloading must occur in the same scope
3. function overloading between father and son
(1) A function in a subclass hides a function with the same name as the parent class
(2) Subclasses cannot overload member functions in a parent class
(3) using a scope-resolved character to access a function with the same name in the parent class
(4) Subclasses can define exactly the same member functions in the parent class
"Programming experiments" function overloading between sons and fathers
#include <iostream>#include<string>using namespacestd;classparent{ Public: intmi; voidAddintv) {mi+=v; } voidAddintAintb//two add in parent forms an overloaded relationship{mi+ = (A +b); } };/*1. The 3 Add functions in child make up the overloaded relationship 2, because a function named "Add" (as long as there are 1 functions with the same name as the parent class) is present, so all functions called add in the parent class will be hidden in the subclass .*/classChild: Publicparent{ Public: intmi; voidAddintv) {mi+=v; } voidAddintXintYintz) {mi+ = (x + y +z); }};intMain () {child C; C.MI= -;//mi in sub-classC.parent::mi= +;//The mi in the parent class, hidden, must be added to the scope-resolved accesscout<<"C.MI ="<< C.MI << Endl;// -cout <<"C.parent::mi ="<< C.parent::mi << Endl;// +C.add (1);//Subclass of AddC.parent::add (2,3);//the add of the parent class, hidden in the subclass, must be scoped to the access characterC.add (4,5,6);//Subclass of Addcout<<Endl; cout<<"C.MI ="<< C.MI << Endl;// thecout <<"C.parent::mi ="<< C.parent::mi << Endl;//1005 return 0;}
4. Summary
(1) Subclasses can define members of the same name in a parent class
(2) A member in a subclass hides a member of the same name in the parent class
(3) A function in a subclass and parent class does not constitute an overloaded relationship
(4) Subclasses can define exactly the same member functions in the parent class
(5) using a scope-resolved character to access a member of the same name in the parent class
47th lesson the conflict between father and son