Presentation is loading. Please wait.

Presentation is loading. Please wait.

Understanding Friends Object-Oriented Programming Using C++ Second Edition 7.

Similar presentations


Presentation on theme: "Understanding Friends Object-Oriented Programming Using C++ Second Edition 7."— Presentation transcript:

1 Understanding Friends Object-Oriented Programming Using C++ Second Edition 7

2 Objectives In this chapter, you will learn: What a friend is How to declare a friend function How to use a friend function with data from two classes How and when to use a forward reference When to use a friend function with two or more instances of the same class How to bestow friendship on functions that are members of other classes How to bestow friendship on an entire class 7

3 What Are Friends? A friend function is a function that can access private data members of a class, even though the function itself is not a member of the class A friend class is a class whose functions can all access private data members of another class A friend function can access private data from a class of which it is not a member, but a friend function cannot be a friend on its own The friend relationship is always one-sided 7

4 How to Declare a Function as a Friend The class contains data fields for a customer number and balance The class contains three functions; two are members of the class The default constructor for the Customer class supplies values for the data fields if none are provided when a Customer object is instantiated 7

5 The Customer Class 7

6 How to Declare a Function as a Friend As a member of the Customer class, the displayCustomer() function meets the following conditions: –Requires the class name Customer and the scope resolution operator in the function header –Must have access to the private fields custNum and balanceDue –Must be declared in the public section of the class definition, so that a main() program (or any other client function) can use it 7

7 How to Declare a Function as a Friend The function displayAsAFriend() is not a Customer member function It must meet the following conditions: –Cannot use the class name Customer and the scope resolution operator in the function header –Need not be declared in the public section of the class definition –Must use the C++ keyword friend in its declaration 7

8 How to Declare a Function as a Friend When any function tries to access an object’s private data member, the compiler examines the list of function prototypes in the class declaration, and one of three things happens: –The function is found to be a member function, and access is approved –The function is found to be a friend function, and access is approved –The function is not found to be a member or a friend, and access is denied; you receive an error message 7

9 A main() Program Using the Customer Class 7

10 Using a Friend Function to Access Data from Two Classes Figure 7-3 shows the definition section of a CustTransaction class You might create a CustTransaction object for each customer transaction, such as a purchase of an item, payment on an account, or return of an item 7

11 Using a Friend Function to Access Data from Two Classes If you create a function that performs the payment application operation, you have at least five choices (although four of these are inferior choices): –You can make the balanceDue field in the Customer class public, and the paymentAmount field in the CustTransaction class public –If you create a payment application function that is not a member of either the Customer or the CustTransaction class, the function will not have access to the private data fields of either class 7

12 Using a Friend Function to Access Data from Two Classes The choices continued: –If you make the payment application function a member of the Customer class, the function has access to balanceDue, but not to paymentAmount, which is private within the CustTransaction class –If you make the payment application function a member of the CustTransaction class, the function has access to paymentAmount, but not to balanceDue, which is private within the Customer class –You can make the payment application function a friend of both classes 7

13 The applyTransaction() Function 7

14 Using a Forward Reference To use the applyTransaction() function as a friend to both the Customer and the CustTransaction class, you must declare the function as a friend within each class The declaration of the applyTransaction() function is: friend void applyTransaction(Customer cust, CustTransaction trans); You already know you must declare a variable before using it 7

15 Using a Forward Reference You also must declare, or prototype, a function before you use it Similarly, a class must be declared before you use it A forward reference lets the compiler know that the class exists, and that the details will come later 7

16 Using a Friend Function with Two Classes 7

17 7

18 Using a Forward Reference When two classes refer to each other, you can choose to forward declare either one, and then define the other class first The same principle holds true if three, four, or more classes share a friend function that makes reference to all the classes 7

19 Using a Forward Reference In this case you: –Forward declare all the classes except one –Define the class you did not forward declare, and include the friend function prototype in the class definition –Define all classes that you did forward declare. The forward declared classes will contain the same friend function prototype as the first class you defined –Define the friend function itself In the set of steps on pages 242 to 244 of the textbook, you define two classes that will be used by a computer repair company 7

20 Using a Forward Reference In the steps shown on pages 244 to 245 of the textbook, you add a main() function to the file 7

21 Two Instances of a Class Using a Friend You can use a friend function to access private data members from objects that belong to two different classes If you want a function to have access to two or more instances of the same class, you can use either a class member function or a friend function You can sum two CustTransaction objects without creating a friend function 7

22 Two Instances of a Class Using a Friend You simply create a member function for the CustTransaction class The prototype is: CustTransaction addTransactions (const CustTransaction aPayment); 7

23 CustTransaction Class with addTransactions() Member Function 7

24 Two Instances of a Class Using a Friend Within addTransactions(), the billingSummary.paymentAmount is set equal to the sum of the invoking object’s (firstTrans) paymentAmount and the passed object’s (secondTrans) payment amount A copy of the entire, newly constructed and defined billingSummary is returned to the calling function, where totalTrans receives it One way to avoid a subsidiary transaction is to create a friend function to the CustTransaction class 7

25 CustTransaction Class with addTransactions() Friend Function 7

26 Two Instances of a Class Using a Friend In the set of steps on pages 250 to 253 of the textbook, you create a friend function to compare two Loan objects and determine whether they are equal You consider two loans equal if they are for the same amount at the same interest rate 7

27 Output of Loan Program 7

28 Making a Friend of a Member of Another Class Classes can bestow friendship on nonmember functions; they also can bestow friendship on functions that are members of other classes Consider two classes developed for a college registration system One class is named StudentRequest; it holds a student idNum and a course section in which the student requests enrollment The other class, named CourseRec, holds information about one section of a course, including a section number, enrollment limit, and current enrollment 7

29 Making a Friend of a Member of Another Class The student enrollment request example shows a class granting friendship to a single function that is a member of another class Creating the class definitions for the preceding example requires three operations: –Forward declare the class that is granting friendship, because the class that holds the friend will use this class name –Declare the class containing the function that will be a friend –Define the class that is granting friendship 7

30 Definitions of CourseRec and StudentRequest 7

31 7

32 Making a Friend of a Member of Another Class 7

33 In the steps referred to on pages 257 to 260 of the textbook, you write a program that can be used for classroom scheduling Two classes of objects are needed: Courses and Rooms When a Course section is scheduled, a search is made for a Room that holds the number of students that might enroll 7

34 Making a Friend of Another Class To grant friendship to an entire class, construct the class definition simply by writing the keyword friend followed by class and the name of the class When you grant friendship to a class, every function that is a member of the class becomes a friend of the granting class 7

35 Summary A friend function is a function that can access private data members of a class, even though the function itself is not a member of the class A friend function does not use the class name and the scope resolution operator in the function header You can create a function that is a friend to multiple classes 7

36 Summary If you want a function to have access to two or more instances of the same class, you can use either a class member function or a friend function Classes can bestow friendship on functions that are members of other classes You can bestow friendship on an entire class When you grant friendship to a class, every function that is a member of the class becomes a friend of the granting class 7


Download ppt "Understanding Friends Object-Oriented Programming Using C++ Second Edition 7."

Similar presentations


Ads by Google