0% found this document useful (0 votes)
57 views

OOP UE LEC Composition and Aggregation

The document discusses object oriented programming concepts of composition and aggregation. Composition is a stronger relationship where the composed object becomes part of the composer and cannot exist independently. Aggregation is a weaker relationship where the aggregate object is not part of the container and can exist independently. An example program is provided to practice these concepts by modeling a clinic with doctors and patients using composition or aggregation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

OOP UE LEC Composition and Aggregation

The document discusses object oriented programming concepts of composition and aggregation. Composition is a stronger relationship where the composed object becomes part of the composer and cannot exist independently. Aggregation is a weaker relationship where the aggregate object is not part of the container and can exist independently. An example program is provided to practice these concepts by modeling a clinic with doctors and patients using composition or aggregation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Object Oriented Programming

Composition and Aggregation


Composition
• An object may be composed of other smaller objects, the relationship
between the “part” objects and the “whole” object is known as
composition.
• Composition is stronger relationship.
• Composition is a stronger relationship, because
- the composed object becomes a part of the composer.
- the composed object can’t exist independently.
class Car{
class Engine{ private:
private: string car_num;
string eng_num; Engine engine;
public:
public: Car(){
void set_eng_num(string num) car_num="111";
{ engine.set_eng_num("222");
eng_num = num; }
} void show_Car(){
string get_eng_num(){ cout<<car_num;
return eng_num;} cout<<engine.get_eng_num();
}
}; };
main(){
Car c1;
c1.show_Car();
}
Aggregation
• An object may contain a collection (aggregate) of other objects, the
relationship between the container and the contained object is called
aggregation.
• Aggregation is represented by a line with unfilled-diamond head
towards the container
class Address{ class Student{
private: private:
int house_num; string name;
int street_num; Address *address;
public:
void set_house_num(int i) public:
{ Student(Address address1, string name1)
house_num = i; {
} name=name1;
int get_house_num(){ address=&address1;
return house_num; address->set_house_num(1);
} address->set_street_num(5);
void set_street_num(int i) }
{ void show()
street_num = i; {
} cout<<name;
int get_street_num(){ cout<< address->get_house_num();
return street_num; cout<< address->get_street_num();
}
}
}; };
int main(){
string name1= "student's name ";
Address address2;

Student s1(address2, name1);


s1.show();

return 0;
}
• Aggregation is weaker relationship.
• Aggregation is weaker relationship, because aggregate object is not a
part of the container.
• Aggregate object can exist independently.
Class exercise
• Write a C++ program that has a class named Clinic which has at least three
doctors and ten patients. The clinic class also keeps the information of clinic’s
name and address. Each patient and doctor is allotted an Id which is
generated order-wise, e.g., the patient 1 is allotted the id 1, patient 2 is
allotted id 2, and so on. Similarly, the doctors are also allotted the Ids.
• The information required to be kept for a doctor include doctor’s name, Id,
and his/her specialization while the information related to a patient include
patient’s name, Id, age, and disease.
• The class clinic should take input of 3 doctors and 10 patients from user and
display that on screen.
• Implement the given problem by applying aggregation/composition.
Note: recall aggregation/composition, array of objects, constructor, static
members.
References
• C++ How to Program
By Deitel & Deitel
• The C++ Programming Language
By Bjarne Stroustrup
• Object oriented programming using C++ by Tasleem Mustafa, Imran Saeed, Tariq
Mehmood, Ahsan Raza
• https://www.tutorialspoint.com/cplusplus
• http://ecomputernotes.com/cpp/introduction-to-oop
• https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
• https://www.guru99.com/c-loop-statement.html
• www.w3schools.com
• https://www.geeksforgeeks.org/

You might also like