Unformatted text preview:

Understanding FriendsObjectivesWhat Are Friends?How to Declare a Function as a FriendThe Customer ClassSlide 6Slide 7Slide 8A main() Program Using the Customer ClassUsing a Friend Function to Access Data from Two ClassesSlide 11Slide 12The applyTransaction() FunctionUsing a Forward ReferenceSlide 15Using a Friend Function with Two ClassesSlide 17Slide 18Slide 19Slide 20Two Instances of a Class Using a FriendSlide 22CustTransaction Class with addTransactions() Member FunctionSlide 24CustTransaction Class with addTransactions() Friend FunctionSlide 26Output of Loan ProgramMaking a Friend of a Member of Another ClassSlide 29Definitions of CourseRec and StudentRequestSlide 31Slide 32Slide 33Making a Friend of Another ClassSummarySlide 36Understanding FriendsObject-Oriented Programming Using C++Second Edition7Objectives•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 class7What 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-sided7How 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 instantiated7The Customer Class7How 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 it7How 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 declaration7How 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 message7A main() Program Using the Customer Class7Using 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 item7Using 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 class7Using 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 classes7The applyTransaction() Function7Using 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 it7Using 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 later7Using a Friend Function with Two Classes7Using a Friend Function with Two Classes7Using 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 classes7Using 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 company7Using a Forward Reference•In the steps shown on pages 244 to 245 of the textbook, you add a main() function to the file7Two 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


View Full Document

WPU CS 2300 - Understanding Friends

Download Understanding Friends
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Understanding Friends and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Understanding Friends 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?