Containership in C++ with Example



Whenever an object of a class is declared as a member of another class it is known as a container class.
In the container-ship the object of one class is declared in another class.

Example:

#include<iostream.h>
#include<conio.h>
class A
               {
                              int a;
                              public:
                              void get();
               };
class B
               {
                              int b;
                              A t;         // object t of class A is declare in class B
                              public:
                              void getdata();
               };
void A :: get()
{
               cin>>a;
               cout<<a;
}
void B :: getdata()
{
               cin>>b;
               cout<<b;
               t.get();                  //calling of get() of class A in getdata() of class B
}
void main()
{
               Clrscr();
               B ab;
               ab.getdata();
               getch();
}

3 comments: