Wednesday, January 27, 2010

Association , Aggregation and Composition

Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

Aggregation is a specialize form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let’s take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy.
UML:
An aggregation relationship is indicated by placing a white diamond at the end of the association next to the aggregate class. If B aggregates A, then A is a part of B, but their lifetimes are independent:


Composition is again specialize form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.
UML
Composition, on the other hand, is shown by a black diamond on the end of association next to the composite class. If B is composed of A, then B controls the lifetime of A.

Let’s take another example relationship between Questions and options. Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will automatically delete.
Composition is used for objects that have a HAS-A relationship to each other. A car has-a metal frame, has-an engine, and has-a transmission. A personal computer has-a CPU, a motherboard, and other components.

Compositions:
* Typically use normal member variables
* Can use pointer values if the composition class automatically handles allocation/deallocation
* Responsible for creation/destruction of subclasses

Aggregations:
* Typically use pointer variables that point to an object that lives outside the scope of the aggregate class
* Can use reference values that point to an object that lives outside the scope of the aggregate class
* Not responsible for creating/destroying subclasses

IS_A HAS-A difference
IS-A :Public inheritance Mango is a fruit , I am a man .
is-a (subsumption) is a relationship where one class D is a subclass of another class B (and so B is a superclass of D).
HAS -A : has-a relationship in an object is called a member field of an object. Multiple has-a relationships will combine to form a possessive hierarchy , Composition , Aggregation and association all comes under this But all have different Scope.
Private and Protected Inheritance alsocpmes under HAS-A relationship

REf
http://ootips.org/uml-hasa.html

Sunday, January 10, 2010

Good C++ Questions

* Would you be able to explain why/when base classes should have virtual destructors?
Answer : Only polymorpic( class has any virtual functions ) base class should have Virtual Destructor . C ++ specifies that when a derived class object is deleted through a pointer to a base class with a non-virtual destructor, results are undefined If derived class object pointer is accessed as base class pointer .now delete of that pointer(which points to derived object ) will release in Partial free i.e only base object will destroy and derived part will remain as it is . Virtual Destructor ensures proper calling of destructor
Putting virtual Destructor in nonpolymorphic class is an overhead as it will increase Size of every OBJ by 4 byte by inserting VPTR.
* Would you be able to differentiate between inheritance of interface and inheritance of implementation?
Answer: Derivation of Pure virtual function is inheritance of interface Pure virtual funtion will have no implemetation . and derived can implement it on their own . it only derives the interface
Derivation of Simple Member function is inheritance of implementation As derived calss will have same implemetatin of the function as it was in Base class .

* What sorting algorithms do you know? How fast are they?
Answer: I know Merge sort ,Bubble sort ,Quick Sort ,BST (Binary search tree ),
BST is best O(logn). But choice os soritng Algo depends on data size/data pattern also .

* What is "placement new" and why/when would You use it?
Answer: New gets Memory From Heap. But with Placement option we can provide A pre Allocated memory location to save object.
We Use it to void Mem allocation fail which is very rare . It is more used in making Memory pool . It Improves Performance also .

* Should a destructor throw an exception when it detects a problem?
Answer: 1) Destructor Should not throw an error . As Some time Destructor are called in an error scenario
In an Exception Handling from throw(obj) to catch all frames are deallocated automatically Which is called stack Unwinding All local Objs Destructor are called automatically at that point of time if any of the distructor will throw an error . Than its an problem to go to catch or process the thrown error So terminate got called and the whole program will exit

2) If there is and Array of pbjects and all have to dellaocate one by one .
If one in begining will throw an error How others destructor will get called This behaviour is undefined in C++ .
Thing is Destructor should Swallow the Exception in themselves .

* Can you explain the difference between "operator new" and "new operator"?
Answer: new operator is used for Memory allocation and it calls constructor also for .
operator new : Is actual function which only allocates memory Its Prototype is < void* operator (size_t) > , it can be overloaded .

* Where in a hierarchy should you use virtual inheritance?
Answer : In Multiple Inheritance If we know that at some level of inheritance Diamond problem ( A-->B A--->C and B,C-->D now D will have two instance of A) can arise.
To avoid the ambiguity of base object . Virtual Inheritance is required at first level i.e ( A virtual-->B A virtual --->C)

* How can you reseat a reference to make it refer to a different object?
Answer : Its not Allowed. REfrence cannot refer to new OBJECT (address) unlike a pointer.
We can understand like this Reference is a Const Pointer .
int d = 20;
int * const pref = &d ;
now pref++ is not allowed
*pref = 40 ; //is allowed ,pref cant point to any new location.
I hope we understand Diffrence between const pointer and pointer to const .
link : http://blog.voidnish.com/?p=37
Can we overload a Function by changing return type ?
Ans : No Changing of return type is ot considered as change in Function Signature
The C++ compiler identifies a function by its signature (function signature). The function signature is broken down into the following components in that order.

return type ,function label or name and parameters list

The parameter list is further defined as parameter label or name. If the order of the parameters (datatype name) changes even if the function name/label does not change the signature is considered unique. So in C++ it is conceivable to have many different functions/methods with exact same label name (for example Function1) so long as the entire signature is considered unique. So changing the number of parameters their data types the order of them or any combination of these can change the function signature.
Important Note: Changing the return type ALONE is NOT considered change in signature.

Followers