×

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a containership. We can also call it has_a relationship. The class containing the object in one class into another is known as the container class. The object that is present in the container class is known as a contained object, and the object contains another object known as a container object. 

The iterator method can access all the objects available in the container class. The container class has the capacity to hold mixed and similar objects in its memory. A container class may be heterogeneous or homogeneous type. The main usage of the containership is to make the OOPs concept very easy for the C++ language. 

The C++ standard container class has the following:

  1. Std::map: It is used to handle the array matrix. 
  2. Std::vector: It provides some additional features like adding and removing the object from the container class. 
  3. Std::string: It is the collection of characters in the array. 

Syntax for the containership class:

class first {
    .
    .
};


class second {


    first f;
    .
    .
};

Difference between Containership and Inheritance

Containership: Containership is a process in which one class uses the definition of another class. When a class gets the details of another class then the class is called containment, composition, or aggregation. The data member of the class becomes the object of the new class. Hence, the other class is known as containership. The composition is also called a has_a relationship. 

Inheritance: Inheritance is a type of process in which a new class is derived from an old class. The reusability of the code is supported by inheritance. With the help of inheritance, some additional features can be added to a new class. Once a class is written, it is not possible to write it again. It is also called an is_a relationship. 

Example 1:

#include <iostream>
using namespace std;


class first {
public:
           void showf()
           {
                       cout << "Hello, it is the output from first class\n";
           }
}
class second {
           first f;


public
           second()
           {
                       f.showf();
           }
};


int main()
{
           second s;
}

Output:

Hello, it is the output from first class

Explanation

In the above program, the second class has the object of the first class. This is another type of inheritance. This type of inheritance is known as a has_a relationship.

Example 2:

#include <iostream>
using namespace std;


class first {
public:
           first()
           {
                       cout << "Hello, it is the output from first class\n";
           }
};
class second {


           first f;


public:


           second()
           {
                       cout << "Hello, it is the output from second class\n";
           }
};


int main()
{


           second s;
}

Output:

Hello, it is the output from first class
Hello, it is the output from second class

Explanation

In this program, the second class is not inherited from the first class. But, we have the object of the first class in the second class. The default constructer of the first class is known as the first, and the second class's constructer is called the second. 

Example 3:

#include <iostream>
using namespace std;


class first {
private:
           int num;


public:
           void showf()
           {
                       cout << "Hello, it is the output from first class\n";
                       cout << "it's num value = " << num << endl;
           }


           int& getnum()
           {
                       return num;
           }
};
class second {
           first f;


public:
           
           second()
           {
                       f.getnum() = 50;
                       f.showf();
           }
};


int main()
{
           
           second s;
}

Output:

Hello, it is the output from first class
it's num value = 50

Explanation

In this program, in containership, we can define the class only publically, not protected or private. They can get the value of the first class with the help of the getnum function.

Example 4:

#include<iostream>
using namespace std;


class cDate
{
	int mDay,mMonth,mYear;
public:
	cDate()
	{
		mDay = 1;
		mMonth = 02;
		mYear = 1998;
	}
	cDate(int d,int m ,int y)
	{
		mDay = d;
		mMonth = m;
		mYear = y;
	}
	void display()
	{
		cout << "day" << mDay << endl;
		cout <<"Month" << mMonth << endl;
		cout << "Year" << mYear << endl;
	}
};


class cEmployee		
{
protected:
	int mId;
	int mBasicSal;
	cDate mBdate;	
public:
	cEmployee()
	{
		mId = 5;
		mBasicSal = 30000;
		mBdate = cDate();
	}
	cEmployee(int, int, int, int, int);
	void display();
};


cEmployee :: cEmployee(int i, int sal, int d, int m, int y)
{
	mId = i;
	mBasicSal = sal;
	mBdate = cDate(d,m,y);
}
void cEmployee::display()
{
	cout << "Id : " << mId << endl;
	cout << "Salary :" <<mBasicSal << endl;
	mBdate.display();
}




int main()
{


	cEmployee e1;		
	e1.display();
	cEmployee e2(2,20000,11,11,1999);
	e2.display();
	return 0;
}

Output:

Containership in C++

Related Topics

How to Reverse a String in C++ using While Loop

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer...

6 minutes read.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.

C++ Structs

We frequently encounter scenarios in which we must store a bunch of data, whether of comparable or dissimilar data kinds. Arrays are used to hold a group of data of...

6 minutes read.

Passing a Vector to a function in C++

A pointer is sent to the function when we feed it an array. However, there are two ways to pass a vector: Pass by Value Pass by Reference A copy of a vector...

3 minutes read.

C++ Bitwise XOR Operator

Exclusive OR is another name for the bitwise XOR operator. The ‘^’ is used to indicate it. It operates at the bit level of the operands, as the name implies....

4 minutes read.

Vector Size in C++

What are Vectors?  In the C++ programming language, vectors are run-time sequence containers representing arrays with variable sizes, which are contained within STL (Standard Template Library). They utilize contiguous storage spaces...

6 minutes read.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

4 minutes read.

CPP Templates

C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using...

4 minutes read.

Pthread in C++ Parameters

Pthreads, also known as POSIX threads, is a POSIX standard for multithreading in C/C++. It allows a program to control multiple different threads of execution concurrently. Using pthreads, you can create...

4 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.

Skyline Problem in C++

We have given n rectangular buildings in a 2-dimensional city. Here, to compute the Skyline of the given n rectangle structures in a two-dimensional metropolis while removing hidden lines, the...

3 minutes read.

Tensorflow in C++

With cppflow, you can execute TensorFlow models in C++ without Bazel, TensorFlow installation, or TensorFlow compilation. Tensor manipulation, eager execution, and running stored models straight from C++ are all possible. Knowing...

7 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

goto statement in C and C++

goto statement in C and C++ The goto statement is a jump statement, also sometimes referred to as an unconditional jump statement. Within a function, the goto statement can be used...

3 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

Iostream in C++

Using Iostream in C++, we can perform input and output operation capabilities. This represents input and output, and the stream is used to carry out this capability. A stream is...

4 minutes read.

Size_t Data Type in C++

In C++, the type to express the object size in bytes is defined as Size_t, an unsigned integer type offered by the standard library for describing the object's size and...

3 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

Const Keyword in C++

The const programming language keyword will be covered in this section. The constant value that cannot change during program execution is defined using the const keywords. It implies that once...

9 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.