The Wayback Machine - https://web.archive.org/web/20240829143003/https://www.geeksforgeeks.org/introduction-to-complex-objects-and-composition/
Open In App

Introduction to Complex Objects and Composition

Last Updated : 29 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

An object is a basic unit of Object-Oriented Programming and represents the real-life entities. Complex objects are the objects that are built from smaller or a collection of objects. For example, a mobile phone is made up of various objects like a camera, battery, screen, sensors, etc. In this article, we will understand the use and implementation of a complex object. 
In object-oriented programming languages, object composition is used for objects that have a “has-a” relationship with each other. For example, a mobile has-a battery, has-a sensor, has-a screen, etc. Therefore, the complex object is called the whole or a parent object whereas a simpler object is often referred to as a child object. In this case, all the objects or components are the child objects which together make up the complex object(mobile). 

Classes that have data members of built-in type work really well for simple classes. However, in real-world programming, the product or software comprises of many different smaller objects and classes. In the above example, the mobile phone consisted of various different objects that on a whole made up a complete mobile phone. Since each of those objects performs a different task, they all are maintained in different classes. Therefore, this concept of complex objects is being used in most of the real-world scenarios. The advantages of using this concept are: 

  • Each individual class can be simple and straightforward.
  • One class can focus on performing one specific task and obtain one behavior.
  • The class is easier to write, debug, understand, and usable by other programmers.
  • While simpler classes can perform all the operations, the complex class can be designed to coordinate the data flow between the simpler classes.
  • It lowers the overall complexity of the complex object because the main, task of the complex object would then be to delegate tasks to the sub-objects, who already know how to do them.

The most important advantage is if any changes have to be made in a child class, only the child class can be changed rather than changing the entire parent class.

  • For example, if we want to change the battery class in the mobile object, with the help of composition, the changes are only made in the battery class and the entire mobile object works fine with the updated changes.

Scope of Use: Although there are no well-defined rules to state when a programmer must use the composition, as a rule of thumb, each class should be built to accomplish a single task. The task should be to either perform some part of manipulation or be responsible for coordinating other classes but cannot perform both tasks. This immensely increases the maintenance of the code and future updations because, when we wish to update a feature or an object, only the class pertaining to that specific functionality needs to be updated. And also, it’s very easy to keep track of the errors in the program. For example:

C++




// C++ program to implement a
// composition
 
// A point class
class Point {
private:
    int x;
    int y;
};
 
// Every location has a point.
// Every point has two coordinates.
// The above class's functionality
// is only to store the coordinates
// in two variables. Any functionality
// using the points is implemented
// here
class Location {
    Private : Point Source;
    Point Destination
};


In the classes given here, Location uses objects of class Point as its data members. Hence, Location is a complex class which uses a simple class Point. Let us have a look at the program that makes use of the composition.

Below is the implementation of a composite class:

C++




// C++ program to implement a
// composite class
using namespace std;
#include <iostream>
 
// Class with a private parameter
// and the getters and setters
class One {
 
    // Private parameter
private:
    int num;
 
    // Public setter and getter
public:
    void set(int i)
    {
        num = i;
    }
    int get()
    {
        return num;
    }
};
 
// Another class
class Two {
 
    // Public method and the object
    // of the previous class
public:
    One O;
    void show()
    {
        cout << "\n Number = "
             << O.get();
    }
};
 
// Driver code
int main()
{
    // Creating the object of
    // class Two
    Two T;
 
    // Perform some operation using
    // the object of One in this class
    T.O.set(100);
    T.show();
}


Output: 

Number = 100

 

Explanation: Note that in the program, class Two has an object of class One. To access a member of One, we must use the object of Two as in T.O.set(100). Moreover, since num is a private member of One, we must use a public function to access its value.
 



Similar Reads

