5.12 FRIEND CLASSES

Declaration of friend is not restricted to methods only. You can declare one class as friend as other. For example,

class ABC { // some data members             // some member functions             friend XYZ ;           }

In this declaration, class XYZ is declared as a friend of class ABC. It implies that class XYZ can use all of data members and member functions (private no bar!) in its definition.

We can rewrite the above program to reflect this principle.

Problem: Rewrite Program 5.9 with following modifications. Declare class RefBooks as friend in class TextBooks. Declare method ttlval() in class RefBooks with object of class TextBooks as parameter(pass by value). Do not use any friend function.

Solution: See Program ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.