AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: johny_bravo — October 6, 2006
    -6 votes
      + -

    foo() for class C will be called.

  2. Submitted By: zeuscony — October 6, 2006
    +3 votes
      + -

    it depends. if in A foo is defined as virtual function. then call C’s foo(),
    if it doesn’t defined virtual, then
    call A’s foo()

  3. Submitted By: Venu — October 6, 2006
    +20 votes
      + -

    Actually, if access is NOT specified, it deafults to private derivation. In private derivation, binding is static. So, whether foo is declared virtual or not it still defaults to static binding. So, A->foo() is called.

    However, If a public derivation is specified from C <– B and B <– A, then if foo() is virtual C->foo() is called; if foo() is non virtual A->foo() is called.

  4. Submitted By: govind — October 18, 2006
    -4 votes
      + -

    foo is not virtual than class c’s function will be called other wise as it searched top to bottom class A’s function will be called

  5. Submitted By: Jim Stevens — November 14, 2006
    +2 votes
      + -

    The question is deeply ambiguous, with answer 4 ( venu ) above, being the correct one:

    I have interpreted it as follows,
    the correct answer in this interpretation being A

    class a
    {
    private:
    int a;

    public:
    int foo( int i );
    };

    class b : public a
    {
    private:
    int b;

    public:
    int foo( int i );
    };

    class c : public b
    {
    private:
    int c;

    public:
    int foo( int i );
    };

    int a :: foo( int i )
    {
    i ++ ;
    }

    int b :: foo( int i )
    {
    i ++ ;
    }

    int c :: foo( int i )
    {
    i ++ ;
    }

    void my_application :: a_test( void )
    {
    a A;
    b B;
    c * cptr = new c;

    (( a * )cptr )->foo( 1 );

    delete( cptr );
    }

  6. Submitted By: Nitin — May 25, 2007
    +2 votes
      + -

    The following statement “In private derivation, binding is static.” in Venu’s answer is incorrect. The binding depends upon whether the function is declared virtual or not. I think it has nothing to do the type of derivation.

    I tested following piece of code:
    #include
    class A
    {
    public:
    virtual void foo()
    {
    std::cout foo();
    int i;
    std::cin >> i;
    }

    and it prints “In B”

  7. Submitted By: Nitin — May 25, 2007
    not yet rated
      + -

    Here is the correct program

    #include
    class A
    {
    public:
    virtual void foo()
    {
    std::cout foo();
    int i;
    std::cin >> i;
    }

  8. Submitted By: santoshk — October 15, 2007
    -1 votes
      + -

    yeah what ever Nitin said is true.. it does not depend on the public or private derivatio or either public or private function . It depends only on virtual or not .. ;-)

  9. Submitted By: shreejc — November 15, 2007
    not yet rated
      + -

    ” I cast C to A “- derived class pointer can not point to base class. So casting C to A itself is impossible.
    If somebody feels otherwise, please prove with code.

  10. Submitted By: Bill Campbell — February 6, 2008
    not yet rated
      + -

    When you cast a C to a A slicing occurs… you loose C and B s A.foo () is called

  11. Submitted By: Amit — June 10, 2008
    not yet rated
      + -

    Bill Campbell is correct.

    When you cast C to A, object slicing occurs - meaning the “object” is treated of type A, so it looses all of the derived class details.

    So, irrespective of foo() being virtual or not, A.foo() would be called.

    In general, Virtual functions can only be called through pointers, where a base class pointer is refering to a derived class’s object.

  12. Submitted By: Kiran — September 22, 2008
    not yet rated
      + -

    I interpret this question like this, we have class a which derives both b and c

    Class b {
    public:
    int foo();
    }

    Class c {
    public:
    int foo();
    }

    Class a: public b, public c {

    public:
    int foo();
    }

    c *c1 = new c();
    a *a1;
    /* we do a cast of c to a */
    a1 = c1;

    /* So guys we have two interpretations, C is downcasted to a. C already has foo function. The function foo which is active in memory is that of c->foo().

    So no matter you call a->foo(); you would get result of implemetation done by c->foo() and not the implementation of a->foo();

    Let me know if i am wrong.

  13. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.