friend: a function or class granted access to another class's private/protected members despite not being a member itself — breaks strict encapsulation deliberately for specific, controlled casesstatic member: shared across all objects of a class (one copy total), rather than per-object — accessed via the class name, not requiring an instancethis pointer: an implicit pointer available inside non-static member functions, pointing to the calling object itselfconst: qualifier preventing modification — const member functions cannot modify the object's data members; const parameters/objects cannot be alteredMCQ Trap:
staticmembers belong to the class (one shared copy); non-static (instance) members belong to each object separately.
friend — deliberately breaking encapsulation, in a controlled wayclass 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 classclass 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;
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
};
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 |
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.