Some interview questions

Discussion in 'C++' started by Sashi, Jun 14, 2005.

  1. Sashi

    Sashi Guest

    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
     
    Sashi, Jun 14, 2005
    #1
    1. Advertising

  2. * 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?
     
    Alf P. Steinbach, Jun 14, 2005
    #2
    1. Advertising

  3. "Alf P. Steinbach" <> schrieb im Newsbeitrag
    news:...
    >* 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
     
    Gernot Frisch, Jun 14, 2005
    #3
  4. * 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?
     
    Alf P. Steinbach, Jun 14, 2005
    #4
  5. 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.
     
    Gianni Mariani, Jun 14, 2005
    #5
  6. Sashi

    Kristo Guest

    [OT] Re: Some interview questions

    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
     
    Kristo, Jun 14, 2005
    #6
  7. Sashi

    Default User Guest

    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
     
    Default User, Jun 15, 2005
    #7
  8. Sashi

    maadhuu Guest

    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.
     
    maadhuu, Jun 15, 2005
    #8
  9. Sashi

    Howard Guest

    "maadhuu" <> wrote in message
    news:...
    > 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
     
    Howard, Jun 15, 2005
    #9
  10. Sashi

    Guest

    , Jun 15, 2005
    #10
  11. >> 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.
     
    Gernot Frisch, Jun 16, 2005
    #11
  12. Sashi

    maadhuu Guest

    i totally agree with Mr Howard.....and i think ypu are absolutely right !!!
    sorry for the mistake from my side!!
     
    maadhuu, Jun 16, 2005
    #12
  13. Sashi

    Guest

    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
     
    , Jun 16, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pandapower

    some interview questions.

    pandapower, Nov 24, 2003, in forum: C Programming
    Replies:
    26
    Views:
    1,181
    CBFalconer
    Nov 28, 2003
  2. Jatinder

    Some Questions Asked in Interview

    Jatinder, May 14, 2004, in forum: C Programming
    Replies:
    50
    Views:
    4,078
    Dan Pop
    May 24, 2004
  3. Matt

    Some Interview Questions

    Matt, Oct 26, 2004, in forum: C Programming
    Replies:
    18
    Views:
    1,464
    Chris Torek
    Oct 30, 2004
  4. Digital Puer

    some C++-specific interview questions

    Digital Puer, Sep 7, 2006, in forum: C++
    Replies:
    39
    Views:
    1,091
    Digital Puer
    Sep 9, 2006
  5. reema
    Replies:
    0
    Views:
    294
    reema
    Aug 26, 2008
Loading...

Share This Page