The difference between overloading, overriding and hiding of class member functions

1. Overloading means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (maybe the number of parameters is different, the parameter type may be different, or both). Implementation principle: The compiler modifies the names of the functions with the same name according to the different parameter lists of the functions, and then these functions with the same name become different functions (at least for the compiler). For example, there are two functions with the same name: function func(p:integer):integer; and function func(p:string):integer;. Then the function names modified by the compiler may be like this: int_func, str_func. The calls to these two functions have been determined between the compilers and are static. That is, their addresses are bound at compile time (early binding), so overloading has nothing to do with polymorphism!
Features :
(1) The same scope (in the same class);
(2) The function names are the same;
(3) The parameters are different;
(4) The virtual keyword is optional.

class A{
public:
void fun(int i);
void fun(double i);
void fun(int i, double j);
void fun(double i, int j);
int fun(int i); //error, Not overloaded
};
the first four are overloaded functions, the last one is not

2.  Overriding (overriding) is to designate the derived class function to override the base class function (referring to the method of the subclass redefining the virtual function of the parent class.). Implementation principle: Really related to polymorphism. When the subclass redefines the virtual function of the superclass, the superclass pointer dynamically calls the function belonging to the subclass according to the different subclass pointers assigned to it. Such a function call cannot be determined during compilation (calling The address of the virtual function of the subclass cannot be given). Therefore, such function addresses are bound at runtime (late bound).
Features :
(1) Different scopes (in the derived class and the base class respectively);
(2) The function names are the same;
(3) The parameters are the same;
(4) The base class function must have the virtual keyword.

#include<iostream>
using namespace std;
class A
{
public:
virtual void test(int i)
{
cout<<”This is A::test()”<<i<<endl;
}
};
class B:public A
{
public:
virtual void test(char s)
{
cout<<”This is B:test()”<<s<<endl;
}
};
int main()
{
A a;
B b;
A * p=&a;
p->test(3);
p=&b;
p->test(‘c’);
return 0;
}

3.  Hidden means that the function of the derived class shields the function of the base class with the same name. The rules are as follows:
(1) 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, with or without the virtual keyword, the functions of the base class will be hidden (not to be confused with overloading).
(2) If the function of the derived class has the same name and the same parameters as the function of the base class, but the function of the base class does not have the virtual keyword. At this point, the functions of the base class are hidden (not to be confused with overriding)

#include <iostream>
using namespace std;
class A
{
public:
void foo()
{
printf("1\n");
}
};
class B :public A
{
public:
void foo()
{
printf("3\n");
}
};
int main(void)
{
A a;
A *p_A = &a;
p_A->foo();//执行成员函数

B b;
B *p_B = &b;
p_B->foo();//执行成员函数

p_A = &b;   //p_A指针执行子类,可以访问被隐藏的基类成员函数
p_A->foo();
system("pause");
return 0;

}
output is 1 3 1

隐藏和重写,重载的区别:
#include<iostream>
using namespace std;
class A{
public:
void test1(int i, int j)
{
cout << "A::test1() : " << i << " " << j << endl;
}
void test1(int i)
{
cout << "A::test1() : " << i << endl; //重载
}
virtual void test2(int i)
{
cout << "A::test2(int) : " << i << endl;
}
};
class B : public A
{
public:
//隐藏
void test1(double i)
{
cout << "B::test1() : " << i << endl;
}
//重写
void test2(int i)
{
cout << "B::test2(int) : " << i << endl;
}
//隐藏
void test2(double i)
{
cout << "B::test2(double) : " << i << endl;
}
};
int main()
{
B b;
pa = &b;
pb = &b;
pa- >test2(3); //Rewrite, polymorphism, call B's function
b.test2(10); //Hide, call B's function
pb->test2(20); //Hide, call B's function
return 0;
}

The output is:
B::test2(int) : 3
B::test2(int) : 10
B::test2(int) : 20

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325758482&siteId=291194637