程序题
1.What's the output of the following code?(MS_2013_Intern_2)
class A
{
public:
virtual void f()
{
cout<<"A::f()"<<endl;
}
void f() const
{
cout<<"A::f() const"<<endl;
}
};
class B: public A
{
public:
void f()
{
cout<<"B::f()"<<endl;
}
void f() const
{
cout<<"B::f() const"<<endl;
}
};
void g(const A* a)
{
a->f();
}
int main()
{
A* a = new B();
a->f();
g(a);
delete a ;
}
答案: B::f() A::f() const
2.What's the output of the following code?(MS_2013_Intern_8)
1 #include <iostream> 2 3 class A{ 4 public: 5 long a; 6 }; 7 8 class B : public A 9 { 10 public: 11 long b; 12 }; 13 14 void seta(A* data, int idx) 15 { 16 data[idx].a = 2; 17 } 18 19 int _tmain(int argc, _TCHAR *argv[]) 20 { 21 B data[4]; 22 23 for(int i=0; i<4; ++i) 24 { 25 data[i].a = 1; 26 data[i].b = 1; 27 seta(data, i); 28 } 29 30 for(int i=0; i<4; ++i) 31 { 32 std::cout<<data[i].a<<data[i].b; 33 } 34 35 return 0; 36 }
答案:22221111
浙公网安备 33010602011771号