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: Arvind — December 5, 2006
    +29 votes
      + -

    Compile error: no matching function B::func()

    Also note that func is private.

  2. Submitted By: Pranav — December 5, 2006
    +13 votes
      + -

    the output will be the compile error because B doent know the defination of func().

    To make this program run .it should be like

    Class A
    {
    public:
    void func();
    };
    class B:pubic A
    {
    using A::func();
    void func(int a);
    };
    int main()
    {
    B derv;
    derv.func();
    }

  3. Submitted By: Megha — December 7, 2006
    +2 votes
      + -

    there will be a compilation error bcoz A::func() is only declared but not defined anywhere. wen derv.func() is called , A::func() is called as it is publicly inherited from class A . B::func() can’t be called from main as it is private & also bcoz its prototype is different.

    using A::func() can only be used if class B was privately inherited from A.

  4. Submitted By: Abul Asim M. R. Qarshi — December 8, 2006
    -3 votes
      + -

    well its overloading instead or overriding.. so method will be called of the base class that is A.

    calling derv.func() will invoke A::func()

  5. Submitted By: raj malhotra — January 1, 2007
    -1 votes
      + -

    the output will be compiler error because the func() has no defintion

  6. Submitted By: ankit — January 4, 2007
    -7 votes
      + -

    this program contains the concept of function overloading!
    error will be there-too few parameters passed in derv::func(int x) !

  7. Submitted By: MikeS — January 18, 2007
    +4 votes
      + -

    compiler complains of syntax errors:
    Class A // upper case C
    class B:public A // what is a “pubic”

  8. Submitted By: lalitha — January 23, 2007
    +0 votes
      + -

    We get syntax error;
    here we are using overloading of function method but without function definition;
    so we ge error like this
    `class B derv’ has incomplete type and cannot be initialized

  9. Submitted By: Suvigya — February 1, 2007
    +2 votes
      + -

    It is an error as we have not defined function func() and a private function of Class B cannot be invoked from Main(). This is absurd!

  10. Submitted By: RK — May 3, 2007
    -1 votes
      + -

    The compiler will give following error

    Error: B::func(int) is not accessible from main().
    Too few arguments in call to “B::func(int)”.

  11. Submitted By: HXia — May 20, 2007
    +2 votes
      + -

    If ignore the typo and suppose the functions are defined somewhere.

    The derv.func(); will invoke A::func(). because:

    A::func()
    B::func(int a)

    are two different functions. A::func() is public. Sure the main return integer, but there is no return clause?

  12. Submitted By: vikram bhat — May 21, 2007
    +1 votes
      + -

    there will be compilation error because function overloading will not work for inheritance.
    even if you want it to work you must include the following code line in derived class.
    A::func();

    ofcourse the function needs to be public

  13. Submitted By: Siyad — May 23, 2007
    +7 votes
      + -

    Friends,

    For your reference this is what MS VC compiler gives :’func’ : function does not take 0 parameters.

    I believe the reason is, name hiding in C++. If you have same named identifier in two scopes. The later will hide the former. Eg. if you have variable X on global scope and a same named variable X there in one of the function scope, then the later will have previllage over the global one. And same applies to the functions too.

  14. Submitted By: anuthubn — May 25, 2007
    -4 votes
      + -

    The derv.func(); will invoke A::func(). because:

    A::func()
    B::func(int a)

    are two different functions. A::func() is public

  15. Submitted By: unknown — June 28, 2007
    -3 votes
      + -

    How can you create an object of an abstract class?

  16. Submitted By: FanZai — July 3, 2007
    +0 votes
      + -

    int main()
    return type is int, but no return;

  17. Submitted By: santhosh Kumar — July 13, 2007
    -2 votes
      + -

    line 6: class B:pubic A

    pubic A or public A?

  18. Submitted By: Bhabani Shankar — July 30, 2007
    +0 votes
      + -

    Overloading concept is applicable within the same scope. Hence it is irrevalent to overload the base class functions within derived classes. The concept of overriding of functions should be used for this sort of requirements.

  19. Submitted By: Muhammad Ishaq — September 19, 2007
    +2 votes
      + -

    Well, I have read all the answers above, and as already been said, func() is not visible because func(int a) has hidden it. (you can’t say that func(int a) is just an overloaded version of func(), overloading works in the same scope). To make it work, you have to provide a corresponding void func() in B and inside that you can call A::func();

  20. Submitted By: Herojeet — October 25, 2007
    not yet rated
      + -

    The answer suggested by “Muhammad Ishaq” is correct and verified. Assuming typo and other minor errors

  21. Submitted By: Blazer — August 13, 2008
    not yet rated
      + -

    All other typos (and mishaps?) aside:

    The func() in Class A will never be looked at, never the less executed, due to automatic data hiding when two functions are named the same in inherited classes. As mentioned earlier here, the correct way to call a base class function, is to use the class::function_name notation inside the derived class’ function.

    Class B: public A
    void func( ) {
    A::func();
    }

    Never the less, as the func fuction in B is not public, you will first get the error: “function does not take 0 parameters.”, and if you do pass a number, you will get an error stating something like “can’t access private member of class B”.

    Adding public: to class B. Solves that problem. But due to previously explained data hiding, you won’t invoke A::func(); either way, unless directly calling it from inside a function of B.

  22. Submitted By: Pankaj Nagrale — August 29, 2008
    -1 votes
      + -

    Well, func() in class A will be called as class B publically inherit class A. So all the public member function of A can be called by B if not available in class B and this is case of function overloading and not overriding. No syntax error or anything like that.

  23. Submitted By: Srijit — October 28, 2008
    -1 votes
      + -

    It will call the func in base class.an example of function overloading.

  24. Submitted By: Jason — November 9, 2008
    +5 votes
      + -

    The original code has a typo, so I’ll assume that was a mistake and that the intent is to discuss inheritance and overloading functions but still making the base class methods accessible from the child classes.

    1. You’ll get a compile error because the original code declares the classes and methods, but never defines them. To fix that, define the methods:

    class A
    {
    public:
    void func();
    };

    void A::func() {
    }

    class B : public A
    {
    void func(int a);
    };

    void B::func(int a) {
    }

    int main(void)
    {
    B derv;
    derv.func();
    }

    2. However, this will still fail compilation. This time, it is because you have overloaded the method func() from class A. There are two ways to fix this:

    a.) You can change the call in main() from derv.func(); to derv.A::func(); so that it will explicitly call the method in the base class.

    b.) You can use the “using” keyword in the base class definition to expose the overloaded method from the base class. The other posts above use the wrong syntax ‘using A::func();’ You should not include the parenthesis. So just change this to ‘using A::func;’ However, you can’t just do what the others have done here because they did not put this in a public: section so it would not be accessible outside the class.

    The correct way:

    class B : public A
    {
    public:
    using A::func;
    void func(int a);
    };

    Do you guys even try to compile your answers before posting them here as correct?

  25. 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.