tailieunhanh - A Complete Guide to Programming in C++ part 26

A Complete Guide to Programming in C++ part 26. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | EXPRESSIONS WITH REFERENCE TYPE 229 Every C expression belongs to a certain type and also has a value if the type is not void. Reference types are also valid for expressions. The Stream Class Shift Operators The and operators used for stream input and output are examples of expressions that return a reference to an object. Example cout Good morning This expression is not a void type but a reference to the object cout that is it represents the object cout. This allows you to repeatedly use the on the expression cout Good morning The expression is then equivalent to cout Good morning Expressions using the operator are composed from left to right as you can see from the table of precedence contained in the appendix. Similarly the expression cin variable represents the stream cin. This allows repeated use of the operator. Example int a double x cin a x cin a x Other Reference Type Operators Other commonly used reference type operators include the simple assignment operator and compound assignments such as and . These operators return a reference to the operand on the left. In an expression such as a b or a b a must therefore be an object. In turn the expression itself represents the object a. This also applies when the operators refer to objects belonging to class types. However the class definition stipulates the available operators. For example the assignment operators and are available in the standard class string. Example string name Jonny name Depp Reference to name Since an expression of this type represents an object the expression can be passed as an argument to a function that is called by reference. This point is illustrated by the example on the opposite page. 230 CHAPTER 12 REFERENCES AND POINTERS DEFINING POINTERS Sample program Prints the values and addresses of variables. ---------------------------------------------------- include iostream using namespace std int var ptr Definition of variables var and ptr int main Outputs the values and .

TỪ KHÓA LIÊN QUAN