What is the diffrence betwen aggregtion and compostion

This is a discussion on What is the diffrence betwen aggregtion and compostion within the C++ Programming forums, part of the General Programming Boards category; Good evening,.. i missed my last cpp lecture and can't understand few thing i hope you can explain it (the ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    112

    Post What is the diffrence betwen aggregtion and compostion

    Good evening,..
    i missed my last cpp lecture and can't understand few thing i hope you can explain it (the other classmates didn't understand it aswal so i went to you guys )

    she gave a code and said :
    there are two type of heritage :
    compostion:
    works like "has a " and "is part of" : a computer is the some of it components (IO,CPU,BUS,storage device,...);

    aggretion.
    no info at all
    Inheritance:
    work like "is a" : a chief excuter is type of an manger that is type of an employe.
    here is example for :
    compostion (sorry for the indent style i just copy paste it (im afraid to some how make it less understandable));

    Code:
    .
    #include <cstring>
    class Engine{
              char* model;
              int volume;
    public:
             Engine(char* Model="UNKNOWN",int Volume=0)
             {
    	model=new char[strlen(Model)+1];
    	if(model)					     strcpy(model,Model);
    	else return;
    	volume=Volume;
               }
             ~Engine()
               {if(model) delete model;}
              void Print()
              { cout<<model<<" "<<volume<<endl;        }
    };
    class Car{
              char* color;
              int year;
              Engine eng;
    public:
              Car(char* Model="UNKNOWN",int Volume=0,char* olor="UNKNOWN",
               int Year=1):eng(Model,Volume)
                {
                 color=new char[strlen(Color)+1];
                  if(color)
                    strcpy(color,Color);
                  year=Year;
                 }
                 ~Car()	 {if(color) delete color;}
                 void Print()
                 {
                  cout<<color<<" "<<year<<endl;
                  eng.Print();
                  }
    };
    
    int main()
    {
       char buf_mod[100],buf_color[100];
       int year,volume;
       cout<<"enter data :; cin>>buf_mod>>year>>buf_color>>volume;
       Car p(buf_mod,year,buf_color,volume);
        p.Print();
    }
    and an example for Aggregation.

    Code:
    class Point
    {
          int x,y;
    public:
          Point(int _x,int _y){x=_x; y=_y;}
          ~Point(){}
          void Print()
          { cout<<x<<" "<<y<<endl;}
    };
    
    class Line
    {
         Point *p1,*p2;
         public:
          Line(int x1,int y1,int x2,int y2)
           {
             p1=new Point(x1,y1);
             p2=new Point(x2,y2);
            }
    
            /*Line(int x1,int y1,int x2,int y2):p1(new Point(x1,y1)),p2(p2=new Point(x2,y2))
             {  }*/
            ~Line()
             {if(p1) delete p1;if(p2) delete p2;}
             void Print()
             {
               cout<<" p1 :";p1->Print();  cout<<" p2 :";
                p2->Print();
              }
    };
    
    int main()
    {
    	Line l1(1,2,10,20),*l_ptr;
    	l_ptr=new Line(1,2,3,4);
    	l_ptr->Print();
    	l1.Print();
    	delete l_ptr;
    }
    i can't realy get the idea what is the defrence ..
    if in both i transfer all the func and from the base class to the new class what is the defrance.
    could you plese try to provide a little more understandble class that will show the diffrence
    in Dietel's book i didn't even found does types.

    thnx in advance..
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by jabka View Post
    i can't realy get the idea what is the defrence ..
    You can't tell the difference between saying "I own a car" vs. "I am a car?"

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    from the code type of view i meant.
    i cant see any difference with the emplementation.
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,961
    To be honest, I thought they were the same

    A quick search (you did search, didn't you?) led me to Association, Aggregation and Composition, which states:
    Aggregation is the typical whole/part relationship. This is exactly the same as an association with the exception that instances cannot have cyclic aggregation relationships (i.e. a part cannot contain its whole).

    Composition is exactly like Aggregation except that the lifetime of the 'part' is controlled by the 'whole'. This control may be direct or transitive. That is, the 'whole' may take direct responsibility for creating or destroying the 'part', or it may accept an already created part, and later pass it on to some other whole that assumes responsibility for it.
    With this in mind, it looks like neither of your examples show aggregation since the second example makes use of RAII, thus the lifetime of the Point that the Point* members point to is controlled by the Line object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> You can't tell the difference between saying "I own a car" vs. "I am a car?"
    But that's not the difference between composition and aggregation.

    Aggregation is like composition in that both refer to when one object contains another object. In composition, the outer class is responsible for the member object. It owns it, so that when the class instance is destroyed the member is no longer useful either. For example, in the code above, there is no use for an engine except as part of a car. Each car has an engine, and when an instance of a Car is destroyed, so is its Engine.

    For aggregation, the containing class is just holding references to the member objects. It does not own them, they exist outside of that class. So in the example above, points exist regardless of whether there is a line that holds them. If you destroy a line, you don't remove the points also.

    Perhaps its the difference between "the car has an engine" and "the car has a driver".

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    This is to an extent repeating earlier replies, but here goes...

    Both Composition and Aggregation are examples of one object 'containing' another.

    In Composition, the container object is responsible for creation and destruction of the sub-object. The existence of the contained object implies existence of the container. Both of your examples in the first post are examples of Composition. Most instances of objects containing objects are Composition.

    In Aggregation, the container object is not responsible for creation or destruction of the sub-object. In practice, this means that the Aggregator only contains a pointer to the aggregated object, which it did not create and does not free. Aggregation is necessary when unrelated objects need to share the same resource.
    Code:
    Lists::classify_student (Student * classify_me) {
       if (classify_me -> getGPA() > 3)
          honor_roll.push_back (classify_me);
       
       if (classify_me -> missed_days() == 0)
          perfect_attendance.push_back (classify_me);
    
       // &ct
    }
    Note that aggregated resources can be freed while still referenced by the aggregator, so some care has to be taken with object management.
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    I find web definitions help make sense of this stuff...

    Associate :- Make a logical or causal connection; "I cannot connect these two pieces of evidence in my mind"; "colligate these facts"; "I cannot relate these events at all"; consort: keep company with; hang out with; "He associates with strange people"; "She affiliates with her colleagues"

    (ie. communicating with other objects)

    Code:
    class A
    {
      private:
        B* itsB;
    };
    Aggregate :- Gather in a mass, sum, or whole; formed of separate units in a cluster; "raspberries are aggregate fruits"

    (ie. amassing other objects in a non-containing manner)

    Code:
    class Node
    {
      private:
        vector<Node*> itsNodes;       // Node*, so not actually within this class
    };
    Compose :- The act of creation; form the substance of; "Greed and ambition composed his personality" ( Composition :- the spatial property resulting from the arrangement of parts in relation to each other and to the whole; "harmonious composition is essential in a serious work of art"; the bringing together of parts or elements to form a whole; the structure, organization, or total form of a work of art. )

    (ie. composing the object within + deleting it)

    Code:
    class Car
    {
      public:
        virtual ~Car() {delete itsCarb;}
      private:
        Carburetor* itsCarb
    };
    we are one

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21