Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
2
of 4 vote

Thanks. Following above suggestion..tried some code. For simplicity using array, but it can always be extended to user defined collection classes.

#include <iostream>

using namespace std;

class BaseDataType
{
public:
virtual void print() = 0;
};

class IntDataType: public BaseDataType
{
int m_x;
public:
void print()
{
cout << "IntDataType" << endl;
}
};

class FloatDataType: public BaseDataType
{
float m_x;
public:
void print()
{
cout << "FloatDataType" << endl;
}
};

class DoubleDataType: public BaseDataType
{
double m_x;
public:
void print()
{
cout << "DoubleDataType" << endl;
}
};

int main()
{
BaseDataType* a[3];
a[0] = new IntDataType;
a[1] = new FloatDataType;
a[2] = new DoubleDataType;

for(int i = 0; i < 3; ++i)
{
a[i]->print();
}
return 0;
}

- learner123 April 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So basically, this was homework? Huh?

- Anonymous April 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Yes you can.
One simple way is to create a class say A, extend with with different subclasses(class B for int, class C for float etc),
Then use an array of pointers to base class (A), and use it to store objects of derived classes.

- puneet.sohi April 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Basically the Object class of Java...

- Anonymous April 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm not a Java expert, but I think you're talking about the Object class that is the parent for any other class?

- puneet.sohi April 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes.

- Anonymous April 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nopes...just practicing for interviews :)

- learner123 April 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can use the void* type as the generic type. At the end of the day, all you need to store from the object is a "pointer" to it.

vector<void*> arr;
	int *a = new int;
	*a = 1;
	double* b = new double;
	*b = 2.0;
	Type* t = new Type();
	arr.push_back(a);
	arr.push_back(b);
	arr.push_back(t);
	Type* tt = (Type*) arr[2];

however, I would not suggest this. Since (void*) does not carry any information about type. The better approach, is (as suggested above) use some inheritance. As it is in C#, Java, etc. (Object class).

- Ehsan June 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use of template..
#include <iostream.h>

template <class T>
class collection {
private:
T n;
public :
void store_data(){
cin>> n;
}
void display_data(){
cout << " given data "<< n <<"\n";
}
};

main(void){
collection <int> obj1;
collection <float> obj2;
cout <<" enter integers\n";
obj1.store_data();
cout<< " enter floating no \n";
obj2.store_data();
obj1.display_data();
obj2.display_data();

}

- sumit June 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using derivation, templates, or void.

- ntf January 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

template <typename... _Mixin>
class Collection : public _Mixin...
{
template< typename... _Args>
Collection(_Args&&... arg) :_Mixin(std::forward<_Args>(arg))...{};
};

- Prabhat Kumar Srivastava April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

template <typename... _Mixin>
class Collection : public _Mixin...
{
template< typename... _Args>
Collection(_Args&&... arg) :_Mixin(std::forward<_Args>(arg))...{};
};

- Prabhat Kumar Srivastava April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

should be smth like this:

class Base {                 // base class for wrapper classes
    public:
      virtual ~Base() {}
};

template <class T>           // generic wrapper class
class Wrapper : public Base {  
    T object;
public:
    Wrapper(const T& obj) : object(obj) {
	}
	Wrapper(T&& obj) : object(std::move(obj)) {
	}
	
    Wrapper() {}
    operator T() { return object; }
};

class Collection {             
    std::vector < std::unique_ptr< Base >> v;
public:

	Collection() {}
	
	template < class T >
	void insert(const T& t) {
		v.push_back(std::unique_ptr< Base >(new Wrapper<T>(t)));
	}
	
	template < class T >
	void insert(T&& t) {
		using T2 = typename std::remove_reference<T>::type;
		v.emplace_back(new Wrapper< T2 >(std::forward<T>(t)));
	}
    
	// try to convert an i-th object to type 'T'
	template < class T >
	bool assign(T& t, unsigned idx) {
		if(idx >= v.size())
			return false;
		Wrapper<T>*  wp = dynamic_cast<Wrapper<T>*>(v[idx].get());
		if (wp == 0)
			return false;
		t = *wp;
		return true;
	}
};

struct YOYO {
};

int main(int argc, char* argv[]) {

	Collection c;
	YOYO yoyo;
	int aaa = 2;
	std::string xxx("123123");
	c.insert(12);
	c.insert(yoyo);
	c.insert(YOYO());
	c.insert(std::string("123123"));
	c.insert(xxx);
	
	int aaa;
	if(c.assign(aaa, 0)) {
		std::cerr << "got x value = " << aaa << "\n";
	}
	if(c.assign(yoyo, 2)) {
		std::cerr << "got YOYO value\n";
	}
	return 0;
}

- pavel.em October 01, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More