两道笔试题

nwpulipeng 2006-05-10 11:08:28
Which of the following class DOES NOT need a copy constructor?
a.A matrix class in which the actual matrix is allocated dynamically
within the constructor and is deleted within its destructor.
b.A payroll class in which each object is provided with a unique ID.
c.A word class containing a string object and vector object of line
and column location pairs.
d.A library class containing a list of book object.

Which virtual function redeclarations 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. ostream& Base::print(int,ostream&=cout);
ostream& Derived::print(int,ostream&);
d. void Base::eval() const;
void Derived::eval();

...全文
406 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingsun555 2006-05-11
  • 打赏
  • 举报
回复
第一题是问哪个类不需要一个CPOY CTOR,这个意思是不是可以利用编译器提供的COPY CTOR而不用程序员自己定义一个呢?如果这样的话, 我想B选项肯定得要自己手工定义一个COPY CTOR吧,还有A选项也必须自己定义一个COPY CTOR
Lemon_2000 2006-05-10
  • 打赏
  • 举报
回复
b c
逸学堂 2006-05-10
  • 打赏
  • 举报
回复
一:b(一看薪水类,就不能有拷贝构造)
二:b. Base* Base::copy(Base*);
Derived* Derived::copy(Base*);
其他的俺不敢确定。
铖邑 2006-05-10
  • 打赏
  • 举报
回复
b c?
yeahspyme 2006-05-10
  • 打赏
  • 举报
回复
2c的缺省参数有问题?
matrass_zhao 2006-05-10
  • 打赏
  • 举报
回复
ugg说的对,2.b是对的。covariant return type,大家可以查查看。

为什么c不对哪?
逸学堂 2006-05-10
  • 打赏
  • 举报
回复
b. Base* Base::copy(Base*);
Derived* Derived::copy(Base*);
~~~~~
C++的标准中提到过,虚函数的返回值可以是父子类(父类或者子类)。
nwpulipeng 2006-05-10
  • 打赏
  • 举报
回复
本人也认为第二题应该选C
matrass_zhao 2006-05-10
  • 打赏
  • 举报
回复
2. b不对吧,应该是c才对。
sankt 2006-05-10
  • 打赏
  • 举报
回复
1.题目本身有问题.一个空类里面都有默认的拷贝构造函数.
如要选择,B比较合适

2.
a 肯定不是,因为参数不一样.
b 正解.
c 这是友元函数,没有覆盖的说法.因为覆盖对应于成员函数.
d 错误.

证明:
/*
void Base::eval() const;
void Derived::eval();
*/

#include<iostream>
using namespace std;

class Base
{
public:
virtual void eval() const
{
cout<<"eval const"<<endl;
}
};

class Derived : public Base
{
public:
virtual void eval()
{
cout<<"eval "<<endl;
}
};

int main()
{
//Derived d;
//Base& b = d;
Base *b = new Derived();

b->eval();
delete b;

system("pause");
return 0;
}

64,691

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