C++中多态的理解

#include<iostream>
using namespace std;
class father
{
	public:
		virtual void show(){cout<<"call father::show()"<<endl;}
		virtual void show(int){cout<<"call father::show(int)"<<endl;}
};
class son:public father
{
	public:
		void show(){cout<<"call son::show()"<<endl;}
};
int main()
{
	father *pf = new father;
	father *ps = new son;
	father f;
	son s;
	pf->show();
	pf->show(1);
	ps->show();
	ps->show(1);
	cout<<"object call"<<endl<<endl<<endl;
	f.show();
	f.show(1);
	s.show();
	//s.show(1);   添加后会出错
}
添加s.show(1)后会出错:classExtend.cpp: 在函数‘int main()’中:
classExtend.cpp:28:10: 错误:对‘son::show(int)’的调用没有匹配的函数
classExtend.cpp:28:10: 附注:备选是:
classExtend.cpp:12:8: 附注:virtual void son::show()
classExtend.cpp:12:8: 附注: 备选需要 0 实参,但提供了 1 个


输出结果:

call father::show()

call father::show(int)
call son::show()
call father::show(int)
object call


call father::show()
call father::show(int)
call son::show()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值