×

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

C++ cin and cout

In this article, we will discuss the C++ cin and cout with their library and examples. C++ Standard Input/Output: User-program communication is made possible by C++’s usage of input and output (I/O)...

5 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

Object in C++

In this article, we will learn about Object in C++. In short, an object is a stateful entity with behaviour. Data is referred to as state, and functionality is referred to...

3 minutes read.

abs() function in C++

In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned...

4 minutes read.

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.

C++ Namespaces

An Overview In each scope, a name can only represent one entity. As a result, there cannot be two independent variables with the similar names in the same scope, as this may cause...

10 minutes read.

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

Ways to Copy a Vector in C++

Vectors in C++ are the same as arrays, along with additional outstanding features than them, like array lists in Java programming language. In Vectors, the size constraint is eliminated, which...

5 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

Scope Resolution Operator vs this Pointer in C++

In this tutorial, we will compare the Scope Resolution operation to this Pointer in C++ language. Scope Resolution Operator The Scope Resolution Operator in C++ programming language is usually denoted by (::)....

3 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

C++ Reading file

In file handling, read() function is used to read data from the file into the program. The read() uses ifstream library to read data from a file. Syntax file-stream-class   file-stream-object;   file-stream-object.read((char *)&var , sizeof (var)); <h3">Example ofstream  outfile;         outfile . read((char*)&emp,sizeof(emp)); C++ File Handling read() Function Example Reading the content of existing...

2 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

C++ Program: Matrix Multiplication

Matrix Multiplication in C++ What is a Matrix? A matrix is a set of numbers in the form of rows and columns forming a rectangular array. It includes numbers, which are often...

4 minutes read.

Loops in C++

A loop statement in most programming languages allows us to execute a statement or a collection of statements numerous times. Control structures of programming languages vary, allowing for more complex...

6 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.