This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CHAPTER 8 Operator Overloading, Friends, and References 8.1 BASIC OPERATOR OVERLOADING 318 Overloading Basics 319 Tip: A Constructor Can Return an Object 324 Returning by const Value 325 Tip: Returning Member Variables of a Class Type 328 Overloading Unary Operators 329 Overloading as Member Functions 330 Tip: A Class Has Access to All Its Objects 332 Overloading Function Application ( ) 333 Pitfall: Overloading && , || , and the Comma Operator 334 8.2 FRIEND FUNCTIONS AND AUTOMATIC TYPE CONVERSION 334 Constructors for Automatic Type Conversion 334 Pitfall: Member Operators and Automatic Type Conversion 335 Friend Functions 336 Pitfall: Compilers without Friends 340 Friend Classes 340 8.3 REFERENCES AND MORE OVERLOADED OPERATORS 342 References 342 Pitfall: Returning a Reference to Certain Member Variables 343 Overloading >> and << 344 Tip: What Mode of Returned Value to Use 351 The Assignment Operator 353 Overloading the Increment and Decrement Operators 354 Overloading the Array Operator [ ] 357 Overloading Based on L-Value versus R-Value 359 Chapter Summary 359 Answers to Self-Test Exercises 360 Programming Projects 362 8 Operator Overloading, Friends, and References Eternal truths will be neither true nor eternal unless they have fresh meaning for every new social situation. Franklin D. Roosevelt, Address at the University of Pennsylvania [September 20, 1940] INTRODUCTION This chapter discusses a number of tools to use when defining classes. The first tool is operator overloading, which allows you to overload operators, such as + and == , so that they apply to objects of the classes you define. The second tool is the use of friend functions which are functions that are not members of a class but still have access to the private members of the class. This chapter also discusses how to provide automatic type conversion from other data types to the classes you define. If you have not yet covered arrays (Chapter 5), you should skip the subsection of 8.3 entitled Overloading the Array Operator [ ] . It covers a topic that may not make sense unless you know about array basics. 8.1 Basic Operator Overloading Hes a smooth operator. Line from a song by Sade (written by Sade Adu and Ray St. John) Operators such as + , - , % , == , and so forth are nothing but functions that are used with a slightly different syntax. We write x + 7 rather than +(x, 7) , but the + operator is a function that takes two arguments (often called operands rather than arguments) and returns a single value. As such, operators are not really necessary. We could make do with +(x, 7) or even add(x, 7) . Operands are an example of what is often called syntactic sugar , meaning a slightly different syntax that people like. However, people are very comfortable with the usual operator syntax, x + 7 , that C++ uses for types such as int and double . And one way to view a high-level language, such as C++, is as a way to make people comfortable with programming computers. Thus, this syntactica way to make people comfortable with programming computers....
View Full Document
This note was uploaded on 04/10/2008 for the course CCS 115 taught by Professor _ during the Spring '08 term at NJIT.
- Spring '08