Distinguish Overloading, Overriding, and Hiding

There are three very confusing concepts in object-oriented (OO) world: overloading (Overloading), rewrite (Overriding), hidden (Hiding).

1. Overload

Overload refers to the different functions of the same scope with the same function name, but different type or number of arguments. Overloading in C already existed, as we are familiar with the abs function as follows:

double abs(double); 
int abs(int); 
abs(1);         // call abs(int); 
abs(1.0);       // call abs(double);

Overloaded functions that have the same name, different parameters of some functions in a class space. Such as the following class Maxer the Max function:

class Maxer {
public:

    void Max(int a, int b);

    void Max(double a, double b);

    void Max(double a, double b, double c);

    ...// other code
};

However, if you inherit a base class with a derived class newMaxer Maxer:

class newMaxer : public Maxer {
public:

    void Max(int a, double b);

    ...// other code
};

Overload sibling derived class newMaxer not function in the base class Maxer Max Max in function because they belong to different scopes. Therefore, when write the following code, the compiler will be given:

newMaxer newMax; 
newMax.Max(1, 3);     // 编译报错

This is because not found Max (int, int) in the derived class scope function is defined in the base class Maxer the derived class Max Max (int, double) to cover up, so it was a "parameter does not match "error. If they want four brothers reload configuration, Max function declaration base class needs to be introduced into the derived class scope, as follows:

class newMaxer : public Maxer {
public:

    using Maxer::Max;
    void Max(int a, double b);

    ...// other code
};

2. Rewrite

Rewriting means to re-implement the base class virtual function in a derived class, i.e., the function name and the parameters are the same, but not the same function to achieve the body. We are very familiar with the rewrite is an operation that is closely related to the realization of virtual functions. This involves two key elements: and the derived class virtual function, as follows:

class Student {
public:

    Student(){}

    ~Student(){}

    virtual void Show() {
        std::cout<<"Student..."<<std::endl;
    }
    
};

class CollegeStudent : public Student {
public:

    CollegeStudent(){}

    ~CollegeStudent(){}

    virtual void Show() {
        std::cout<<"CollegeStudent..."<<std::endl;
    }
}

But there are several points to note rewrite:

(1) function rewrites the access level (public, private, protected) has nothing to do.

class CollegeStudent : public Student {
public:

    CollegeStudent(){}

    ~CollegeStudent(){}
    
private:

    virtual void Show() {
        std::cout<<"CollegeStudent..."<<std::endl;
    }
}

Show above the derived class with access to different levels of the base class, but managed to achieve a special customized for the function.

(2) const may cause a virtual member function of rewrite failure.

Const member functions and member functions in the general function signature is different, its constant property is part of the function signature.

class CollegeStudent : public Student {
public:

    CollegeStudent(){}

    ~CollegeStudent(){}
    
    virtual void Show()const {
        std::cout<<"CollegeStudent..."<<std::endl;
    }
}

Because different function signature, so the derived class's Show function does not override the base class Show function.

(3) a function of rewriting the original function must have the same return type.

Because the return type is not a part of the function signature function, so if a derived class overrides a function of the corresponding base type, then they must have the same return type. If the return value is different compiler throws an error warning "override the virtual function return types are different", as follows:

class CollegeStudent : public Student {
public:

    CollegeStudent(){}

    ~CollegeStudent(){}
    
    virtual bool Show() {
        std::cout<<"CollegeStudent..."<<std::endl;
    }
}

The rule exists for an exception, it referred to as "covariant return type." Covariant return value must be a pointer or a subclass or a parent class reference, as follows:

class CollegeStudent : public Student {
public:

    CollegeStudent(){}

    ~CollegeStudent(){}
    
    CollegeStudent& Show() {
        std::cout<<"CollegeStudent..."<<std::endl;
    }
}

Note that, if there is a return value, the return value must be a reference or a pointer to a subclass or a parent class, if the return value of the parent class is referenced, subclass return value is a reference; if the parent class of the return value is a pointer, The return value is a pointer subclass. Otherwise, the compiler will not pass.

3. Hide

Hide is raw non-virtual functions assigned class shielding function in the base class with the same name. So it's two important factor is derived and non-virtual functions.

When calling a class member function, the compiler will find step by step along class inheritance chain up the definition of the function, if found to stop. If a derived class has a base class and a function of the same name, it is lower due to the derived class in the inheritance chain, the compiler will eventually select the derived class. In this way, the same name as the base class member function will be shielded hidden function to find the compiler will not reach the base class.

Or the use of the foregoing newMaxer class Max function to illustrate the problem, as shown below:

class Maxer {
public:

    void Max(int a, int b);

    void Max(double a, double b);

    void Max(double a, double b, double c);

    ...// other code
};

class newMaxer : public Maxer {
public:

    bool Max(int a, int b);
    void Max(int a, double b);

    ...// other code
};

When the compiler to find Max function in the inheritance chain, the derived class Max function prevents it from looking upward, to hide the base class Max.

4. Summary

Finally, it sets out a simple form so that we can all three of these have a clear understanding.

relationship Scope Whether virtual Function name Parameter Type Return Type
Overload the same Dispensable the same different It may be the same or different
Rewrite (cover) different Have the same the same Same (covariant)
Hide (redefined) different Dispensable the same It may be the same or different It may be the same or different

Homepage:

www.codeapes.cn

Guess you like

Origin www.cnblogs.com/codeapes666/p/12093774.html
Recommended