Interview Question

  • 0
    of 0 votes
    2
    Answers

    virtual function and dynamic binding is okey, but what is the use of dynamic binding ? why we need it ? we can use "if ... else " conditions and functions pointers instead.

    - Sachin on July 25, 2012 in India Report Duplicate | Flag
    C++

Country: India
Interview Type: Phone Interview


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

Yes we can. But it's less convenient and much more annoying to do so.

- Anonymous on July 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Moreover, if you use the " if ... else " constructs instead of virtual functions, you will have to change your top level processing code whenever a new subtype needs to be handled, while with virtual functions the only thing that needs to be doen is to pass the pointer or reference of the proper instance to the top level function that expects a pointer or reference to the base class.
See the below code that I have created for specifically this comment and see the diffs of conditional and dynamic processing:

class Base{
public:
    Base() : class_type(0){};
    virtual ~Base() {};
    
    virtual void process(void) {// some processing};
    int class_type;
};

class D1 : public Base{
public:
    D1() : class_type(1){};
    virtual ~D1() {};
    
    virtual void process(void) {// some processing};
    int class_type;
};

class D2 : public Base{
public:
    D2() : class_type(2){};
    virtual ~D2() {};
    
    virtual void process(void) {// some processing};
    int class_type;
};


void handle_objects_conditionally(Base *obj)
{
    if (obj->type == 0)
        //call some Base class specific handler
    else if (obj->type == 1)
        //call some D1 class specific handler
    else if (obj->type == 2)
        //call some D2 class specific handler
}

void handle_objects_dynamically(Base *obj)
{
    obj->process(); // the proper handler shall be called automagically for you
}

- ashot madatyan on July 25, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through 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