Composition (aggregation)
Composition (aggregation):
• Composition (aggregation)is a way to relate two classes.
• In composition (aggregation), one or more members of a class are
objects of another class type.
• It is another way to reuse the class. It is a form of association that
represents HAS-A relationship
Aggregation :
“Aggregation occurs when a class contains an instance of another class. “
• In real life, objects are frequently made of other objects.
• A house, for example, is made of door objects, window objects, wall
objects, and much more.
• It is the combination of all these objects that makes a house object
• When designing software, it sometimes makes sense to create an object
from other objects.
• For example, to create a Course class, hold the following information:
The course name
The instructor’s last name, first name, and office number
The textbook’s title, author, and publisher
• A good design principle is to separate related items into their own
classes.
• In this example, an Instructor class could be created to hold the
instructor-related data
• A TextBook class could be created to hold the textbook-related data.
objects of these classes could then be used as attributes(members)
in the Course class.
The code for the Instructor class
class Instructor {
private:
string lastName; // Last name
string firstName; // First name
string officeNumber; // Office number
public:
Instructor()
{ set("", "", ""); }
Instructor(string lname, string fname, string office)
{set(lname, fname, office);}
void set(string lname, string fname, string office)
{ lastName = lname;
firstName = fname;
officeNumber = office; }
void print() const
{ cout << "Last name: " << lastName << endl;
cout << "First name: " << firstName << endl;
cout << "Office number: " << officeNumber << endl; } } ;
The code for the TextBook class
• Class TextBook{
• private:
• string title; // Book title
• string author; // Author name
• string publisher; // Publisher name
• public:
• TextBook() {
• set("", "", ""); }
• TextBook(string textTitle, string auth, string pub) {
• set(textTitle, auth, pub); }
• void set(string textTitle, string auth, string pub)
• {title = textTitle; author = auth; publisher = pub; }
• void print() const {
• cout << "Title: " << title << endl;
• cout << "Author: " << author << endl;
• cout << "Publisher: " << publisher << endl; } };
class Course
{
private:
string courseName; // Course name
Instructor instructor; // Instructor
TextBook textbook; // Textbook
public:
Course(string course, string instrLastName, string instrFirstName, string instrOffice,
string textTitle, string author, string publisher)
{courseName = course;
[Link](instrLastName, instrFirstName, instrOffice); //Assign the instructor.
[Link](textTitle, author, publisher); // Assign the textbook
}
void print() const
{ cout << "Course name: " << courseName << endl << endl;
cout << "Instructor Information:\n";
[Link]();
cout << "\nTextbook Information:\n";
[Link]();
cout << endl;}
};
int main()
{
// Create a Course object.
Course myCourse("Intro to Computer Science", // Course name
"Kramer", "Shawn", "RH3010", // Instructor info
"Starting Out with C++", "Gaddis", // Textbook title and author
"Addison-Wesley"); // Textbook publisher
// Display the course info.
[Link]();
return 0;
}
Usage and Concept of Aggregation
in C++
• The concept of aggregation is based on real-world scenarios,
which include many different classes sharing a common attribute.
• For example, consider the class ‘address,’ along with a group of
classes of professions, like ‘student,’ ‘scientist,’ ‘programmer’ etc.
• Each of the objects of the classes would have an attribute for their
address.
• Hence, each class would contain an object of the ‘address’ class
class address{
public:
string city, state, locality;
address( string city_c, string state_c, string locality_c)
{
city = city_c; state = state_c; locality = locality_c;
}
};
class student{
address *adrs;
public:
int id;
string name;
student( int i, string nm, address*ad)
{ id = i; name = nm; adrs = ad; }
void display()
{
cout<< id<< " "<< name << " "<< adrs->locality << " “
<< adrs->city << " "<< adrs->state;}
};
int main()
{
address a1("C-146, Sec-15","Noida","UP");
student s1(101,"Nakul",&a1);
[Link]();
return 0;
}
The output is given by :
101 Nakul C-146, Sec-15 Noida UP
Class Member Access Operator :
• Arrow operator (->) in C++ also known as Class Member Access Operator
• Is a combination of two different operators that is Minus
operator (-) and greater than operator (>).
• It is used to access the public members of a class, or structure, with the
help of a pointer variable