I got phone interviewed with Bloomberg.
1)diff bet class and structure
2)syntax of copy constructor
3)why we need copy constructor?
4)why we need overloaded = operator?
5)signature of overloaded = operator for a class?
e.g. Myclass & operator =(const Myclass &)
6)why & used in return type of = operator ?
7)suppose :
class Myclass{
public:
Myclass(int a){m=a;};
private :
int m;
};
a)how will u create an array of objects of Myclass on stack?
b)how will u crete an array of objects on heap?
8)what precautions should be taken to write a class's destructure?
9)what will u do to prevent others from creating more than one instances of ur class?
10)how will u make singleton thread safe?
10.1) what problem may occure with singleton at run time?
11)what is diff between "operator new" and "new" ?
12)what happens if new fails to allocate memory?
7a: Myclass myclass[10];
7b:
Myclass *myclass = new MyClass[10];
int i;
for (i = 0; i < 10; i++)
myclass[i] = new MyClass();
Cookie on May 17, 2009 |Edit | Edit
@John
Myclass myclass[10]; will not work.This statement would give a compile time error as the class lacks a default constructor.
To construct the array on stack it would be done like below
int i[] = {1,2,3,4,5,6,7,8,9,10};
MyClass myClass[10] = {1,2,3,4,5,6,7,8,9,10};
Anonymous on June 10, 2009 |Edit | Edit
Correct:)
Hi John,
Your 7a is already corrected by Cookie
Your '7b' is also incorrect.
---> " Myclass *myclass = new MyClass[10]; " here new will allocate memory and creates object as well, by calling its default constructor. During compilation, You will encounter 'No appropriate default constructor available' error.
Rest of the code is also error.
The correct answer to 7b is:
MyClass* mc[10];
for (int i=0; i < 10; i++)
mc[i] = new MyClass(i); <--- it will call user-defined constructor here.
Jey on June 10, 2009 |Edit | Edit
Is it can be some thing like this for stack?
MyClass objMyClass[] = {MyClass(1), MyClass(2)};
bbs59 on June 12, 2009 |Edit | Edit
Jey is right,
the way like MyClass myClass[10] = {1,2,3,4,5,6,7,8,9,10} is not correct.
you must do it like
MyClass myClass[10] = {MyClass(1),MyClass(2),...,MyClass(10)}
Reply to Comment
bbs59 on June 15, 2009 |Edit | Edit
just remember aggregation initialization: Type MyArray[size] = {Elem1, Elem2,...};
e.x, int m[] = {1,3,5,7};
So,
On Stack: MyClass myArray[] = {MyClass(1), MyClass(2),...,MyClass(10)};
On Heap: MyClass* p[] = {new MyClass(1), new MyClass(2),..., new MyClass(10)};
Reply to Comment
contactjey on June 15, 2009 |Edit | Edit
Jey:
The correct answer for allocating the objects on heap would be using placement new operator i guess.
create a chunk of memory (char*) with normal new[]
offset it to point to account object using placement new
note placement new wont allocate any memory but calls ctor
in this way u dont need a array of pointers
deletion could be little tricky
first explicitly call the dtor for each object and delete the chuck as a whole
Reply to Comment
wihenao on August 13, 2009 |Edit | Edit
To point #8 I would add. Make it virtual if any other function is virtual
Wednesday, August 19, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment