MCQ Trap: static members belong to the class (one shared copy); non-static (instance) members belong to each object separately.

friend — deliberately breaking encapsulation, in a controlled way

class Box
{
private:
    double width;
public:
    Box(double w) : width(w) {}
    friend void printWidth(Box b);   // grants THIS specific function access to private members
};

void printWidth(Box b)
{
    cout << "Width: " << b.width;   // allowed -- printWidth is a friend, even though it's not a member
}

friend is used sparingly — usually for operator overloading (like operator<<) where the function genuinely needs private access but can't be a member function due to argument order.

static — one copy shared by the whole class

class Employee
{
public:
    static int employeeCount;   // shared across ALL Employee objects
    Employee() { employeeCount++; }
};
int Employee::employeeCount = 0;   // defined outside the class

// Employee::employeeCount accessed via CLASS name, not an object
cout << Employee::employeeCount;

Composition — "has-a" using another class as a member

class Engine { public: void start() { cout << "Engine starts\n"; } };

class Car
{
private:
    Engine engine;   // Car HAS-A Engine -- composition, not inheritance
public:
    void start() { engine.start(); }   // delegates to the contained object
};

Object Relationships, Side by Side — Association, Aggregation & Composition

CSS examiners frequently ask you to "distinguish" between these three — memorize this spectrum, from loosest to tightest coupling:

Relationship Type Ownership? Lifetime dependency Example
Association "uses-a" (general link) No ownership either way Independent — neither object's lifetime affects the other A Teacher and a Student in the same School
Aggregation "has-a" (weak) Whole "has" parts, but doesn't own them exclusively Part can outlive the whole A Department has Professors; professors still exist if the department is dissolved
Composition "has-a" (strong) Whole exclusively owns the parts Part is destroyed when the whole is destroyed A House and its Rooms — rooms don't exist independently of the house

Association — the loosest relationship

Two classes interact with or know about each other, but neither owns nor is composed of the other.

class Student;   // forward declaration
class Teacher
{
public:
    void teach(Student& s);   // Teacher USES a Student -- association, no ownership
};

Association can be one-directional (only one class knows about the other) or bidirectional (both classes reference each other), and can carry multiplicity (one-to-one, one-to-many, many-to-many) — e.g., one Teacher teaches many Students.