Containership in C++


In this tutorial, we will be discussing a program to understand containership in C++.

The parameter if a certain class contains another class is called as containership. The inside class is called contained class, while the class in which it is present is called container class.

Example

 Live Demo

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

Output

Hello from first class
Hello from second class

Updated on: 12-Mar-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements