What is the difference between association, aggregation and composition?

后端 未结 19 1846
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 01:44

What is the difference between association, aggregation, and composition? Please explain in terms of implementation.

19条回答
  •  灰色年华
    2020-11-22 02:00

    Composition (If you remove "whole", “part” is also removed automatically– “Ownership”)

    • Create objects of your existing class inside the new class. This is called composition because the new class is composed of objects of existing classes.

    • Typically use normal member variables.

    • Can use pointer values if the composition class automatically handles allocation/deallocation responsible for creation/destruction of subclasses.

    Composition in C++

    #include 
    using namespace std;
    /********************** Engine Class ******************/
    class Engine
    {
        int nEngineNumber;
        public:
        Engine(int nEngineNo);
        ~Engine(void);
    };
    Engine::Engine(int nEngineNo)
    {
        cout<<" Engine :: Constructor " <

    Output

    --------------- Start Of Program --------------------
    ------------- Inside Car Block ------------------
    Engine :: Constructor
    Car :: Constructor
    Car :: Destructor
    Engine :: Destructor
    ------------- Out of Car Block ------------------
    ------------- Inside Bus Block ------------------
    Engine :: Constructor
    Bus :: Constructor
    Bus :: Destructor
    Engine :: Destructor
    ------------- Out of Bus Block ------------------
    --------------- End Of Program --------------------
    

    Aggregation (If you remove "whole", “Part” can exist – “ No Ownership”)

    • An aggregation is a specific type of composition where no ownership between the complex object and the subobjects is implied. When an aggregate is destroyed, the subobjects are not destroyed.

    • Typically use pointer variables/reference variable that point to an object that lives outside the scope of the aggregate class

    • Can use reference values that point to an object that lives outside the scope of the aggregate class

    • Not responsible for creating/destroying subclasses

    Aggregation Code in C++

    #include 
    #include 
    using namespace std;
    /********************** Teacher Class ******************/
    class Teacher
    {
        private:
        string m_strName;
        public:
        Teacher(string strName);
        ~Teacher(void);
        string GetName();
    };
    Teacher::Teacher(string strName) : m_strName(strName)
    {
        cout<<" Teacher :: Constructor --- Teacher Name :: "<

    Output

    --------------- Start Of Program --------------------
    Teacher :: Constructor --- Teacher Name :: Reference Teacher
    Teacher :: Constructor --- Teacher Name :: Pointer Teacher
    ------------- Inside Block ------------------
    Department :: Constructor
    Department :: Destructor
    ------------- Out of Block ------------------
    Teacher :: Destructor --- Teacher Name :: Pointer Teacher
    Teacher :: Destructor --- Teacher Name :: Reference Teacher
    --------------- End Of Program --------------------
    

提交回复
热议问题