473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Some interview questions

All, I had an interview today and I couldn't answer these questions.
Any good answers?
Why does a copy constructor param need to be const?
Ever need a virtual constructor?
Major difference between public and private inheritance (other than the
obvious one)?
Thanks,
Sashi

Jul 23 '05 #1
12 1435
* Sashi:
All, I had an interview today and I couldn't answer these questions.
Any good answers?
Why does a copy constructor param need to be const?
It doesn't.

Ever need a virtual constructor?
Is that a question?

See the FAQ on virtual constructors.

Possibly your interviewer hadn't read the FAQ.

Major difference between public and private inheritance (other than the
obvious one)?


What's obvious is subjective, so that's a question that only you can answer;
the question is essentially, what's the major non-obvious for _you_?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

"Alf P. Steinbach" <al***@start.no> schrieb im Newsbeitrag
news:42****************@news.individual.net...
* Sashi:
All, I had an interview today and I couldn't answer these
questions.
Any good answers?
Why does a copy constructor param need to be const?


It doesn't.


That doesn't help. If you answer questions like these, you upset your
possible boss. Argue with:

It doesn't. However it's clever to do so to create copies of const
objects. Also, the object copied from usually is not changed when
making a copy of it - however sometimes it _is_ required to be changed
ant thus, that's where the cctor mustn't have a const argument.

Ever need a virtual constructor?


Is that a question?
See the FAQ on virtual constructors.
Possibly your interviewer hadn't read the FAQ.


If you do this:
CBase* pBase = new CDerived();
you will call the ctor of the _derived_ function. You can't call the
ctor of the base if you want to create the derived. (my english is
bad, hopefully the content is not)

Major difference between public and private inheritance (other than
the
obvious one)?


What's obvious is subjective, so that's a question that only you can
answer;
the question is essentially, what's the major non-obvious for _you_?


Hmmm... What if you call a base class's public function of a privately
derived class?

class Base
{
public:
void foo();
}

class Derived : private Base
{
public:
void foo2() {Base::foo();}
}

int main()
{
Base* pDerived = new Derived;
pDerived->foo(); // what's it doing? (I don't know honestly)
}
HTH,
Gernot



Jul 23 '05 #3
* Gernot Frisch:

* Alf P. Steinbach:
* Sashi:

Why does a copy constructor param need to be const?
It doesn't.


That doesn't help.


Course it does: it corrects the invalid assumption in the question.

If you answer questions like these, you upset your possible boss.


Who would want a technically incompentent person who believes she is
technicallty competent, as boss?
[Re other stuff: see the FAQ]

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4
Gernot Frisch wrote:
....
class Base
{
public:
void foo();
}

class Derived : private Base
{
public:
void foo2() {Base::foo();}
}

