Home > OOP > Containership in C++

What is containership in c++?

Aggregation in c++ or containership or nesting refers to creating the object/s of a class in another class. The class which contains the objects/s of other class or classes is container class. You can create a container class by making objects of other classes inside that classes. Containership in c++ is different from inheritance as inheritance is “is-a” kind of relationship while in containership we have “has-a” relationship or “part-whole” relationship. E.g. a book is a part of a library.

Aggregation is a strong relationship. In aggregation whenever we create a concrete instance of the container class. It will have the objects of other classes as its attributes. As when we have a library it is necessary to have books in it. It means whenever we create an object of library class it will always make an object for the book as its attribute.

The basic syntax for aggregation in c++ is:

class A
{
     public:
          A ()
          {
               // this is a constructor of class A.
          }
};
class B
{
     /* An object of class A is created inside class B. 
       This is called Containership. */
     A object_A;
     public:
          B ()
          {
               // this is a constructor of class B.
          }
};

Example: –

#include "stdafx.h"
#include "iostream"
#include"string"

using namespace std;

class Person
{
     protected:
          string name;
          string address;
     public:
          Person()
          {
               name ="";
               address = "";
          }
          void getData()
          {
               cout << "Please Enter Name : ";
               getline(cin, name);
               cout << "Please Enter Address : ";
               getline(cin, address);
          }
          void show()
          {
               cout << "Name : " << name << endl;
               cout << "Address : " << address << endl;
          }
};

class Student : public Person
{
     protected:
          string regNo;
          float cgpa;
     public:     
          void      
          {
               Person::getData();
               cout << "Please Enter Registration Number : ";
               getline(cin, regNo);
               cout << "Please Enter cgpa : ";
               cin >> cgpa;
               cin.ignore();
          }
          void show()
          {
               Person::show();
               cout << "Registration Number : " << regNo << endl;
               cout << "CGPA : " << cgpa << endl;
          }
};
class Teacher :public Person
{
     string designation;

     public:
          void getData()
          {
               Person::getData();
               cout << "Please Enter Department : " ;
               getline(cin, designation);
          }
          void show()
          {
               Person::show();
               cout << "Designation : " << designation << endl;
          }
};
class School
{
     /* Creating Objects of student and teacher class. 
        As Students and teachers are part of school. */
     Student s;
     Teacher t;
     
     public:
          void getRecord()
          {
               cout << "***Getting Student Record*** \n\n";
               s.getData();
               cout << "\n***Getting Teacher Record*** \n\n";
               t.getData();
          }
          void showRecord()
          {
               cout << "\n***Student Record*** \n\n";
               s.show();
               cout << "\n***Teacher Record*** \n\n";
               t.show();
          }
};
void main()
{
     School sch;
     sch.cout << "\n***School Record*** \n\n";
     sch.showRecord();
}

Output: –
container class - containership in C++ example

In the above example, two classes student and teacher are inheriting from the class person (Inheritance) while class school is composed of two classes student and teacher ( Aggregation / Containership ).

Example

#include "stdafx.h"
#include "iostream"
#include "string"

using namespace std;

class Address 
{
     string address;
     string city;
     string province;

      public:
           Address()
           {

           }
           Address(string address, string city, string province)
           {
                this->address = address;
                this->city = city;
                this->province = province;
           }
           string getAddress()
           {
               string add;
               add = address + ", " + city + ", " + province;
               return add;
           }
};
class Employee 
{
     //creating object of address clase inside employee class.
    //Each employee has an address representing has-a relationship. 
      Address addressOfEmployee; 
      int id;
      string name, address;

     public:
          Employee() 
          {

          }
          Employee(int id, string name, Address addressOfEmployee) 
          {
               this->id = id;
               this->name = name;
               this->address = addressOfEmployee.getAddress();
          }
          void display()
          {
              cout << id <<"\t";
              cout << name <<"\t";
              cout << address <<"\t";
          }
};
int main(void) 
{
     int id;
     string name, homeAddress, city, province;

     cout << "Please Enter Employee Id\n";
     cin >> id;
     cin.ignore();
     cout << "Please Enter Employee Name : ";
     getline(cin,name);

     cout << "*****Please Enter Address of Employee*****";
     cout << "\nPlease Enter Home Address :";
     getline(cin, homeAddress);

     cout << "Please Enter City : ";
     getline(cin, city);

     cout << "Please Enter Province : ";
     getline(cin, province);

     Address ad = Address(homeAddress, city, province);
     Employee emp = Employee(id, name, ad);
     emp.display();
     cout << endl;
     return 0;
}

aggregation in c++

Whenever you need to find whether aggregation exists between classes. just try to read them as:

  • The car has Engine.
  • The car has wheels.
  • Libray has Books.

Aggregation in c++ is like ownership as the parent and child. It means a child object can not belong more than one parents at the same time. The container class is not responsible for destroying the parts or subclasses. E.g. If we don’t have the library we still have books or if a car doesn’t exist we can still have engines.

Note:- Aggregation or containership in C++ is different from nesting in c++. 

Nesting refers to simply defining a class inside another class.

E.g.

class A
{
    public:
       // Nesting the class B
       class B 
       {
          //Class B Body.
       };
};

One thought on “Containership in C++

  1. Pingback: Objects in C++

Leave a Reply

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