Y
yuvalif
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;
}
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;
}