int main()
{
Base* pDerived = new Derived;
This should fail. You're accessing a privately inherited class.
pDerived->foo(); // what's it doing? (I don't know honestly)


This line should be ok.

BTW - this is somthing your compiler will tell you much faster than I.
Jul 23 '05 #5
Alf P. Steinbach wrote:
* Gernot Frisch:

* Alf P. Steinbach:
* Sashi:
>
> Why does a copy constructor param need to be const?

It doesn't.


That doesn't help.


Course it does: it corrects the invalid assumption in the question.

If you answer questions like these, you upset your possible boss.


Who would want a technically incompentent person who believes she is
technicallty competent, as boss?


I wouldn't, but I like a paycheck more than I dislike such a boss. If
you're unemployed, nitpicking a potential employer is not going to
help. Nobody said you had to stay in a job where the boss was
incompetent. Take what you can get but keep interviewing for something
better.

Kristo

Jul 23 '05 #6


Kristo wrote:
Alf P. Steinbach wrote:
Who would want a technically incompentent person who believes she is
technicallty competent, as boss?


I wouldn't, but I like a paycheck more than I dislike such a boss.


Specially with those wedding deposits and all.
If
you're unemployed, nitpicking a potential employer is not going to
help. Nobody said you had to stay in a job where the boss was
incompetent. Take what you can get but keep interviewing for something
better.


You're speaking theoretically, of course. Besides, WONDERFUL cow-orkers
can make up for other defects of the job. Right?

Brian

Jul 23 '05 #7
the reason as to why a copy constructor has to be constant and is passed as
a reference is as follows :

as in it should be like say foo(const foo& abc){ }

is because :

suppose foo is a class which has a variable (say data) for which memory is
allocated using new and it is destroyed using delete ,then if u have the
following in main

main()
{
foo a;
foo b(a); //copy constructor evoked
}

now if a is passed as an object,ur program is going to crash as u will
have a temporary object created ,which will also refer to the same memory
location and once it goes out of scope, the memory is destroyed and when
object "a" goes out of memory - u r trying to free the same momory !
hence,it is passed as a reference and why it is a const reference is for
the simple reason that if u have
const foo a;
in main,then this object cannot be used to initialize another object till
u accept it as a const object.
hope this answered ur question.all the best........the other 2 answers
were already explained.

Jul 23 '05 #8

"maadhuu" <ma************@yahoo.com> wrote in message
news:ad******************************@localhost.ta lkaboutprogramming.com...
the reason as to why a copy constructor has to be constant and is passed
as
a reference is as follows :

as in it should be like say foo(const foo& abc){ }

is because :

suppose foo is a class which has a variable (say data) for which memory is
allocated using new and it is destroyed using delete ,then if u have the
following in main

main()
{
foo a;
foo b(a); //copy constructor evoked
}

now if a is passed as an object,ur program is going to crash as u will
have a temporary object created ,which will also refer to the same memory
location and once it goes out of scope, the memory is destroyed and when
object "a" goes out of memory - u r trying to free the same momory !
hence,it is passed as a reference


But (assuming its body is properly written) wouldn't the copy constructor
itself take care of the internal pointer, either allocating new memory or
else using some kind of reference-counting scheme? I mean, that's usually
why one writes a copy constructor in the first place, when there is
dynamically allocated member data.

The problem with passing by value, I believe, is that such a scheme would be
infinitely recursive. To make the copy of the object being passed, the copy
constructor would be invoked, which would need a copy of the object, which
would invoke the copy constructor, which would...

I'm not sure what the standard says about it, but my compiler won't even
allow me to define a copy constructor where the parameter is passed by
value, probably because there's no way to resolve the infinite recursion.

-Howard
Jul 23 '05 #9
I recently gave a talk on C++ for interviews at 7City.
It was videoed (fame at last :)

It's available http://www.wilmott.com/detail.cfm?articleID=222

There's handouts if anyone cares.

DominiConnor
Quant Headhunter

Jul 23 '05 #10
>> If you answer questions like these, you upset your possible boss.

Who would want a technically incompentent person who believes she is
technicallty competent, as boss?


Here in Germany unemployment rises. If you're in the east, you'll even
brush his shoes propably.
Jul 23 '05 #11
i totally agree with Mr Howard.....and i think ypu are absolutely right !!!
sorry for the mistake from my side!!

Jul 23 '05 #12
Sashi wrote:
All, I had an interview today and I couldn't answer these questions.
Any good answers?
Why does a copy constructor param need to be const?
Technically, the parameter to a copy constructor _has_ to be
a const reference only if you will be copying from const
instances.

But I think most would agree that it's counter-intuitive for
a "copy" operation to change the thing that's being copied.
Making the parameter a const reference gives an assurance
that the source of the copy is not changed.
Ever need a virtual constructor?
Yes. The inability to make a copy (outside the heap) of an object
when you only know its subclass is one of the limitations of
using virtual functions for generic code as opposed to templates.
But allowing virtual constructors or something equivalent for
stack variables would probably require drastic additions to
the language syntax. I don't see any way to do it without
breaking the rule (in Standard C/C++) that any object created
in the stack has a size known at compile time. (I don't
know what the value of this rule is, and GNU gcc has extensions
that break it anyway.) Hard to see how to use virtual constructors
for statically allocated variables because of the issue of the
size not being known at compile time.
Major difference between public and private inheritance (other than the
obvious one)?
They're _really_ deap sea fishing with this one. Some differences:

1) Can't address instances with pointer or reference to private
base class.

2) Even public members of private base class are inaccessable
in a dervived class instance from outside the derived class.

3) Can't be used to represent an "is-a" relationship (which is
basically saying the same thing as #1 but in a more non-obvious way).
Thanks,
Sashi


Jul 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jobs | last post by:
What is application object ? Application object can used in situation where we want data to be shared across users globally. What's the difference between Cache object and application object...
0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: Jobs | last post by:
All answers to the below interview questions are at http://www.geocities.com/dotnetinterviews/ or you can download the complete answer zip file from...
0
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if...
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
0
by: freesoftwarepdfs | last post by:
Ultimate list of Interview question website.....Do not miss it http://www.questpond.com http://msdotnetsupport.blogspot.com/2007/01/net-interview-questions-by-dutt-part-2.html...
0
by: Free PDF | last post by:
..NET , SQL Server interview questions websites.... http://www.questpond.com http://www.geocities.com/dotnetinterviews/...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.