Forums  > Careers  > C++ Interiew Questions  
     
Page 1 of 1
Display using:  

IAmEric
Phorgy Phynance
Banned
Total Posts: 2961
Joined: Oct 2004
 
Posted: 2004-11-23 17:29
Yo,

I know the pros are probably already getting bored with the topic of C++, but I thought it might be a good idea (I know it would help ME a lot and maybe others too) if we gathered up some of the typical C++ questions that get asked in a quant interview. I'm studying my ass off, but I know it'll be easy to trip me up during an interview. I feel I am really close to making it and just need to get through this last hurdle.

Without further ado, let me start off by posting some questions my buddy asked me last night. Please, do not post SOLUTIONS here. Maybe we can start another thread for solutions.



Questions of the day...

1. What is the difference between the classes B, C and D below?

class A
{
/* */
};

class B : public A
{
/* */
};

class C: protected A
{
/* */
};

class D : private A
{
/* */
};

2. What are mutable members of a class?
3. What does the following code achieve?
template<int N>
int func(int mm)
{
return mm*func<N-1>(mm);
}
template<> int func<0>(int mm) { return mm; }

int main(void)
{
int mm=10;
std::cout << fun<3>(mm) << std::endl;
return 0;
}




Nonius
Founding Member
Nonius Unbound
Total Posts: 10185
Joined: Mar 2004
 
Posted: 2004-11-23 17:44

let me take a guess on B vs. D.  B inherits the public interface of A whereas D inherits the private implementation. 

I'm probably wrong, but I never use D.


No more Mr. Nice Guy. Angry

Crassus


Total Posts: 1183
Joined: May 2004
 
Posted: 2004-11-23 18:00

this won't spoil it.

1.  a reference book question.   limits (increasing strictness) the access of D to inherited members and methods.

2. see my original question

3. looks to me to be a power of mm

 


What is best in life? To crush your enemies... to see them driven before you and to hear the lamentation of the women

IAmEric
Phorgy Phynance
Banned
Total Posts: 2961
Joined: Oct 2004
 
Posted: 2004-11-23 18:03
Ok. Screw the "no solutions" request Smiley

Here is my response together with his replies:



1. What is the difference between the classes B, C and D below?

class A
{
/* */
};

class B : public A
{
/* */
};

class C: protected A
{
/* */
};

class D : private A
{
/* */
};


B is public, C is protected, D is private? Smiley

I don't know the real answer Expressionless

Let me guess without cheating...

All three are inheriting from A, but the accessibilities are different. If A has a public method A::f(), then B, C, D all have access to f(), meaning that INSIDE the classes B, C, D, we can do things like

class B
{
/* */
B b;
b.f();
}

class C
{
/* */
C c
c.f();
}

class D
{
/* */
D d
d.f();
}

However, in some external main() program

int main()
{
B b;
C c;
D d;

b.f(); //allowed
c.f(); //not sure if allowed
d.f(); //not allowed

return 0;
}

His reply:
You are partially correct. public inheritance implies that the public members of the base class are visible to the main. Private inheritance implies that even the public members of the base class are only visible to the derived class and not to outside (including its own derived classes). protected is somewhere in between: the derived classes can see the public members of the base, but not to the outside world.

It is actually a little more than that, for protected members of the base class. See Stroustrup.




2. What are mutable members of a class?


No idea Sad

His reply:
mutable is a key word used to identiify members of a class that can be modified even when the object is a const ref or const pointer. That is the mutable members of a const object can be modified. To give an example:

class A
{
public:
mutable int errorCode;
};

int function(const A& aa)
{
aa.errorCode = value;
return 0;
}

is acceptable.




3. What does the following code achieve?
template<int N>
int func(int mm)
{
return mm*func<N-1>(mm);
}
template<> int func<0>(int mm) { return mm; }

int main(void)
{
int mm=10;
std::cout << fun<3>(mm) << std::endl;
return 0;
}


Assuming it compiles (I don't understand why it is a template), I think it will write out:

1000

It seems to be equivalent to

int mmpN = std:: pow(mm,N),

which in this case is,

10^3 = 1000

His reply:
It is an example of template specialization used for a neat trick. Yes, it computes the power function, except that there is no function call! The code the compiler produces would be just mm*mm*mm*1, which is much faster than a function call. The downside is that N must be known at compile time. So good for computing powers (for instance) that are known at compile time. The function looks recursive, but recursive in templates not in function calls.




Crassus


Total Posts: 1183
Joined: May 2004
 
Posted: 2004-11-23 18:16

he did not explicitly state it, but mutable let's you change the state (memeber functions) of an object in const functions


What is best in life? To crush your enemies... to see them driven before you and to hear the lamentation of the women

monkeyA
Mr. Ass to you

Total Posts: 834
Joined: Apr 2004
 
Posted: 2004-11-23 20:25

I've been asked all those questions Smiley

using templates, design a binary search tree class.
then write: add_value, print_sorted and max_depth functions.

using the STL 'vector' class, write a function which, given a vector, returns a vector containing all permutations of that vector

 


If there was problem, Yo I'll solve it

Jim


Total Posts: 99
Joined: Jun 2004
 
Posted: 2004-11-29 23:36

But since func<0>(int mm) returns mm and not 1, you get mm^(N+1) not mm^N.

BTW, templates can be a real pain to debug because the compiler often evalates the whole thing in one step.

 

Previous Thread :: Next Thread 
Page 1 of 1