C ++中的求职面试问题 [英] job interview question in C++

查看:69
本文介绍了C ++中的求职面试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我工作的公司正在向我的团队请求一位C ++开发人员。我用b $ b准备了以下测试我希望听到一些评论

关于它:它太难/太容易了,如果它太具体了,是不是真的用于测试C ++中的知识等等。

如下:


(1)解释下面的代码,它的作用是什么?什么是输出

ofmain?

(2)为类写一个复制构造函数" Values"。

( 3)任务(2)有什么问题,更改代码使其成为可能的




#include< vector>

#include< iostream>


使用命名空间std;


class BaseValue

{

public:

BaseValue():m_val(0){}

BaseValue(const BaseValue& newValue):m_val(newValue.m_val) {}

virtual~BaseValue(){}

void SetValue(int val){m_val = val; } $ / $
virtual int GetValue()const = 0;

protected:

int m_val;

};


class Value2:public BaseValue

{

public:

Value2():BaseValue() {}

Value2(const Value2& newValue):BaseValue(newValue){}

virtual int GetValue()const {return m_val * 2; }

虚拟~Value2(){}

};


类Value4:public BaseValue

{

public:

Value4():BaseValue(){}

Value4(const Value4& newValue):BaseValue(newValue) {}

virtual int GetValue()const {return m_val * 4; }

虚拟〜值4(){}

};


类值

{

public:

值(const char * sValList)

{

char * tmpList =(char *)malloc (strlen(sValList));

strcpy(tmpList,sValList);

char * token = strtok(tmpList,",");

while(令牌)

{

int type = atoi(token);

if(type == 2)

m_values.push_back(new Value2());

else if(type == 4)

m_values.push_back(new Value4());

token = strtok(NULL,",");

}

}


值(const值和newValues)

{

for(vector< BaseValue *> :: const_iterator it =

newValues.m_values。 begin();

it!= m_values.end(); ++ it)

{

m_values.push_back(new BaseValue(*它));

}

}


~值()

{

for(vector< BaseValue *> :: const_ite rator it = m_values.begin();

it!= m_values.end(); ++ it)

{

删除*它;

}

}

void SetValue(int val,unsigned int pos)

{

//这里需要一些验证

m_values [pos ] - > SetValue(val);

}


void Dump()const

{

for(vector< BaseValue *> :: const_iterator it = m_values.begin();

it!= m_values.end(); ++ it)

{

cout<< (* it) - > GetValue()<<结束;

}

}


私人:

vector< BaseValue *> m_values;

};

int main()

