Cramster.com - Homework Solutions, Lecture Notes, Exams, and Free Online Homework Help
Sign Up Now! Login Customer Support
McAfee Secure sites help keep you safe from identity theft, credit card fraud, spyware, spam, viruses and online scams
Problem Solved.
    Home    
    Homework Help    
   Answer Board   
    Resources (Beta)    
   
Member's Topic Headline:

Object oriented programming

Know the answer? Have a better solution? Share it.
Get Help Now.
View homework problems
explained for free!
Member Testimonials

Question:

Advertisement:

Answer | Ask New Question | Customize Profile | Leaderboards | 
FAQ

Member's Avatar

Rookie
Karma Points: 4
Respect (68%):
Date Posted: 11/7/2007 11:31:36 AM  Status: Live
Object oriented programming
Course Textbook Chapter Problem
N/A N/A N/A N/A
Question Details:
 

Explain the difference between composition and aggregation with sample code.

Answers:

Member's Avatar

Novice
Karma Points: 31
Date Posted: 11/7/2007 11:53:23 AM  Status: Live
Asker's Rating: Helpful   
Response:
 

Composition

 

The composition is a strong relationship between classes i.e. both classes are dependent on each other for proper functioning. As we know, inheritance gives us an 'is-a' relationship. To make the understanding of composition easier, we can say that composition gives us a 'part-of' relationship. Composition is shown on a UML diagram as a filled diamond (see Figure).



 

Example:

We can take the example of computer and CPU. Both Computer and CPU are two separate classes but a CPU is of know use of without Computer and Computer cannot function without CPU. So there is a strong relationship here that will be represented by composition. Similarly, The Car and Engine is composition as Car cannot work without Engine and Engine is of no use without Car.

 

Aggregation

In case of Aggregation, the container and contained object can exists independently and are not dependent on each other. For example the Car and Garage, A garage has Car. But if Car is not present in Garage still Car can function independent of Garage. Both are separate and not dependent on each other. So this relationship is Aggregation.

If inheritance gives us 'is-a' and composition gives us 'part-of', we could argue that aggregation gives us a 'has-a' relationship. Within aggregation, the lifetime of the part is not managed by the whole. To make this clearer, we need an example. For the past 12+ months I have been involved with the implementation of a CRM system, so I am going to use part of this as an example.

 

Example:

The CRM system has a database of customers and a separate database that holds all addresses within a geographic area. Aggregation would make sense in this situation, as a Customer 'has-a' Address. It wouldn't make sense to say that an Address is 'part-of' the Customer, because it isn't. Consider it this way, if the customer ceases to exist, does the address? I would argue that it does not cease to exist. Aggregation is shown on a UML diagram as an unfilled diamond (see Figure). 

 

C++ Example of Composition and Aggression

class Professor;

class Department
{
  ...
  private:
               // Aggregation
               Professor* members[5];
  ...
};

class University
{
  ...
  private:
               // Composition
               Department faculty[20];
  ...
};

A.Shah's Comment:
thanx a lot



By reading or posting messages on these forums, you are agreeing to the Answer Board's Terms of Service and Conduct (TSC).


About Cramster | Terms of Use | Privacy Policy | Contact Us | Press Room | Site Map | Support | Anti-Cheating Policy

Cramster.com is not affiliated with any publisher. Book covers, title and author names appear for reference only.
Copyright © 2008 Cramster, Inc.