Interview Question


Country: United States




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

Create class-B as base of class-A with public access. So all the things happening in base class(B) can be visible to derive class(A).

- Rahul September 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Define the class B as base and class A as derived, while deriving the base use public inheritance. public inheritance is "Is A" relation so everything of B is also part of A.
if you changed anything in B then it automatically will get reflected in A.

- SR September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sounds like a typical "observer pattern" scenario. B has to be "observable" and A has to be an "observer" of B's properties.

- Ranjith September 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I see the observer design pattern related to information exchanged in runtime.
This seems to be about attributes of classes before compile time.

- Miguel Oliveira September 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would implement an Abstract class which will contain those changes made to B.
Then, both A and B extend this abstract class.

- Miguel Oliveira September 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This requires some more information like- do u only need to reflect only those two variables? Can class A can change the variables too? etc.
So far as suggested by Ranjith use of observer pattern seems fair.

- Machinist September 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using callbacks. Defining a function pointer in class B and initialize its value in B's constructor to some member function of class A. Then each time, when B is changed, call this function pointer, A will notice the change.

- Gai September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what do you mean by "whatever we do change in Class B"? Relation between the instances or classes? you mean static variables?
I am sorry if this annoys you, I am new here.

- vasudev September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For me, the question is not quite clear, but this is "an" answer to it (what exactly is "whatever that happens to...")

class A {
	void	ReceiveMessage(Message m)
	{
		/* Do Something with it! */
	};
};

class B {
	A* a_instance; // An instance of A
	// Link to an instance of A
	B(A* x) {
		a_instance = x;
	};
	/* DO SOMETHING */
	void DoSomething()
	{
		// something
		Message m;
		// m <- whatever you want to tell A.
		UpdateA(m);
           };
	void	UpdateA(Message m) {
		a_instance->ReceiveMessage(m);
	};
};

A a;
B b(&a);
B.DoSomthing();

- Ehsan September 18, 2013 | 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