Object Composition-Delegation in C++ with Examples
Object Compositions An object is a basic unit of Object-Oriented Programming and represents real-life entities. Complex objects are objects that are built from smaller or a collection of objects. For example, a mobile phone is made up of various objects like a camera, battery, screen, sensors, etc. This process of building complex objects from simp
5 min read
asin() function for complex number in C++
The asin() function for complex numbers is defined in the complex header file. This function is the complex version of the asin() function. This function is used to calculate the complex arc sine of complex number z and returns the arc sine of complex number z. Syntax: template&lt;class T&gt; complex&lt;T&gt; asin (const complex&lt;T&gt;&amp; z );
2 min read
atanh() function for complex number in C++
The atanh() function for complex number is defined in the complex header file. This function is the complex version of the atanh() function. This function is used to calculate the complex arc hyperbolic tangent of complex number z and returns the arc hyperbolic tangent of complex number z. Syntax: template&lt;class T&gt; complex&lt;T&gt; atanh (con
2 min read
acos() function for complex number in C++
The acos() function for complex number is defined in the complex header file. This function is the complex version of the acos() function. This function is used to calculate the complex arc cosine of complex number z and returns the arc cosine of complex number z. Syntax: template&lt;class T&gt; complex&lt;T&gt; acos (const complex&lt;T&gt;&amp; z
2 min read
asinh() function for complex number in C++
The asinh() function for complex numbers is defined in the complex header file. This function is the complex version of the asinh() function. This function is used to calculate the complex arc hyperbolic sine of complex number z and returns the arc hyperbolic sine of complex number z. Syntax: template&lt;class T&gt; complex&lt;T&gt; asinh (const co
2 min read
log10() function for complex number in C++
The log10() function for complex numbers is defined in the complex header file. This function is the complex version of the log10() function. This function is used to calculate the complex common log of a complex number z i.e with base 10 and returns the common log of complex number z. Syntax: template&lt;class T&gt; complex&lt;T&gt; log10 (const c
2 min read
atan() function for complex number in C++
The atan() function for complex numbers is defined in the complex header file. This function is the complex version of the atan() function. This function is used to calculate the complex arc tangent of complex number z and returns the arc tangent of complex number z. Syntax: template&lt;class T&gt; complex&lt;T&gt; atan (const complex&lt;T&gt;&amp;
2 min read
exp() function for complex number in C++
The exp() function for complex number is defined in the complex header file. This function is the complex version of the exp() function. This function is used to calculate the base e exponential of a complex number z and returns the base-e exponential of complex number z. Syntax: template&lt;class T&gt; complex&lt;T&gt; exp (const complex&lt;T&gt;
2 min read
acosh() function for complex number in C++
The acosh() function for complex number is defined in the complex header file. This function is the complex version of the acosh() function. This function is used to calculate the complex arc hyperbolic cosine of complex number z and returns the arc hyperbolic cosine of complex number z. Syntax: template&lt;class T&gt; complex&lt;T&gt; acosh (const
2 min read
pow() function for complex number in C++
The pow() function for complex number is defined in the complex header file. This function is the complex version of the pow() function. This function is used to calculate the complex power of base x raised to the y-th power. Syntax: template&lt;class T&gt; complex&lt;T&gt; pow (const complex&lt;T&gt;&amp; x, int y); or, template&lt;class T&gt; com
2 min read
cos() function for complex number in C++
The cos() function for a complex number is defined in the complex header file. This function is the complex version of the cos() function. This function is used to calculate the complex cosine of complex number z. This function returns the cos of the complex number z. Syntax: cos (z); Parameter: z: This method takes a mandatory parameter z which re
2 min read
tan() function for complex number in C++
The tan() function for complex numbers is defined in the complex header file. This function is the complex version of the tan() function. This function is used to calculate the complex tan of the complex number z. This function returns the tan of complex number z. Syntax: tan(z); Parameter: z: This method takes a mandatory parameter z which represe
2 min read
proj() function for Complex Numbers in C++
The proj() function is a built-in function and is defined in the complex header file. This function is used to find the projection of complex number [Tex]z [/Tex]onto the Riemann sphere. Syntax: template &lt;class T&gt; complex&lt;T&gt; proj (const complex&lt;T&gt;&amp; z); Parameter: z: This method takes a mandatory parameter z which represents th
2 min read
arg() function for Complex Number in C++
The arg() function for complex numbers is defined in the complex header file. This function is used to return the argument of the complex number z. Syntax: template&lt;class T&gt; T arg (const complex&lt;T&gt;&amp; z); Parameter: z: It represents the given complex number. Return Type: It returns the argument of the complex number. Below are program
2 min read
tanh() function for Complex Number in C++
The tanh() function for complex number is defined in the complex header file. This function is the complex version of the tanh() function of cmath header file. This function is used to calculate the complex hyperbolic tangent of the complex number z. Syntax: template&lt;class T&gt; complex&lt;T&gt; tanh (const complex&lt;T&gt;&amp; z );Parameter: z
2 min read
sinh() function for Complex Number in C++
The sinh() function is a built-in function in C++ defined in the complex header file. This function is the complex version of the sinh() function available in the cmath header file. This function is used to calculate the complex hyperbolic sine of a complex number z. Syntax: template&lt;class T&gt; complex&lt;T&gt; sinh (const complex&lt;T&gt;&amp;
2 min read
Geometry using Complex Numbers in C++ | Set 2
After going through previous post, we know what exactly are complex numbers and how we can use them to simulate points in a cartesian plane. Now, we will have an insight as to how to use the complex class from STL in C++.To use the complex class from STL we use #include &lt;complex&gt; Defining Point ClassWe can define our point class by typedef co
5 min read
Complex numbers in C++ | Set 1
The complex library implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them. real() - It returns the real part of the complex number.imag() - It returns the imaginary part of the complex number. [GFGTABS] CPP // Program illustrating the use of real() and // imag() function #
5 min read
Complex numbers in C++ | Set 2
We introduced and discussed the concept in Complex numbers in C++ | Set 1The remaining functions with example are discussed here: log() - It is used to return the log of the complex number. [GFGTABS] CPP // CPP program to illustrate the use of log() #include &lt;iostream&gt; // for std::complex, std::log #include &lt;complex&gt; using namespace std
3 min read
C++ Classes and Objects
In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. In this article, we will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program. What is a Class in C++?A class is a user-defined data type, which holds its own data members and member functions, w
9 min read
Static Objects in C++
Prerequisite: Static Keyword in C++ An object becomes static when a static keyword is used in its declaration. Static objects are initialized only once and live until the program terminates. They are allocated storage in the data segment or BSS segment of the memory. C++ supports two types of static objects: Local Static ObjectsGlobal Static Object
3 min read
Count the number of objects using Static member function
Prerequisite : Static variables , Static Functions Write a program to design a class having static member function named showcount() which has the property of displaying the number of objects created of the class. Explanation: In this program we are simply explaining the approach of static member function. We can define class members and member fun
2 min read
How to initialize Array of objects with parameterized constructors in C++
Array of Objects: When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects. Syntax: ClassName ObjectName[number of objects]; Different methods to initialize the Array of objects with parameterized constructors:
6 min read
Anonymous Objects in C++
Prerequisite: Class and Objects in C++ An Object is an instance of a Class. A class has no memory allocated it is just like a blueprint, but when it is instantiated (i.e. an object is created) memory is allocated just like the real-world instance of that blueprint. An object that does not have a reference variable is known as an anonymous object. T
4 min read
Creating a Vector of Class Objects in C++
Prerequisites: Object Oriented Programming in C++Vector in C++ Class is a user-defined data type that can be accessed by creating objects or instances of that class. A vector is a type of container which can store elements of a similar type. Vector of Class The vector of class objects is an example of a custom vector made where we can store multipl
3 min read
Reference to Dynamic Objects in C++
In C++, the objects can be created at run-time. C++ supports two operators new and delete to perform memory allocation and de-allocation. These types of objects are called dynamic objects. The new operator is used to create objects dynamically and the delete operator is used to delete objects dynamically. The dynamic objects can be created with the
3 min read
How to Parse an Array of Objects in C++ Using RapidJson?
RapidJSON is an open-source C++ library for parsing and serializing JSON (JavaScript Object Notation) data. It is designed to be fast and efficient, with a focus on simplicity and ease of use. It is widely used in a variety of applications and is known for its fast performance and low memory overhead. It is a popular choice for parsing and serializ
4 min read
How to make a C++ class whose objects can only be dynamically allocated?
The problem is to create a class such that the non-dynamic allocation of object causes compiler error. For example, create a class 'Test' with following rules. C/C++ Code Test t1; // Should generate compiler error Test *t3 = new Test; // Should work fine The idea is to create a private destructor in the class. When we make a private destructor, the
2 min read
How to Restrict Dynamic Allocation of Objects in C++?
C++ programming language allows both auto(or stack-allocated) and dynamically allocated objects. In Java &amp; C#, all objects must be dynamically allocated using new. C++ supports stack-allocated objects for the reason of runtime efficiency. Stack-based objects are implicitly managed by the C++ compiler. They are destroyed when they go out of scop
2 min read
Comparing String objects using Relational Operators in C++
If strings are compared using relational operators then, their characters are compared lexicographically according to the current character traits, means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered. Parameters : Two Strings required t
2 min read
Article Tags :
Practice Tags :