Bloomberg LP Interview Question
Software Engineer / DevelopersMethod 1:
---------
include <vector>
....
std::vector<class_name> object_name(500, class_name(value));
....
Method 2:
---------
class_name object_name[500] = {class_name(value), class_name(value), .......}
.
Its simple, we first need to ask a question:
Whats integer vaue should I pass to constructor?
If the ans is support 10,
Objhect *obj = new Object[100] (10);
If the argument has to be decided at run time, then you cannot create an array of sucb a class directly.
create a vector of that Obj* and keep pushing obj* into it.
#include <iostream>
class Sample
{
private:
int pV;
public:
Sample(int i)
{
pV = i;
}
};
int main()
{
Sample ob[3] = { 0, 0, 0};
Sample *ptrSample = new Sample[100](5);
return 0;
}
There are two to create the objects:
TestClass objects[500] = {1,2,3,...,500};
but this is not possible if the number is that high.. so there is another way to create the object...
TestClass* objects[500];
Now you have 500 pointer to the TestClass objects. now initialize the pointer like -
while (i<500) {
objects[i] = new TestClass(0);
i++;
}
You can initialize object with the desired value or can change the value later.
Now with this approach you have to take care while de-allocating the memory.
If we provide one argument constructor, we "HAVE TO" provide the default constructor too.
Other wise the code wont compile at all.
To declare array of objects, you can do this, Assume 'Alfa' is class name
1# Alfa *a[10]; // array of 10 pointer to Alfa class
for ( i=0; i<10; i++)
a[i] = new Alfa(i); //call what ever the constructor you want.
2# Alfa a[10]; //will give 10 objects on stack.
you can do it using double pointers :
TestClass **ptr;
ptr = new TestClass *[500];
for each i
ptr[i] = new TestClass("value");
I was asked this yesterday.
One trick to remember (and I don't see it here): Make constructor that takes int argument explicit.
class TestClass {
explicit TestClass(int size) { _ptr = new TestClass[size]; }
get() const { return _ptr; }
};
Then, do this:
TestClass a(500);
But what I didnt like abt my solution is the new guy _ptr.
Why cannt we use malloc , malloc doesnt invoke a constructor .
class Array{
Array(int);
}
Array *a;
a = (Array *)malloc(sizeof(Array) * 500);
firewithin and kk's solutions are right but do not scale to a large number such as 500. A better way is to allocate just one large raw chunk of memory and use placement new to initialize all the objects. The following program initializes 500 objects using integer values 1 to 500.
struct A { A(int i) {} };
int main(void) {
const int N = 500;
A * ptr = static_cast<A *>(::operator new (sizeof(A) * N));
for(int i = 0;i < N; ++i)
{
new (ptr + i) A(i); // placement new
}
// Don't forget to call destructors at the end.
for(int i = 0;i < N; ++i)
{
(ptr + i)->~A();
}
// free memory.
::operator delete (ptr);
}
Following is working fine, tested in MSVC++ 6.0, debugged and noticed different addresses in the objects array.
- Rajendra Kumar Uppal February 03, 2010