What Is Containership In C++ - C++ Programming Tutorial
C++ Course / STL Basics / What Is Containership In C++

What Is Containership In C++

BLUF: Mastering What Is Containership In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: What Is Containership In C++

C++ is renowned for its efficiency. Learn how What Is Containership In C++ enables low-level control and high-performance computing in the tutorial below.

A fundamental concept in object-oriented programming known as containment allows for the creation of complex data structures using basic ones. Containment involves placing one object within another, facilitated by utilizing object-oriented features that support the establishment of intricate object relationships. This blog post will explore the concept of C++ containment, covering its syntax, providing sample code, and discussing the outcomes.

When employing the C++ containership syntax, a class includes another class as one of its member variables. This is achieved by initializing an object within the constructor, following its declaration as an internal variable of the class. Below is a visual representation:

Example

class ContainerClass  {
private:
OtherClass* otherObject;
public:
ContainerClass() {
otherObject = new OtherClass();
}
~ContainerClass() {
delete otherObject;
}
};

This illustration utilizes a pointer (OtherClass*) as an internal variable to include OtherClass within ContainerClass. The constructor initializes another Object by utilizing the new keyword to generate a new instance of OtherClass. By using the delete keyword, the destructor releases the memory that otherObject utilized.

Example Code

Let's explore a C++ containment scenario. In this instance, the Rectangle class will include a Point class as one of its member variables. The Point class defines a point in a two-dimensional space with x and y coordinates. The Rectangle class utilizes points to represent the top-left corner, width, and height of a two-dimensional rectangle.

Example

#include <iostream>
 using namespace std;
 class Point {
    private:
        int x, y ;

    public:
Point (int x, int y) {
            this -> x = x;
            this -> y = y;
        }

        int getX() {
            return x;
        }

        int getY() {
            return y;
        }
} ;

class Rectangle {
    private:
        Point* topLeft;
        int width, height;

    public:
Rectangle (int x, int y, int width, int height) {
topLeft = new Point (x, y);
            this -> width = width;
            this -> height = height;
        }

        ~Rectangle() {
            delete topLeft;
        }

        int getWidth() {
            return width;
        }

        int getHeight() {
            return height;
        }

        Point* getTopLeft() {
            return topLeft;
        }
};

int main () {
    Rectangle rect(10, 20, 30, 40);
    Point* topLeft = rect.getTopLeft();

cout<< "Rectangle width: " <<rect.getWidth() <<endl;
cout<< "Rectangle height: " <<rect.getHeight() <<endl;
cout<< "Rectangle top-left corner coordinates: (" <<topLeft->getX() << ", " <<topLeft->getY() << ")" <<endl;

    return 0;
}

Output:

Output

Rectangle width: 30
Rectangle height: 40
Rectangle top-left corner coordinates: (10, 20)

Explanation:

In this diagram, the Point class defines a point using x and y coordinates within a two-dimensional plane. Points in the Rectangle class symbolize the top-left corner, width, and height of a rectangle in a two-dimensional space. The Rectangle class includes a Point as one of its attributes, which is set during object creation.

We instantiate a Rectangle instance within the main function, setting its origin at coordinates (10, 20), a width of 30, and a height of 40. Subsequently, the dimensions of the width, height, and top-left corner are displayed by invoking the getTopLeft method to retrieve the "topLefcpptutorialobject".

Conclusion:

A fundamental concept in object-oriented programming known as containership allows the creation of complex data structures from simpler ones. In objeccpp tutorials, containership is achieved in C++ by encapsulating one object within another. This article delved into the syntax of C++ containership and explored a sample program illustrating the use of containership to construct a class named Rectangle with a Point class as a constituent variable. The results of the provided code demonstrated the successful instantiation and initialization of the Rectangle object. Understanding containership and incorporating it into our code enables the development of more intricate and powerful C++ applications.

Containership facilitates the reuse of code and modularity while also empowering the creation of intricate data structures. By creating smaller classes that can serve as variable members within larger classes, we can enhance the modularity and maintainability of our code. Moreover, containership can optimize memory usage and improve data locality, potentially leading to faster code execution. When applied effectively, containership can serve as a powerful asset in developing efficient and top-quality C++ programs.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience