Comparison of the differences between rewritten functions, hidden functions and overloaded functions in C++

  Table of contents

1. Function overloading

        1.1 Definition

        1.2 Rules for function overloading:

        1.3 The role of function overloading:

2. Function rewriting:

        2.1 Definition

        2.2 Example: 

3. Function hiding

        3.1 Definition

        3.2 Give an example:


1. Function overloading

        1.1 Definition

        When we were studying the encapsulation features of classes and objects, we learned a word called overloading. Function overloading is a special case. C++ allows several similar functions with the same name to be declared in the same scope. The functions of these same-name functions The formal parameter list (parameter number, type, order) must be different, which is often used to deal with the problem of different data types when implementing similar functions.

The case is as follows:

void Swap1(int* a, int* b);
void Swap2(float* a, float* b);
void Swap3(char* a, char* b);
void Swap4(double* a, double* b);

These four Swap functions are all function overloads, which have the same name but different formal parameter lists.

 

        1.2 Rules for function overloading:

1. The function names must be the same.
2. The parameter lists must be different (different number, different type, different parameter arrangement order, etc.).
3. The return types of functions can be the same or different.
4. Merely having different return types is not enough to be an overloaded function.

        

        1.3 The role of function overloading:

        Using the same function name in the same scope to name a group of functions with similar functions reduces the number of function names and avoids name space pollution, which is of great benefit to the readability of the program.

2. Function rewriting:

        2.1 Definition

        Rewriting is also called overwriting. Rewriting means that a function with the same function name, function parameters, and function return value as the parent class is written in the subclass that inherits the parent class . The subclass can use this method to implement different functions. functions to achieve polymorphic features. Therefore, it can be concluded that function rewriting is generally used: " When a subclass inherits a parent class, it rewrites (overrides) the method in the parent class ." The function characteristics are exactly the same, but the specific implementation can be different - (that is, the outer outline and appearance must be exactly the same, but the inner personality and habits can be different).

        2.2 Example: 

//父类
class Person {
public:
	void virtual Buy_Ticket() {
		cout << "Person权限——全票购买" << endl;
	}
};

//子类1:
class Student :public Person {
public:
	void virtual Buy_Ticket() {
		cout << "Student权限——半价购买" << endl;
	}
};

//子类2:
class Soldier :public Person {
public:
	void virtual Buy_Ticket() {
		cout << "Soldier权限——优先购买" << endl;
	}
};

        In the code above, the Buy_Ticket functions of the two subclasses form an override of the Buy_Ticket function of the parent class. The function written by the subclass has exactly the same function name, parameters, and return value as the parent class. 

3. Function hiding

        3.1 Definition

           In the process of learning inheritance, we learned a new vocabulary: hiding, which can also be called redefinition.

       Hiding means that the function of the subclass blocks the function of the parent class with the same name. as follows:


The two functions are in the scope of the parent class and the child class respectively, and the function names are the same ;
if
the functions of the same name in the two parent classes and the child class do not constitute overriding, then they must constitute hiding (redefinition) ;

        3.2 Give an example:

class A {
public:
	void Sleep() {
		cout << "睡好觉" << endl;
	}
};

class B :public A {
public:
	void Sleep(int a) {
		a++;
		cout << "睡长觉" << endl;
	}
};

int main(){

    A a;
    B b;
    a.sleep();
    b.sleep();    //报错
    b.A::sleep();    //正确
    return 0;
}

        In the code above, class A and class B both have a Sleep function with the same name. Since class B is a public inheritance class A, then class B will have a parameterless Sleep() function inherited from class A, and If class B object b wants to call the parameter-less Sleep() function, a compilation error will be reported, because the subclass B will hide the inherited member functions, so the compiler wants to call the parameter-less Sleep() by executing object b function, it is found that there is no parameterless Sleep() function in the subclass, and an error is reported! ! ! The subclass is secretly hidden, so the compiler can't find it!

        If you want to call a hidden function, you need to specify the scope! bA::sleep();

Guess you like

Origin blog.csdn.net/weixin_69283129/article/details/132028784