Class member function overload / override / redefining
1. The characteristics of overload of member functions:
(1) The same scope (in the same class);
(2) Function names are the same;
(3) Different parameters;
(4) The virtual keyword is optional.
(5) Return values can be different
In short, function overloading depends on the number, type and order of function parameters
2. Override refers to a derived class function covering a base class function, characterized by:
(1) Different scopes (in derived and basic classes respectively);
(2) Function names are the same;
(3) The same parameters;
(4) Base class functions must have virtual keywords.
(5) The return value is the same, otherwise the error will be reported;
(6) Access modifiers for overlay functions can be different; (private to public and others)
Redefining overloaded functions in a base class hides all other base class versions of the function.
3. redefining (also hidden) means that functions of derived classes shield base class functions of the same name as those of derived classes. The rules are as follows:
(1) Not in the same scope (in derived and basic classes, respectively)
(2) The function name is the same;
(3) The return values can be different;
(4) 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, regardless of the virtual keyword, the functions of the base class will be hidden (be careful not to be confused with overloading).
(5) If the function of the derived class has the same name as the function of the base class and the parameters are the same, but the base class function has no virtual keyword. At this point, the functions of the base class are hidden (be careful not to confuse coverage).
Function overload is relatively easy to understand, mainly redefining concealment and override rewriting are easy to confuse.
Conclusion:
override rewriting is for base class Virtual functions. In derived classes, besides access modifiers, function names, return values and parameter lists are the same.
redefining (also known as hiding) is different in derived classes except for the same function name (return values, parameter tables, access attributes).
Another important sentence is: what type of pointer is, then the function called by the pointer is the function of the pointer type (except for coverage, in this case).
For example: Derive a; Base * P = & a; p - > func(); if func() function is a virtual function in the base class and it is covered in the derived class, then p - > func() calls the function of the derived class, otherwise it calls the func() function of the base class, whether or not the derived class redefines the func() function.
#include<iostream>
#include<string>
using namespace std;
class Base
{
private:
virtual void display()
{
cout<<"Base display()"<<endl;
}
public:
void say()
{
cout<<"Base say()"<<endl;
}
public:
void exec()
{
display();//Coverage;
say();//Redefining concealment;
}
private:
void fun1(string a) {cout<<"Base fun1(string)"<<endl;}
void fun1(int a) {cout<<"Base fun1(int)"<<endl;}//overload, two fun1 functions are overloaded inside the Base class
};
class ChildA:public Base
{
public:
//override is a list of display ing function parameters in the base class. The function names are the same and virtual functions are modified as virtual functions, so it is overridden here.
void display( )
{
cout<<"ChildA display()"<<endl;
}
//redefining, fun1 function has the same name as the function of the base class, but the parameter list is different, so it is redefined.
void fun1(int a,int b) {cout<<"ChildA fun1(int,int)"<<endl;}
//redefining, say function has the same name as the base class function and the same parameter list, but the base class function has no virtual modification, so it is redefined.
void say()
{
cout<<"ChildA say()"<<endl;
}
};
class ChildB:public Base
{
public:
//redefining is the same as fun1 function name and parameter list in the base class, but the base class function has no virtual modification, so it is redefined.
//Redefining functions can have different return types.
int fun1(double a)
{
cout<<"ChildB fun1(double)"<<endl;
return 0;
}
};
int main()
{
ChildA a;
Base* b= &a;
//display():version of DeriveA call(polymorphism)
//say():version of Base called(allways )
b->exec();
//The function display ing in b is overwritten by class A, and say is its own.
//Call any function with any pointer.
b->say();
a.exec(); //same result as last statement
a.say();
ChildB c;
c.fun1(1.1); //version of ChildB called
}
Take a note and read it later.