重载重写重定义

函数重载
必须在同一个类中进行
子类无法重载父类的函数,父类同名函数将被名称覆盖
重载是在编译期间根据参数类型和个数决定函数调用
函数重写
必须发生于父类与子类之间
并且父类与子类中的函数必须有完全相同的原型
使用virtual声明之后能够产生多态(如果不使用virtual,那叫重定义)
多态是在运行期间根据具体对象的类型决定函数调用
即 重写分为两分类:虚函数重写发生多态,非虚函数重写发生重定义

#include <iostream>
using namespace std;
class Parent{
public:
	void print(){
		cout << "this is parent Print()--" << endl;
	}
	void print(int i,int j){
		cout << "this is parent print(int i,int j)--" << endl;
	}
	virtual void func(){
		cout << "this is parent func()--" << endl;
	}
	virtual void func(int i){
		cout << "this is parent func(int i)----" << endl;
	}
	virtual void func(int i, int j){
		cout << "this is parent func(int i,int j)---" << endl;
	}
	virtual void func(int i, int j, int mm ,int m ){
		cout << "this is Parent func(int i,int j,int mm int m)---" << endl;
	}
protected:
private:
};
class Child :public Parent{
public:
	void print(){
		cout << "this is child Print(int i,int j)--" << endl;
	}
	void print(int i){
		cout << "this is child Print(int i)--" << endl;
	}
	virtual void func(int i, int j){
		cout << "this is child func(int i,int j)---" << endl;
	}
	virtual void func(int i,int j,int m){
		cout << "this is child func(int i,int j,int m)---" << endl;
	}
};
int main()
{
	Child c;
	c.print(1);
	c.Parent::print(1, 2);
	//c.func(); //子类无法重载父类的函数
	c.Parent::func(); //只能通过作用域调用
	c.func(2, 3);
	c.print();
	//c.func(1, 2, 3, 4);error
	system("pause");
	return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值