C++

What are Container Classes C++?

A container class as the name suggests is used to contain different values, objects, and variables, etc. in the memory or the external storage. A container class supports other classes present in the programs and it functions to hide the objects/variables used in the memory. It stores many items and all of these items are easily accessible by other members of the program.

All container classes access the elements of the container efficiently through the iterators. This class is known to hold some similar and mixed objects in the memory. A container can be of a homogeneous or heterogeneous type. If the container holds mixed objects then it is heterogeneous, while in the case of similar items it is known as homogeneous container class.

We are going to explain this concept on the Linux operating system, so you need to have Ubuntu installed and in the running form on your system. So you must install Virtual Box and after downloading and installation now configure it. Now add the Ubuntu file to it. You can access Ubuntu’s official website, and download the file according to your system requirement and operating system. It will take hours, then after installation, configure it on the virtual machine. In the configuration process, make sure you have created the user because it is essential for any operation on the Ubuntu terminal. Moreover, Ubuntu needs the authentication of the user before doing any installation.

We have used the 20.04 version of Ubuntu; you may use the latest one. For the implementation, you need to have a text editor and must have access to the Linux terminal, because we will be able to see the output of the source codes on the terminal through the query. The user must have basic knowledge of C++ and object-oriented programming to make use of classes in the program.

Container Class/Containership and Its Types

A class is said to be a container when it is used to hold values that are utilized by other variables in the same program. A GUI class library has a group of container classes. The pointers containers provide the containers that hold the objects that are allocated through safe heap selection. This usage of container class has the purpose to make OOP very easy in the C++ language. This is done when a standard set of the class is established.

The type of relationship between classes is known as containership. The class that contains this kind of relationship is the container class. Similarly, the object is known as a container object.

C++ Standard Container Classes

The standard classes are described as follow:

  • Std::map: This is used to handle an array or a sparse matrix.
  • Std::vector: Just like an array, the container classes have additional features like inserting and removing elements, memory management automatically, and throw exceptions.
  • Std::string: It is an array of characters.

Syntax of Containership

// Class that is to be contained

class One {

};

// Container class

class Two {

// creating object of One

    One O;

};

Example 1

In this example, we have created a container class with the name second. Firstly the library is used to make the file enabled to read and write. The first class contains a function named display that is used to display the message when the function is called. In the public part of the class, there is a constructor that calls the function of the first class, and then the value is displayed. These all steps can be shortened by doing these functions in a single class but as we are discussing the concept of a container, that’s why each function is performed separately in an individual class.

# include <iostream>

Coming back to the program, in the main program, we have created the object of the second class. By creating this object, the second class will automatically be called, and after calling the constructor will be initiated that will call the first class and the statement will be displayed from the function.

To see the resultant value of the code, we will go to the Ubuntu terminal. To compile the code, we use a compiler, for C++ we use a G++ compiler to compile the code.

$ G++ -o con con.c

$ ./con

By executing the code you will see that the function will be executed and the statement will be displayed.

Example 2

This example resembles the first one, only the difference is that this time we have not inherited the first class into the second one by creating the object. But a message is displayed in the class. The first class has a public portion that contains a constructor directly, but not the display function. This is because we are not willing to make a relation of first-class with the second. We have not made a function call. The object of the second class is created in the main program.

Again, go to the terminal and use the same command, this will display two messages, one from the first class, and the other from the second class.  This example shows that containers can also be used without interaction with other functions.

Example 3

Now in this example, we have not simply displayed any message, a number is given in the constructor of the container class, and then it is displayed in the first class. The first class uses a variable to accept the variable, and display the number with it. Another function is used here that returns the number. Now going to the second class, the constructor uses the object of the first class created in the second class.

By using the object, it calls both the functions that are present in the first class, the display function and the get number function. The getting number() function uses the number and passes it to the function.

# f.getnum() = 50;

# f.display();

After that in the main function, the class second object is created only. On the creation of the second class object, the constructor of the second class is automatically executed. On executing the result on the terminal, you will come to know that the number you entered in the container class is displayed.

Now, these were the examples we have used to explain the concept of a container class. There is confusion between inheritance and containership.

In containership, features of the class are inside the new class, but not in the inheritance, like that class is not the child class. For instance, mobile has an android, and the truck has an engine. On the other hand, in the case of inheritance, when we want a new type of class having the features of the base class, this is inheritance.

Conclusion

The concept of container class is explained by giving examples on the Linux operating system by configuring Ubuntu. The container class acts like the array data type to contain values in it that are usable and accessible through the other objects in the same or different cases. By using container class, the user should be aware of the difference between inheritance and containership. In the given article, we have aimed to describe the basics of container class, and we hope that this knowledge is sufficient to be up to the mark.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.