Relationships between object in C++

 


Relationships between objects in C++

In practice, there is a lot of recurring patterns we are experiencing in everyday life. These relationships gave insides of any practical real-life behaviors that help make consolation and a better understanding of the object. For example, if you see a car. You know car has four tayers and it needs fuel to run.

The car should have powered by electricity or fuel. If this car is newly launched. This information is a universal truth. How you know these things without riding that car. That is true you have not used that specific model but you had other cars that same properties. In programming are also have recurring relationships, patterns that we already used.

Association

Association in c++


An Association is a relationship between two classes. An object of one class must know about other classes in order to work or make a relationship. Association objects can belong to more than one object at the same time. The relationship is both unidirectional or bidirectional. The company and staff member is an example of an association. Company can have staff members, and any person can work in any company. The association model is “Uses a” relationship. Company use staff to work done. Staff uses a company to earn money.

Most often associations are implanted using Pointers.

Aggregation

An aggregation use part-whole-relation. It is a unidirectional relationship. One part can belong to more than one object at a time. It does not have a relationship but creating a part or destroying the part. If a car has the wheel, but car was not created wheels. If car destroys the wheel are still there.

Note – Aggregation models “Has -a” relationship (Car has four wheels, Company has staff member, the car has an engine)


 

 

 

Composition

This is a special case of aggregation, here part cannot exist without class: This is also a model of “has a” relationship between tow different objects. The car has wheels. The computer has a CPU. This is also a part-whole relationship. It's same as using class in c++.

 

Summery

There is the main  type of relationship used in programming. These relationships help to make the program more realistic.



Please comment if you have any suggestion and share to support us

😀 😄 😍 💗

0 Comments