Containership in C++

We can create an object of one class into another and that object will be a member of the class. This type of relationship between classes is known as containership or has_a relationship as one class contain the object of another class. And the class which contains the object and members of another class in this kind of relationship is called a container class.

Syntax for Containership:

// Class that is to be contained
class first {
    .
    .
};

// Container class
class second {

    // creating object of first
    first f;
    .
    .
};

Below examples explain the Containership in C++ in a better way.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// concept of Containership
  
#include <iostream>
using namespace std;
  
class first {
public:
    void showf()
    {
        cout << "Hello from first class\n";
    }
};
  
// Container class
class second {
    // creating object of first
    first f;
  
public:
    // constructor
    second()
    {
        // calling function of first class
        f.showf();
    }
};
  
int main()
{
    // creating object of second
    second s;
}

chevron_right


Output:

Hello from first class

Explanation:In the class second we have an object of class first. This is another type of inheritance we are witnessing. This type of inheritance is known as has_a relationship as we say that class second has an object of first class first as its member. From the object f we call the function of class first.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class first {
public:
    first()
    {
        cout << "Hello from first class\n";
    }
};
  
// Container class
class second {
    // creating object of first
    first f;
  
public:
    // constructor
    second()
    {
        cout << "Hello from second class\n";
    }
};
  
int main()
{
    // creating object of second
    second s;
}

chevron_right


Output:

Hello from first class
Hello from second class

Explanation:In this program we have not inherited class first into class second but as we are having an object of class first as a member of class second. So when default constructor of class second is called, due to presence of object f of first class in second, default constructor of class first is called first and then default constructor of class second is called .

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class first {
private:
    int num;
  
public:
    void showf()
    {
        cout << "Hello from first class\n";
        cout << "num = " << num << endl;
    }
  
    int& getnum()
    {
        return num;
    }
};
  
// Container class
class second {
    // creating object of first
    first f;
  
public:
    // constructor
    second()
    {
        f.getnum() = 20;
        f.showf();
    }
};
  
int main()
{
    // creating object of second
    second s;
}

chevron_right


Output:

Hello from first class
num = 20

Explanation:With the help of containership we can only use public member/function of the class but not protected or private. In the first class we have returned the reference with the help of getnum. Then we show it by a call to showf.



My Personal Notes arrow_drop_up

A fallen star which will rise again

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.