Interview Question






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

<pre lang="c++" line="1" title="CodeMonkey88943" class="run-this">// There would be problems with interface (public methods)
// Suppose, we have following classes:

class Base {
int _a;
public:
Base(int aa = 0) : _a(aa) {}
int geta() { return _a; }
};

class Derived : Base {
public:
Derived(int aa = 0) : Base(aa) {}
};


void SomeFunc(Base* ob) {
// so here, if ob is Derived and if we have private inheritance, how
// geta() would be called? In derived it has become "private".
// I.e. interface has become inaccessable
cout << ob->geta() << endl;
}

int main()
{
Base* d = new Derived(44); // that`s why compiler finds an error here
Base* b = new Base(55);

SomeFunc(d);
SomeFunc(b);

return 0;
}


</pre><pre title="CodeMonkey88943" input="yes">
</pre>

- Anonymous December 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

stackoverflow.com/questions/3674876/why-is-this-not-allowed-in-c

- blueskin.neo December 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

parashift.com/c++-faq-lite/private-inheritance.html

- Noha December 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you make the constructor of base class private, as a result, you can't new it.

- Anonymous March 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The problem occurs at the upcasting part. If we run
der *b = new der;
, then the constructor of the base class can still be called. It means we have no problem to run "new der".

However, when we try upcast a pointer of der type to one of base type, something is wrong, even though I don't exactly know what is wrong. Who can explain a little bit.

- Anonymous March 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

base class pointer in this case points to an object of the derived class which has some additional members to that of base class (of course, not accessible by base class pointer as it's out of its scope).

- Geet April 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can't create an object if the ctor is private member.

- Naren May 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

error C2243: 'type cast' : conversion from 'derived *' to 'base *' exists, but is inaccessible

- Anonymous February 18, 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