Types of Relationships in Objects in C++

So in this tutorial, we will learn about objects and how they exist in class. These objects are present in the same or different classes and have some kind of relationship. With an understanding of these, we can get an idea of how real-life objects behave.

Relationship in objects in C++

Objects and Classes

Objects are some entities that have some features and behavior. Many objects get together to make a class. In a class many objects are present. Real-life example: Maruti Suzuki is an object of class Cars. Now as Maruti Suzuki is an object so it has characteristics like blue color, 4 wheels, etc. These characteristics are called ‘Attributes‘. It has behavior i.e. “Methods” like it honks, halts, runs fast, etc. 

Similar to real life, objects in programming also have relationships. They follow some patterns and have hierarchies. The Class can be said as the “blueprint” for the creation of objects.

Relationships in Objects:

Many types of relations exist between objects. Object-Oriented Programming has different objects and different relationships exist between them too. 

1. Composition:-

In this type of relationship, parts of an object are completely dependent on it. This means that the objects would not have any existence without the object. If we destroy the object, then these parts would immediately get destroyed too. They make up an object and are vital for it. Here objects depend on each other. This means that the existence of one is impossible without others. 

One of the best examples for this is: Login Page and Title Bar – both are completely dependent on Computers. Though, the latter (dependency of computer on items) is not true. In other words, we can say that the Login Page and Title Bar have no sole existence without the Computer. Such a relationship is used when we make some complex objects from simple ones.

Another example of real-life is Our Body and Blood. Blood resides inside a body. This means that blood is a part of the body. Blood is useless if it is not inside a body. Blood present outside the body as no importance.

class Number
{
private:
  int n;
  int d;
 
public:
  Number(int numer=0, int denom=1):
    n{ numer }, d{ denom }
  
    //reduction() in the constructor is used to ensure any number which is in fractional form which we can reduce
    reduction();
  }
};

 

It should be dependent only on one thing at a time and must one thing contains others. We represent this by a Solid Diamond attached to a straight line.

2. Aggregation:-

Now let us see Aggregation relationship.

The Composition is a part-whole relationship, where the parts are contained within the whole.

In these parts of some objects are not compulsorily dependent on one object only. They have their free existence and meaning too. For instance, headlight and engine of a Car. Here, the car is the main object. Headlight and engine are present in a car. But this does not mean that they cannot have their sole existence. This headlight can be removed and used in some other car too.

We represent this type of relationship by a Hollow Diamond, attached to a straight line.

3. Association:-

 We have seen two types of relationships of objects above. In both of them, one thing was common and it is that they both have an object-part relationship. There were some mutual needs and dependencies. This is the relationship between two objects which are not related to each other. In this, there are various points to be there for Association to exist.

  • They must have exactly 1 relationship – 1
  • Zero or more – 0 to infinite
  • Zero or one -0 to 1
  • Specified range – eg. 2 to 5
  • Multiple Disjoint- eg. 2 to 5, 6 to 10

Association is simply some kind of connection between two objects. Here the relationship may be in one direction or in both directions that is either both are dependent on each other or anyone. For example, the relationship between a teacher and a student. It is a relationship in which both are important for each other but not connected or dependent on each other only. The Teacher will teach who if the student is not there. Similarly, who will teach the student if the teacher is not there? But this does not mean that one teacher teaches only one student and vice-versa.

#include <string>
class Subject
{
private:
    std::string Name;
    const Subject *p;
 
public:
    Subject(const std::string &Name, const Course *p = nullptr):
        Name{ Name }, p{ p }
    {
    }
 
};

 

Many students are taught by the teacher at the same time. The Student is also studying with many students and many different teachers. One teacher may only teach one child ( in case of home tuition ). What if that teacher stops teaching that child? The child may find some other teacher. We represent these by using vectors.

4. Dependency:-

Dependency is a relationship that happens when one object call forth another object to work and function to finish some specific task. This relationship is not so strong as association. This is one-direction relationship. When two things do the same task and need each others’ help, then it is dependency. We represent it by just an arrow and straight dotted lines.

5. Generalization:-

This is a relationship, just used to denote that one thing comes under another. It is a simpler and broader version of both Aggregation and Composition. For instance, Student and Person. Here the Student is a part of the bigger class- Person. Now, a person is any human being. But the student is a specific type of person who studies. It is represented by a hollow triangular arrow and a straight line attached to it.

In conclusion, I would say that all objects have some connection with their classes and other objects of that class. We explored in detail about all the relationships here. These are also the basics of UML in OOPS. To do programming, it becomes important to first clear these concepts. Objects and Classes and the way they are connected must be known in C++.

Also read: Euler tour of a tree in C++

Leave a Reply

Your email address will not be published. Required fields are marked *