Name masking in C + + inheritance

Name masking in C + + inheritance

If the members in the derived class (including member variables and member functions) have the same name as the members in the base class, the members inherited from the base class will be masked. The so-called masking means that when using this member in a derived class (including when defining a derived class and accessing the member through a derived class object), it actually uses the new member of the derived class rather than inheriting from the base class.

The following is an example of the name masking of a member function:

#include<iostream>
using namespace std;
//Base class People
class People{
public:
    void show();
protected:
    char *m_name;
    int m_age;
};
void People::show(){
    cout<<"Hi, everyone. My name is"<<m_name<<",this year"<<m_age<<"year"<<endl;
}
//Derived class Student
class Student: public People{
public:
    Student(char *name, int age, float score);
public:
    void show();  //Mask show() of base class
private:
    float m_score;
};
Student::Student(char *name, int age, float score){
    m_name = name;
    m_age = age;
    m_score = score;
}
void Student::show(){
    cout<<m_name<<"What is your age"<<m_age<<",The result is"<<m_score<<endl;
}
int main(){
    Student stu("Xiao Ming", 16, 90.5);
    //Instead of inheriting from the base class, the new member function of the derived class is used
    stu.show();
    //Member functions inherited from the base class are used
    stu.People::show();
    return 0;
}

Operation results:

Xiao Ming's age is 16 and his grade is 90.5
 Hi, everyone. My name is Xiao Ming. I'm 16 years old

In this example, both the base class People and the derived class Student define the member function show (). Their names are the same, which will cause masking. In the code in line 37, stu is the object of Student class. The show() function of Student class is used by default.

However, the show() function in the base class People can still be accessed, but add the class name and domain resolver, as shown in line 39.

Base class member functions and derived class member functions do not constitute overloads

When the names of base class members and derived class members are the same, it will cause masking. This sentence is well understood for member variables. Pay attention to member functions. No matter what the parameters of the function are, as long as the names are the same, it will cause masking. In other words, the base class member function and the derived class member function will not constitute an overload. If the derived class has a function with the same name, it will mask all functions with the same name in the base class, regardless of whether their parameters are the same or not.

The following example illustrates this:

#include<iostream>
using namespace std;
//Base class
class Base{
public:
    void func();
    void func(int);
};
void Base::func(){ cout<<"Base::func()"<<endl; }
void Base::func(int a){ cout<<"Base::func(int)"<<endl; }
//Derived class
class Derived: public Base{
public:
    void func(char *);
    void func(bool);
};
void Derived::func(char *str){ cout<<"Derived::func(char *)"<<endl; }
void Derived::func(bool is){ cout<<"Derived::func(bool)"<<endl; }
int main(){
    Derived d;
    d.func("c.biancheng.net");
    d.func(true);
    d.func();  //compile error
    d.func(10);  //compile error
    d.Base::func();
    d.Base::func(100);
    return 0;
}

In this example, func(), func(int) of Base class and func(char *) and func(bool) of Derived class have the same names and different parameter lists. They seem to constitute an overload and can access all functions through object d. in fact, it is not. The func of Derived class obscures the func of Base class, resulting in no matching function in lines 26 and 27, So the call failed.

If there is an overload relationship, the two FuncS of the Base class constitute an overload, while the two FuncS of the Derive class constitute another overload.

Keywords: C++ Back-end

Added by serial on Tue, 08 Feb 2022 04:53:38 +0200