{

值值(2,4, 4,2,4;


values.SetValue(11,0);

values.SetValue(11,2);


values.Dump();


返回0;

}

Hi All,
The company I work for is requiting a C++ developer to my team. I
prepared the following "test" and I would like to hear some comments
about it: is it too hard/easy, if its too specific, is it really
testing ones knowledge in C++, etc.
It is as following:

(1) Explain the following code, what it does? What will be the output
of "main"?
(2) Write a copy constructor for class "Values".
(3) What is the problem with task (2), change the code to make it
possible.

#include <vector>
#include <iostream>

using namespace std;

class BaseValue
{
public:
BaseValue() : m_val(0) {}
BaseValue(const BaseValue &newValue) : m_val(newValue.m_val) {}
virtual ~BaseValue() {}

void SetValue(int val) { m_val = val; }
virtual int GetValue() const = 0;
protected:
int m_val;
};

class Value2 : public BaseValue
{
public:
Value2() : BaseValue() {}
Value2(const Value2 &newValue) : BaseValue(newValue) {}
virtual int GetValue() const { return m_val*2; }
virtual ~Value2() {}
};

class Value4 : public BaseValue
{
public:
Value4() : BaseValue() {}
Value4(const Value4 &newValue) : BaseValue(newValue) {}
virtual int GetValue() const { return m_val*4; }
virtual ~Value4() {}
};

class Values
{
public:
Values(const char *sValList)
{
char *tmpList = (char*)malloc(strlen(sValList));
strcpy(tmpList, sValList);
char *token = strtok(tmpList, ",");
while(token)
{
int type = atoi(token);
if (type == 2)
m_values.push_back(new Value2());
else if (type == 4)
m_values.push_back(new Value4());
token = strtok(NULL, ",");
}
}

Values(const Values &newValues)
{
for(vector<BaseValue*>::const_iterator it =
newValues.m_values.begin();
it != m_values.end(); ++it)
{
m_values.push_back(new BaseValue(*it));
}
}

~Values()
{
for (vector<BaseValue*>::const_iterator it = m_values.begin();
it != m_values.end(); ++it)
{
delete *it;
}
}

void SetValue(int val, unsigned int pos)
{
// some validations are required here
m_values[pos]->SetValue(val);
}

void Dump() const
{
for (vector<BaseValue*>::const_iterator it = m_values.begin();
it != m_values.end(); ++it)
{
cout << (*it)->GetValue() << endl;
}
}

private:
vector<BaseValue*> m_values;
};
int main()
{
Values values("2,4,4,2,4");

values.SetValue(11,0);
values.SetValue(11,2);

values.Dump();

return 0;
}

推荐答案

yu*****@gmail.com 写道:
大家好,
我工作的公司正在向我的团队请求一位C ++开发人员。我准备了以下测试。我想听一些关于它的评论:它太难/太容易了,如果太具体了,是不是真的用C ++等测试知识。
Hi All,
The company I work for is requiting a C++ developer to my team. I
prepared the following "test" and I would like to hear some comments
about it: is it too hard/easy, if its too specific, is it really
testing ones knowledge in C++, etc.



[snip]


首先,我会从C风格的字符串切换到std :: string。到位

of strtok,你可以使用Boost tokenizer库:

http://boost.org/libs/tokenizer/index.html


如果那不公平的游戏在面试环境中,我会更改程序

做其他事情。


干杯! -M


[snip]

First of all, I''d switch from C-style strings to std::string. In place
of strtok, you could use the Boost tokenizer library:

http://boost.org/libs/tokenizer/index.html

If that''s not fair game in an interview setting, I''d change the program
to do something else.

Cheers! -M




mlimber写道:

mlimber wrote:
yu ***** @ gmail.com 写道:
大家好,
我工作的公司正在请求C ++开发人员我的团队我准备了以下测试。我想听一些关于它的评论:它太难/太容易了,如果太具体了,是不是真的用C ++等测试知识[snip]
<首先,我会从C风格的字符串切换到std :: string。
Hi All,
The company I work for is requiting a C++ developer to my team. I
prepared the following "test" and I would like to hear some comments
about it: is it too hard/easy, if its too specific, is it really
testing ones knowledge in C++, etc. [snip]

First of all, I''d switch from C-style strings to std::string.




的确如此。我希望,在略读代码之后,但在任何严肃的分析之前,这将是受访者的第一反应之一。

代替strtok,你可以使用Boost tokenizer库:

http:// boost.org/libs/tokenizer/index.html




智能指针库也可能有所帮助。


如果OP坚持使用C风格的字符串,内存分配等,

需要包含相应的标题。


哦,和在那里有一个atoi。摆脱它(除非它是一个

的受访者应该发现的东西)。


Gavin Deane



The smart pointers library will probably help too.

If the OP does insist on using C-style strings, memory allocation etc,
the appropriate headers will need to be included.

Oh, and there was an atoi in there to. Get rid of that (unless it''s one
of the things the interviewee is supposed to spot).

Gavin Deane


yu*****@gmail.com 写道:
yu*****@gmail.com wrote:
(3)任务(2)有什么问题,更改代码使其成为可能。


我很好奇 - 它是什么?

值(const char * sValList)
{
char * tmpList = (char *)malloc(strlen(sValList));
strcpy(tmpList,sValList);
char * token = strtok(tmpList,",");
while(token)
{int = type = atoi(token);
if(type == 2)
m_values.push_back(new Value2());
else if(type == 4)
m_values.push_back(new Value4());
token = strtok(NULL,",");
}
}
(3) What is the problem with task (2), change the code to make it
possible.
I''m curious - what is it?
Values(const char *sValList)
{
char *tmpList = (char*)malloc(strlen(sValList));
strcpy(tmpList, sValList);
char *token = strtok(tmpList, ",");
while(token)
{
int type = atoi(token);
if (type == 2)
m_values.push_back(new Value2());
else if (type == 4)
m_values.push_back(new Value4());
token = strtok(NULL, ",");
}
}




这里有一个内存泄漏,这是在这种语言中不使用malloc()的许多优秀的

参数之一。更不用说

无偿缩进......


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。






You''ve got a memory leak here, which is one of many excellent
arguments for not using malloc() in this language. Not to mention
gratuitous indentation...

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.






这篇关于C ++中的求职面试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