GeeksforGeeks » Interview Questions
Adobe Interview Question for Software Engineer/Developer about C++
(6 posts)-
You have a library provided by the vendor. All you have is header files and library files.
Library contains the class Shape and there is whole hierarchy tree (i mean classes which derive from this base class).
Now you want to add some function "getArea" (not originally present in the class or any of its derived class) in the class "Shape" , you dont have the source code.Using this library, you have written a lot of code. Now you have to make some changes so that, any object of Shape class (or its derived class) will be able to call this function.
With your strategy, you should be able to override the definition of this function in the derived class.I think it is related to design patterns, not sure.
I gave the solution that : we will use MyShape , and use composition. i.e. will create the object of Shape class inside this "NewShape" class and interface can be made. Internally, we can call the functions of "Shape" object. That will work fine. But we have to do this for the whole design hierarchy. Secondly, if there is some changes by the vendor in future in the library, i have to change my new design tree.
e.g.class Shape { public : void draw(); void clear(); } class Circle : public Shape{ public : void draw(); void clear(); } class Rectangle: public Shape{ public : void draw(); void clear(); } class NewShape{ private Shape * obj; public : void draw(){ obj->draw(); } void clear() { obj->clear(); } int getArear(){ //implementtation of getArea function } } class NewCircle : NewShape{ private Circle * obj; public : void draw(){ obj->draw(); } void clear() { obj->clear(); } int getArear(){ //implementtation of getArea function specific to circle } } class NewRectangle : NewShape{ private Rectangle * obj; public : void draw(){ obj->draw(); } void clear() { obj->clear(); } int getArear(){ //implementtation of getArea function } }I know this solution is not good.. please provide a better approach for this.
Please let me knw if anything is not clear. -
I think virtual functions were required.
-
In the shape class header file we can create a new class and create a virtual function getArea() in that class.
Now Shape class definition can be changed a little to inherit from the newly created class. -
i think the composition solution is good as you do not have access to source of shape..all you have is headers and libs...so you need to make such a change which uses present libs and extend that to implement the getArea function which is what composition providing...
please comment... -
sorry i was confused with the fact that you will need to recompile the lib..which anyways you will need to so its best to create a class with virtual function.
-
u were wrong during the interview and still u r wrong..try to get the correct solution...try to ask from some good IIT/IIIT guys...they have good knowledge so may be they will help you.
Reply
You must log in to post.