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

Slides:



Advertisements
Similar presentations
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Advertisements

Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Class and Objects.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Road Map Introduction to object oriented programming. Classes
Chapter 14: Overloading and Templates
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Understanding Inheritance Object-Oriented Programming Using C++ Second Edition 9.
Classes: A Deeper Look Systems Programming.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11: Classes and Data Abstraction
Chapter 13: Object-Oriented Programming
Chapter 15: Operator Overloading
Review of C++ Programming Part II Sheng-Fang Huang.
1 Friends and Namespace COSC 1567 C++ Programming Lecture 6.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
CITA 342 Section 1 Object Oriented Programming (OOP)
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Learning Objectives Pointers as dada members
Chapter 10: Void Functions
Chapter 3: Using Methods, Classes, and Objects
More about OOP and ADTs Classes
Understanding Inheritance
More about OOP and ADTs Classes
Object-Oriented Programming Using C++ Second Edition
Chapter 6: UNDERSTANDING FRIENDS AND OVERLOADING OPERATORS
Classes and Objects.
Submitted By : Veenu Saini Lecturer (IT)
SPL – PS3 C++ Classes.
Presentation transcript:

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

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

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

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

The Customer Class 7

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

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

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

A main() Program Using the Customer Class 7

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

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

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

The applyTransaction() Function 7

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

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

Using a Friend Function with Two Classes 7

7

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

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

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

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

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

CustTransaction Class with addTransactions() Member Function 7

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

CustTransaction Class with addTransactions() Friend Function 7

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

Output of Loan Program 7

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

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

Definitions of CourseRec and StudentRequest 7

7

Making a Friend of a Member of Another Class 7

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

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

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

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