Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Composition

Composition:
 Composition is referred to building a complex thing with the use of smaller and simple parts.
 the way of implementing complex objects using simpler or smaller ones.
 The object composition concept work on the model of has-a relationship among two
different objects.
 Complex objects are often referred to as parent components or whole components while
objects which are simpler or smaller are often referred to as child components or part
components.
 it gives us the freedom to design complex classes by using simpler and smaller manageable
parts.
 composition in an uncomplicated way by accessing the classes as a member function in
other classes.

 In object composition, the object created is a part of another object which is referred to as
a sub-object. A Sub-object is destroyed when an object composition is destroyed, known as
a do and die relationship.
Types of Object Composition in C++
Object composition is basically of the following subtypes:
 Composition: composition relationship is also called a part-whole relationship in which the part
component can only be a part of a single object at the same time.
 In composition relationships, the part component will be created when the object is created and the
part will be destroyed when the object is destroyed.
 A person's body and heart is a good example of a part-whole relationship where if a heart is part of a
person's body then it cannot be a part of someone else's body at one time.

To be qualified as a composition, the part and object must follow the relationships described below:
 The part component or child component (also referred to as a member) belongs to a single
object (also referred to as class).
 The part component can show its presence with the help of an object.
 The part(member) component is the element of the object.
 The part component has no idea about the presence of the object.
 There are certain sets of rules for creating and destroying parts of the object:
 There is no need to create a part (member) of the objects until it is not required.
 The composition should use the part that was given to it as an input rather than
completely creating a part by itself.
 The composition can assign the part's destruction to any other object.
Aggregation :
 the part component can belong to more than one object at the same time.
 It is also a part-whole relationship.
 The object(class) will not be responsible for the existence or presence of the parts.
To be qualified as aggregation, the part and object must follow the relationship
describes below:
 The part component or child component (also referred to as a member) belongs to
more than one object (also referred to as a class) at the same time.
 The part component does not show its presence with the help of an object.
 The part(member) component is the element of the object.
 The part component does not have an idea about the presence of the object.
Object Delegation :

 Object delegation is a process in which we use the objects of a class as a member of


another class.
 Object delegation is the passing of work from one object to the other one.
 It is an alternative to the process of inheritance.
 But when the concept of inheritance is used in the program it shows an is-
a relationship between two different classes.
 There does not exist any relationship between different classes.
Syntax:
class A
{
// Body of class A
};
class B
{
//Body of class B
A obj1;
public: B(arguments1) : obj1(arguments2); };
int main()
{
}
#include <iostream>
using namespace std; Sum of 10 and 50 = 60
class A { Sum of 10 and 10 = 20
private: int a;
public: void setValue(int b)
{ a = b;
}
void getSum(int c)
{
cout << "Sum of " << a << " and " << c << " = " << a + c <<
endl;
} };
class B {
public: A x;
void showResult()
{ x.getSum(10);
} };
int main()
{
B obj1;
obj1.x.setValue(10);
obj1.x.getSum(50);
obj1.showResult();
}
#include <iostream>
using namespace std; The constructor of Class A is invoked
class A { Data of class B object is 25
public: int data; Data of class A object in class B is 25
A(){
data = 0;
} A(int a)
{ cout << "Constructor of Class A is invoked" << endl;
data = a; } };
class B {
private: int b;
A obj2;
public: B(int a) : obj2(a) //Constructor of class B
{ b = a; } //printing values of data members
void printData()
{ cout << "Data of class B object is " << b << endl;
cout << "Data of class A object in class B is " <<
obj2.data;
} };
int main()
{ B obj1(25); //object of class B
obj1.printData();
return 0; }
#include <iostream.h>
using namespace std;
class Student {
public: string Name;
float ID;
Student(string Name, float ID){
this-> Name = Name;
this-> ID = ID; Student Class : 6
} }; Student Section : A
class Details { Student Name : Ali
private: Student* std; Student ID : 1
public: int stdclass;
string section;
Details(int stdclass, string section, Student* std) {
this->stdclass = stdclass;
this->section = section;
this->std = std; }
void detail()
{ cout << "Student Class : " << stdclass << endl;
cout << "Student Section : " << section << endl;
cout << "Student Name : " << std->Name<< endl;
cout << "Student ID : "<< std->ID << endl; } };
int main() {
Student s1 = Student(”Ali", 1);
Details d1 = Details(6, "A", &s1);
d1.detail();
return 0; }
#include <iostream>
using namespace std;
class A {
public: void show()
{ cout << "show() of Class A is invoked";
} };
class B {
A obj2;
public: void show()
{ obj2.show();
} };
int main() { B obj1; //Object of class B is created
obj1.show();
return 0; }

show() of Class A is invoked


Conclusion
 The Composition in C++ is defined as the way of implementing complex objects using the simpler or smaller
ones.
 In real life when we look around in our surroundings, we will find different things built using several small
components, for example, a laptop is constructed using main memory (RAM), secondary memory (Hard
Drive), processor, etc.
 Composition in C++ is also referred to as object composition.
 The object composition concept work on the model of has-a relationship among two different objects.
 Complex objects are often referred to as parent components while objects which are simpler or smaller are
often referred to as child components.
 Object composition allows us to reduce the overall complexity of the program and also allows us to write
code error-free and with more speed.
 Object composition is basically of the following subtypes:
• Composition
• Association
• Delegation
 Composition relationship is also called a part-whole relationship in which the part component can only be a
part of a single object at the same time.
 Association is also a part-whole relationship; its part can belong to more than one object simultaneously.
 Delegation is a process in which we use the objects of a class as a member of another class.
 Association:is a relationship. Inheritance
 Aggregation:has a relationship. Uni has café, doctor has patient
 Composition:part of relationship, strong relationship with child, car has
wheels.

You might also like