Interview Question






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

change "class Derived; friend Derived;" to "friend class Derived;". will compile

- Jofen October 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Right! Checked via MSVC

- creation May 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

code will not compile,
though we are creating base class object (Base b) inside derived class, we cant access the private member 'a' of the base class inside the derived class.
'b.a' inside the derived class is the problem in code. 'a' is private member of base class.

- anonymous September 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But the dervied class is friend of base class and friend can access private members. It got compiled.

- Maankutti September 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But the "Derived" inside of "Base" is different from "Derived" out side of "Base". The former's full scope is Base::Derived and the latter's full schope is ::Derived

::Derived is not a friend of Base.

- anonymous September 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I really didnt understand ur answer. They have used forward declaration dervied in base and made dervied has friend of base .Since friend can access private members, "a" is accessed using base object directly in dervied.

- Mannkutti September 14, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the code will not and should not compile due to:

1. a being private and being accessed by privately derived b
2. friend declaration should error as class key is not there

- Anonymous September 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In C++ u don't have to explicitly specify class or struct keyword unlike C ... It wont give error because of u havent written "class" keyword. It would give error saying that it cannot access the private members of Base.

I guess the reason is different. Not sure exactly what does 'friend Derived' means rather that 'friend class Derived'. Yes the friend class Derived would definitely solve the problem. But your explanation is wrong .

- Hiten Parmar October 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it will not compile since base object b tries to access its private member 'a'.

- Anonymous September 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

abey nahi ataa hai to chup kar chutiye

- KM November 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be compiled after the following changes (I tried it):
1. change "class Derived; friend Derived;" to "friend class Derived;"
2. move "friend class Derived;" to the first line of class Base

- Anonymous October 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect!

- kedar September 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here class derived is declared within base which means derived is a nested class within base.
The class defined outside is a different class with the same name(of course to confuse). I hope the code given below will help you
class Base{
int a;
public:
int b;
Base(){a = 10; b = 11;}
virtual ~Base(){};
class Derived;
friend Derived;
};
class Base::Derived{
public:
//Derived(){cout<<b<<endl;}
void p(Base b){cout<<b.a<<endl;};
virtual ~Derived(){};
};



int main(){
Base b;
Base::Derived d;
d.p(b);
return 0;
}

- Ashish October 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As per the standards, there should be a elaborated type (containing class or struct keywords)in the friend declaration.
That's why "friend Derived" will not work.
The solution given by Ashish is incorrect (please try it on g++). Also you changed the inheritance to containment which will change the original problem definition.

- Mustafa October 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

chutiye kabhi to kahe kar ki== ata hai

- praya November 01, 2011 | 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