0

Recently I attended an interview. The interviewer and I were talking about virtual functions in c++. I explained all the concept of virtual function and how It works using vtable and vptr. He was ok with my explanation. At the end of the conversation about virtual function, he asked me a question that why should we call the derived class function using a base class pointer?

For Ex., Shape is a base class. Circle and Square are derived classes inherited from the base class 'Shape'. Defined method area() is the virtual function.

Shape *basePtr = new Circle();
baseptr->area();

He asked me why are we using the base pointer to call the derived class function here? Why don't we create a derived class object and through the object we can call the derived class method?

I told him, we can create a handler function as the parameter of the function always be as base class pointer. So that we can simply call the derived class object(s) to the handler function for the use of Generic implementation.

Also, we can store the derived class objects into a vector of the base class pointer and we can access the derived class methods by iterating the vector. Through this, we can set up an order to do the task sequentially.

But he just smiled and shook his head. Why I am posting this question here is what is the exact answer to the question? or My answers of enough to explain the question?

skratchi.at
  • 1,151
  • 7
  • 22
Smith Dwayne
  • 2,675
  • 8
  • 46
  • 75
  • 2
    I personally find your examples just fine. Maybe he was looking for a broader explanation (i.e. sometimes we want to type-erase objects for runtime flexibility or optimization purposes, and virtual dispatching is one tool to do that), but we can't read his mind. – Quentin Apr 09 '19 at 11:06
  • 3
    Their question (as you phrased it) is kind of nonsensical. You _are_ creating a derived class object, whether you store it in a `Shape*` or a `Circle*` doesn't matter. Maybe they wanted to hear more about static vs dynamic type and how you need a pointer/reference for the dynamic type to matter. But we can only guess because the intent of the question has been altered by the interviewer's choice of words, your perception of the words, your recollection of their intent, and then reproducing it here, so it's likely that some important nuance has been lost. – Max Langhof Apr 09 '19 at 11:14
  • 1
    2¢. The key point to polymorphism is code reuse. The caller of the polymorphic object does not have to know about the different variety of objects, it just relies on the interface contract and uses the object. Example, passing an ostream the user of the ostream does not know if it is a string stream, a file stream, a console output stream, a tcp/ip stream. The call site does not need to have a switch statement and case all the different kinds of streams. If a new stream is added, the user of the stream doesn't need to be recoded. Separation of concerns. – Eljay Apr 09 '19 at 11:24
  • 1
    I guess if you have a limited set of memory and you'll create an object to access the members of every class, it means, the program will used more memory for storing every object. Since we are using inheritance we should utilize its advantages like fast implementation time and code reuse functionality. – Master James Apr 09 '19 at 11:44
  • 1
    Sometimes an interviewer will ask why rather than how. By doing this thing, he/she can analyze the way you code. You must need to understand why object oriented came to its existence since we can code without using OOP concept. – Master James Apr 09 '19 at 11:50
  • 1
    Maybe the interviewer was looking for you to say something about dependency inversion, or other concepts that virtual can be used for? – Fantastic Mr Fox Apr 09 '19 at 13:28
  • I'm voting to close this question as off-topic because it's asking us to speculate about the mind of an interviewer. – Ben Apr 10 '19 at 10:11
  • @Ben: Ok. then where to ask these type questions in Stack Exchange forums? – Smith Dwayne Apr 10 '19 at 10:33
  • @SmithDwayne I don't know. Maybe nowhere? There might not be an appropriate forum for every possible question. – Ben Apr 10 '19 at 10:34
  • @Ben: Ok. But my main criteria of this question is `Why virtual function?` other than my two answers. I want to check If there are other answers? – Smith Dwayne Apr 10 '19 at 10:37
  • @SmithDwayne if that's your question then it's a duplicate. – Ben Apr 10 '19 at 10:44

0 Answers0