Amazon Interview Question
Software Engineer / DevelopersCountry: India
just to add more explanations: containment is used when the outer object needs to modify the behaviour of the inner object.
for the second part of the question: when the outer object dies, the contained inner object is also destroyed (since its not exposed to the outer world)
while in case of aggregation, it's not necessarily true:
you just decrease a reference counter since same object can be aggregated by other objects as well
Interfaces are abstract C++ classes that has mostly all functions as pure virtual functions and generally has name starting with "I". IAuto, IGame, IPhone etc...
Interfaces define all the basic functionality a particular COM component should have.
COM component is a C++ class that inherits/implements these Interfaces.
This COM component exposes different methods using its Interfaces
eg:
class IPhone
{
public :
virtual void LiftUp()==0;
virtual void PutDown()==0;
};
ISmartPhone
{
public :
virtual void Touch();
};
class MyPhone : public IPhone, public ISmartPhone
{
public :
void LiftUp()
{}
void PutDown()
{}
void Touch()
{}
};
class MySmartPhone
{
public :
MyPhone* m_pPhone;
//This is aggregation where you define a Class pointer and leaves the decision of allocation and release of memory dynamically.
For which you need to call CoCreateInstance() and Release() methods when you use it.The client code that uses it, needs to take care of allocating memory and releasing it.
};
Containment explained :
This is achieved by defining a smart pointer of a COM component class.
Advantage :
Memory allocation and de allocation is taken care by the mechanism. User/client code need not worry about it.
eg:
class MySmartPhone
{
public:
MyPhone m_Phone; //This is contentment as memory is allocated statically and client code doesn't need to bother about memory allocation and releasing it.
};
Interfaces are abstract C++ classes that has mostly all functions as pure virtual functions and generally has name starting with "I". IAuto, IGame, IPhone etc...
Interfaces define all the basic functionality a particular COM component should have.
COM component is a C++ class that inherits/implements these Interfaces.
This COM component exposes different methods using its Interfaces
eg:
class IPhone
{
public :
virtual void LiftUp()==0;
virtual void PutDown()==0;
};
ISmartPhone
{
public :
virtual void Touch();
};
class MyPhone : public IPhone, public ISmartPhone
{
public :
void LiftUp()
{}
void PutDown()
{}
void Touch()
{}
};
class MySmartPhone
{
public :
MyPhone* m_pPhone;
//This is aggregation where you define a Class pointer and leaves the decision of allocation and release of memory dynamically.
For which you need to call CoCreateInstance() and Release() methods when you use it.The client code that uses it, needs to take care of allocating memory and releasing it.
};
Containment explained :
This is achieved by defining a smart pointer of a COM component class.
Advantage :
Memory allocation and de allocation is taken care by the mechanism. User/client code need not worry about it.
eg:
class MySmartPhone
{
public:
MyPhone m_Phone; //This is contentment as memory is allocated statically and client code doesn't need to bother about memory allocation and releasing it.
};
Containment and Aggregation is to achieve re-usability through object composition.
- Anonymous February 02, 2012Containing Object never exposes internal object to clients where as aggregation delegates responsibility for calls directly to internal component.
delegate responsibility for calls directly to internal
component