Interview Question

  • -
    0
    of 0 votes
    8
    Answers

    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.

    - anuj.iiit2007 on June 06, 2012 in United States Report Duplicate | Flag
    C++

Country: United States


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

i am not sure if this all is needed. we have the header file with us. why can't we add the new required function GetArea() in the header itself for class shape and the hairarchy ?

MyLibrary.h
----------------

class shape {
//some declarations
protected:
double GetArea() {}; // This is the new addition.
}
The same can be done for the other derived classes as well.
In such case, we will not be breaking the original code which makes use of this library and the new code can make use of the new function which is now a part of the class shape and classes down it.

- sanjay on July 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Define class shape again in Your main file you will be done.

- Rishi on June 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is called Decorator Pattern. It will extend the functionality of existing object at runtime without altering its class layout. Refer wiki for more details.

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

Decorator won't work if "entire hierarchy" is also part of library. So question may need some clarification.

- Alex on June 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Visitor pattern can resolve this situation

- Adel on June 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you already have a header file of shape.h then you can declare and define a virtual function mGetArea() in the shape.h file itself. It will not impact any functionality of existing library and derived classes also overrides mGetArea() function if necessary.

Shape.h
class cShape
{
/*
* Existing Code
*
*/
//Newly Added Code
protected:
virtual double mGetArea(){
/*
* Implementation the GetArea logic here
*/
return lArea;
}
virtual ~cShape();
};

class cCircle:public cShape
{
double mGetArea(){
/*
* Circle specific GetArea Implementation.
*/
}
};

class cRectangle:public cShape
{
//If you won'n delcare mGetArea function here ,It will access the mGetArea function declared in cShape class.
};

- jagadish on July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cant we use templates?

- AJ on July 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This can be done using Multiple Inheritance I guess. Please check the below code..

Implementations of class's Shape, Circle, Rectangle, Square, etc are available in library

Implementations of class's NewShape, NewCircle, NewRectangle, NewSquare, etc are implemented by us to include getArea() into class Shape and its Derived classes


class Shape
{
//implementation
};

class Circle : public Shape
{
//implementation
};

class Rectangle : public Shape
{
//implementation
};

class Square: public Shape
{
//implementation
};


class NewShape : public Shape
{
public:
virtual double mGetArea() = 0;

}

class NewCirlce : virtual public Shape, virtual public Circle
{
public:
double mGetArea()
{
// implementation
}
}

class NewRectangle : virtual public Shape, virtual public Rectangle
{
public:
double mGetArea()
{
// implementation
}
}

class NewSquare : virtual public shape, virtual public Square
{
public:
double mGetArea()
{
// implementation
}
}

- Vignesh on June 17, 2012 | Flag Reply


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