C++笔试题深度分析 第四波 上

1. 下面代码是否有错?如果有错,错在哪里?
struct Test
{
Test() { }
Test(int i) { }
void func() { }
};
int main()
{
Test t1(1);
Test t2();
t1.func();
t2.func();
}


答案:

第一: int main  没有 返回值

第二: Test t2();不能这样使用,应该是 Test t2;  反应不过来是受Test t1(1)的影响; Test t2();这是个函数;

 

 

2. 下面的代码输出什么?为什么?
class Test
{
int m_i;
int m_j;
public:
Test(int v) : m_j(v), m_i(m_j)
{
}
int getI()
{
return m_i;
}
int getJ()
{
return m_j;
}
};
int main()
{
Test t1(1);
Test t2(2);
cout<<t1.getI()<<" "<<t1.getJ()<<endl;
cout<<t2.getI()<<" "<<t2.getJ()<<endl;
}


答案 : 

4233198 1
4233104 2

考察初始化顺序;初始化顺序不影响成员变量的赋值顺序,成员变量的赋值只与  成员变量的声明顺序有关系。

 

所以这里先初始化m_i  用m_j初始化;m_j是一个未知的变量;然后初始化m_j;用1和2初始化m_j;

 

3. 下面的代码输出什么?为什么?
class Test
{
int m_i;
int m_j;
public:
Test()
{
cout<<"Test()"<<endl;
}
Test(int v)
{
cout<<"Test(int v)"<<endl;
}
~Test()
{
cout<<"~Test()"<<endl;
}
};
Test Play(Test t)
{
return t;
}
int main()
{
Test t = Play(5);
}


答案:

Test(int v)
~Test()
~Test()

解释:

Play(5)这里编译器会自动转换匹配到 Play(Test(5))

所以这里输出Test(int v)
return t; t 赋值给临时对象 temp 然后析构;

然后Test t 的 t 析构;

这里我也不清楚为什么不是调用3次析构?

 

4. Which virtual function re-declarations of the Derived class are correct?
A. Base* Base::copy(Base*);
Base* Derived::copy(Derived*);
B. Base* Base::copy(Base*);
Derived* Derived::copy(Base*);
C. int Base::count();
int Derived::count();
D. void Base::func(Base*) const;
void Derived::func(Base*);

 

答案:选C  函数要严格一致

 

5. 下面程序输出什么?为什么?
class Base
{
public:
virtual void func()
{
cout<<"Base::func()"<<endl;
}
};
class Child : public Base
{
public:
void func()
{
cout<<"Child::func()"<<endl;
}
};
int main()
{
Base* pb = new Base();
pb->func();
Child* pc = (Child*)pb;
pc->func();
delete pc;
pb = new Child();
pb->func();
pc = (Child*)pb;
pc->func();
}


答案:

Base::func()
Base::func()
Child::func()
Child::func()

考虑虚函数表的使用,虚函数知识点

注意void为虚函数

 

6. A C++ developer wants to handle a static _cast<char*>() operation for the
String class shown below. Which of the following options are valid declarations
that will accomplish this task?
class String
{
public:
// …
// declaration goes here
};
A. char* operator char* ();
B. operation char*();
C. char* operator ();
D. char* operator String ();

答案:选C  类型转换函数的考察

 

7. 以下两种情况:
(1) new 一个 10 个元素的数组
(2) 分 10 次 new 一个整型变量
哪个占用的空间更大些?
A. 1
B. 2
C. 一样多
D. 无法确定

 

答案: 选B  每次一个数组除了数组空间,在数组首地址前面还会有一段内存用来保存这个数组的相关信息。

 

8. 下面程序输出什么?
int main()
{
int v[2][10] =
{
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
};
int (*a)[8] = (int(*)[8])v;
cout<<**a<<endl;
cout<<**(a + 1)<<endl;
cout<<*(*a + 1)<<endl;
cout<<*(a[0] + 1)<<endl;
cout<<*a[1]<<endl;
}

答案:

1
9
2
2
9

考察数组指针 和 二维数组在内存当中是以一位数组的方式存在

 

9. 下面的程序输出什么?为什么?
class Base
{
public:
int a;
Base() { a = 1; }
void println() { cout<<a<<endl;; }
};
class Child : public Base
{
public:
int a;
Child() { a = 2; }
};
int main()
{
Child c;
c.println();
cout<<c.a<<endl;
}

 

答案:

1,

2

c.println()调用父类的函数,父类里面定义的函数不可能看到子类当中的成员变量,

所以肯定是输出父类的a;

cout<<c.a;子类与父类有同名成员变量,子类的覆盖父类的。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值