Overloading, rewriting, and redefining-different processing methods for three functions with the same name

The difference between overloading, rewriting and redefining:

Insert picture description here

Overload

Functions with the same name in a class are handled by different functions due to the number/type of function parameters (different parameter lists ). This situation is called 重载.

class A
{
    
    
	public:
		void show(){
    
    
			cout << "show()" << endl;
		}
		void show(int x){
    
    
			cout << "show(int x):"<< x << endl;
		}
};
int main(){
    
    
	A a;
	a.show();
	a.show(3);
}

For overloaded functions, which function you want to call depends on the parameters you give. The results are as follows:
Insert picture description here

Redefine (hide)
Situation One
  • If functions with the same name are in different classes , they are no longer overloaded functions,
  • If their formal parameter lists are different , the functions of the same name in the base class will be hidden (shielded)
class A
{
    
    
public:
	void show(){
    
    
		cout << "A--show()" << endl;
	}
};
class B : public A
{
    
    
public:
	void show(int x){
    
    
		cout << "B--show()" << x << endl;
	}
};
int main(){
    
    
	B a;
	//a.show();
	//此时基类中的show()和子类中的函数同名而参数不同,
	//满足隐藏的条件,子类从基类继承而来的没有参数的
	//show()已经被隐藏了,无法调用
	a.A::show();
	a.show(5);
	system("pause");
	return 0;
}

Insert picture description here

Situation two
  • The function with the same name is in different classes, the parameters are the same , and there is no virtual keyword declaration before the function with the same name in the base class, then the function with the same name in the base class will still be hidden.
class A
{
    
    
	public:
		/* virtual */void show(){
    
    
			cout << "A--show()" << endl;
		}
};
class B : public A
{
    
    
	public:
		void show(){
    
    
			cout << "B--show()" << endl;
		}
};
int main(){
    
    
	B a;
	a.show();
	system("pause");
	return 0;
}

Insert picture description here

class A
{
    
    
	public:
		/* virtual */void show(){
    
    
			cout << "A--show()" << endl;
		}
};
class B : public A
{
    
    
	public:
};
int main(){
    
    
	B a;
	a.show();
	system("pause");
	return 0;
}

Insert picture description here
Compare the two programs above:

  • When the subclass has a parameter with the same name as the base class, and virtualit is hidden without modification
  • When the subclass has a function with the same function name as the parent class, when the subclass object calls the function, it will first find the implementation in the subclass. If there is an implementation in the subclass, the subclass function is executed. If the subclass The function is not implemented, and then call the parent function.
Rewrite (overwrite)
  • If the function with the same name is in different classes, the parameters are the same, and the function with the same name in the base class is declared with the virtual keyword , then the function with the same name in the base class will be rewritten (overridden). It has the following two characteristics:

① When an object calls a function with the same name in a subclass, its performance is the same as when it is hidden, and the program result remains unchanged (but in fact, it is different from hiding);

class A
{
    
    
public:
	virtual void show(){
    
    
		cout << "A--show()" << endl;
	}
};
class B : public A
{
    
    
public:
	void show(){
    
    
		cout << "B--show()" << endl;
	}
};
int main(){
    
    
	A a;
	B b;
	a.show();
	b.show();
	system("pause");
	return 0;
}

Insert picture description here

② When calling a function with the same name in a subclass through a pointer or reference, because the type of the pointer (reference) needs to be distinguished from the type of the object pointed to by the pointer (reference), the performance at this time is different from hiding-through the base class pointer Pointing to different objects, the pointer will call the corresponding function according to the type of the object.

class A
{
    
    
public:
	virtual void show(){
    
    
		cout << "A--show()" << endl;
	}
};
class B : public A
{
    
    
public:
	void show(){
    
    
		cout << "B--show()" << endl;
	}
};
void fun(A* p){
    
    
	p->show();
}
int main(){
    
    
	A a;
	B b;
	fun(&a);
	fun(&b);
	system("pause");
	return 0;
}

Insert picture description here
A class containing virtual functions has at least one virtual function table pointer, because the address of the virtual function must be placed in the virtual function table, which is also called virtual table for short.

operation result:
Insert picture description here

Common test interview questions related to virtual functions

Guess you like

Origin blog.csdn.net/weixin_44826356/article/details/108029397