Can anyone explain what "HAS-A" and "IS-A" means between two class.
An example would helpful.
Can anyone explain what "HAS-A" and "IS-A" means between two class.
An example would helpful.
Eg:
class SteeringWheel
{};
class Vehicle
{
virtual void doStuff() = 0;
};
class Car: public Vehicle
{
SteeringWheel sWheel;
virtual void doStuff();
};
In the object-oriented world, a class can be something or it can contain something.
For example, a Queue
class may be a sub-class of a LinkedList
class (since a linked list can certainly be used to implement a queue). That is an is-a
relationship. Everything that you can do to the linked list, you should be able to do to the queue.
However, the queue class may also hold other information such as the number of items in the linked list (for efficiency).
To that end, it may also define a member variable called size
. That would be a has-a
relationship - the queue is not a subclass of the integer, it simply contains an integer.
These are two common forms of relationship between two classes.
The HAS-A relationship refers to a class X which has a class Y as a component, probably expressed by placing an instance of class Y as an attribute in every object of class X.
The IS-A relationship refers to a class W which is a class Z, probably because class W is a subclass of class Z, or has class Z somewhere in its inheritance graph. Code which knows how to deal with instances of class Z should be able to handle instances of class W with no code changes required.