IBM Interview Question for Developer Program Engineers






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

virtual overloading means the overriding of virtual function means the already existing base class virttual function we redefine in derived classes

- kumar August 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would say this is a trick question. It's called virtual overriding unless I'm much mistaken. Companies love to trip people up on this because they think people that just read a book the day before will be confused by the similarity of names between "overloading" and "overriding".

- Gene September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Virtual overloading means runtime overloading or simply overriding.

- manish2you September 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The compiler will attempt to resolve any member function in the current class scope. Since a double can be converted to an int, the compiler finds a function foo() in the current class scope of Derived that matches. Therefore it chooses that function.

So basically, if the compiler finds a name of a function, it only looks in the current class scope for matches in parameter types (or looks for one where the parameter type given in the call can be converted to the type in the function declaration).

If the compiler does not find the function name in the current class scope, then and only then does it look in the parent class for a name that matches.

See below code snippet:-

#include <iostream>
using namespace std;

class Widget
{
};

class Base {
public:
    virtual void foo(int f) {
        cout << "Base Foo 1:: "<<f<<endl;
    }
    virtual void foo(double f) {
        cout << "Base Foo 2:: "<<f<<endl;
    }

    void foo(Widget& w){
        cout << "Calling Widget\n"<<endl;
    }
    virtual void func(){
         cout<<"BASE"<<endl;
    }
};

class Derived : public Base {
public:
    void foo(int f) {
        cout << "Derived Foo 1:: "<<f<<endl;
    }
};

int main() {

    Derived *d = new Derived;
    Base *b = d;

    cout << "Calling Base foo(double) through Base\n";
    b->foo(3.14);
    cout << "Calling Base foo(double) through Derived\n";
    d->foo(3.14);
    Widget w;
    d->func();  // error
    d->foo(w);
    delete d;
    getchar();
}

- Sagnik Majumder January 31, 2012 | 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