NivaarExam PrepOfficial exam papers ↗

25-Comp-B11 Advanced Software Design · December 2018

Question 25 of 28: Pros and Cons of Friend Functions and Friend Classes in C++

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

17-Comp-B11 Advanced Software Design — National Exams, December 2018. 3 hours, closed book exam with up to 2 aid sheets allowed (written on both sides), no calculator permitted. The paper is organized into five parts, and candidates were instructed to answer any five (5) questions in Part I, any three (3) in Part II, any four (4) in Part III, any two (2) in Part IV, and any five (5) in Part V — only the first questions answered, in each part, as they appear in the answer book are marked. All questions carry equal weight, so the 19 questions actually marked (5+3+4+2+5 of 28) each count for 100/19 ≈ 5.26% of the paper. All 28 questions are answered below for completeness.

Reference texts: Sommerville, Software Engineering (10th ed., Pearson) — software processes, requirements engineering, design principles, dependability; Pressman, Software Engineering: A Practitioner's Approach (9th ed.) — supplementary process and quality coverage; Gamma, Helm, Johnson & Vlissides (GoF), Design Patterns: Elements of Reusable Object-Oriented Software — creational/structural/behavioural pattern catalogue and the "program to an interface, not an implementation" / "favor object composition over class inheritance" principles; Sebesta, Concepts of Programming Languages (12th ed.) — polymorphism, dynamic binding, inheritance and language-level object semantics; Bertrand Meyer, Object-Oriented Software Construction — design by contract, preconditions/postconditions/invariants; Barbara Liskov's 1987 substitutability paper for Question 11; Karl Wiegers, Software Requirements (3rd ed.) — the functional/quality/process/implementation/business requirements taxonomy of Question 2; Kruchten, The Rational Unified Process: An Introduction, for Question 1; Myers, The Art of Software Testing, for Questions 6 and 28.

PART I — General Principles (answer any 5 of 7)

Question 25: Pros and Cons of Friend Functions and Friend Classes in C++ (Part V)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

friend grants a function or class direct ACCESS to another class's private/protected members, bypassing the encapsulation boundary (Question 23) the class's public interface would otherwise enforce.

Pro 1 — performance. Tightly-coupled, performance-critical code accesses the internal representation directly rather than through possibly-virtual public accessor calls. Example: a Matrix multiplication kernel declared as a friend function of Matrix can read the raw internal array directly, avoiding a getter call per element in a hot inner loop.

Pro 2 — natural syntax for symmetric operators. operator<< for stream output cannot be a member function of the class being printed (the left-hand operand must be the stream, not the object), so it needs friend access to print the object's private state directly. Example: friend ostream& operator<<(ostream& os, const Point& p) prints p's private x/y without needing public getters that would otherwise exist solely for this one purpose.

Con 1 — breaks information hiding (Question 23). A friend can read AND WRITE the class's internal representation directly, so the class's actual invariant-preserving interface silently expands to include every friend, not just its declared public methods. Example: if Matrix's internal array layout later changes from row-major to a sparse representation, every friend function that read the raw array directly must also be found and rewritten, unlike a change kept behind accessor methods, which by definition cannot break well-behaved external callers.

Con 2 — scattered, hard-to-audit access. friend declarations sit far from the class's own call sites, so determining "who can affect this class's invariants" requires a whole-codebase search. Example: a maintainer changing BankAccount's private balance-tracking field must also locate and review every class/function declared as a friend anywhere in the codebase, not just the class's own listed public methods.

Conclusion. friend is a deliberate, narrow escape hatch traded against modularity for a measured performance or syntax need — used sparingly (operator-overload pairs, tightly-coupled iterator/container pairs), never as a routine substitute for a proper public interface.