10.11.3. Identifying Class Relationships From C++ Code Patterns

Think you know the basic C++ code patterns implementing the five UML class relationships? Try this simple test.

  1. Examine classes in each example and identify the represented relationship
  2. Hover the mouse pointer over the code to verify the class relationship
  3. Click on "Details" to jump to a full description of the relationship
class A
{

};

class B : public A
{

};

Inheritance

look for the string ": public" followed by a class name:
: public super_class_name
Details
class B
{
	A* a;
};

class A
{
	B* b;
};

Association

look for pointer member variables in both classes:
A* a;
B* b;
Details
class B
{

};

class A
{
	B* b;
};

Aggregation

look for a single member pointer variable in one (the whole) class:
B* b;
Details
class B
{

};

class A
{
	B b;
};

Composition

look for a non-pointer member variable in one (the whole) class:
B b;
Details
T is an unspecified data type and B is a class name.
class A
{
    public:
	T func1(B b) {...}
	T func2(B* b) {...}
	T func3(B& b) {...}
};
 

Dependency

look for functions with a class-type parameter
Details (4)
T is an unspecified data type and B is a class name.
class A
{
    T func4()
    {
        B b;
        b.function();
    }
};

Dependency

look for a function with a local class-type variable
Details (4)