Tencent2014实习笔试题 南京

来源:互联网 发布:游戏arpu值数据 编辑:程序博客网 时间:2024/04/28 18:55
#include<iostream>
using namespace std;
class Base
{
public:
virtual int foo(int x)//基类的纯虚函数,派生类中可以重新定义改函数
{
return  x*10;
}
int foo(char x[10])//虚函数foo的重载函数。函数名相同,函数参数不同。
{

return sizeof(x)+10;//本题的关键。sizeof(x)中数组x[10]作为函数的参数时,

//x由数组已经退化成指针了.所以sizeof(x)=4.

//(指针表示地址,在32为系统中,地址占32个二进制位(4个字节)

//所以sizeof(x)==4.
}
};
class Derived :public Base 
{
int foo(int x)//是重写Base中的纯虚函数
{
return x*20;
}
virtual int foo(char x[10])//派生类自定义的纯虚函数  在派生类的子类中才能排上用场。在本体中纯属干扰作用
{
return sizeof(x)+20;
}
};
int main()
{
Derived stDerived;
Base *pstBase=&stDerived;
char x[10];
cout<<sizeof(x)<<endl;
printf("%d\n",pstBase->foo(100)+pstBase->foo(x));
return 0;

}



答案是:2014.

可惜打错了。纯虚函数和sizeof操作符的使用!


0 0
原创粉丝点击