chap03 - Chapter 3 Chapter Modifying Modifying objects objects Key Concepts Key assignment operation assignment conversion assignment precedence and

chap03 - Chapter 3 Chapter Modifying Modifying objects...

This preview shows page 1 - 12 out of 30 pages.

Chapter 3 : Chapter 3 : Modifying Modifying objects objects
Key Concepts: Key Concepts: assignment operation assignment conversion assignment precedence and associativity extraction operations const const declarations compound assignment operations input with cin cin increment and decrement operation strings
Memory Depiction Memory Depiction Memory char chLetter = 'C'; 1007 chLetter 'C' short nTemperature = 32; 1005 1006 nTemperature 32 float y = 12.5; 1001 1002 1003 1004 y 12.5 1008 1009 1010 1011 …? iNumber int iNumber;
Assignment Statement Assignment Statement Basic form object = expression object = expression ; Celsius = (Fahrenheit - 32) * 5 / 9; y = m * x + b; Action Expression is evaluated Expression value stored in object
Assignment Statement Assignment Statement Define objects: Memory int iTotalNbr; iTotalNbr ? int iOldNbr = 21; iOldNbr 21 27 Evaluation 27 Store Assignment operation: iNewNbr + iOldNbr; iTotalNbr = iNewNbr 6 int iNewNbr = 6; 27 27 iOldNbr = iTotalNbr;
Assignment Statement Assignment Statement How can we swap the value of two objects? How can we swap the value of two objects? Memory int Y = 20; Y 20 10 int X = 10; X Exchange the value of X and Y : X = Y; Y = X; What happened? What happened?
Assignment Statement Assignment Statement After the assignment: After the assignment: Memory Y 20 20 X Exchange the value of X and Y : X = Y; Y = X;
Assignment Statement Assignment Statement The correct swapping method: The correct swapping method: Memory int Y = 20; Y 20 Exchange the value of X and Y : int Z = X; Z 10 Define a temporary object Z to hold the starting value of X Y = Z; 10 X 10 int X = 10; X = Y; 20
Assignment Statement Assignment Statement After the exchange: After the exchange: Memory Y 10 20 X 10 Z
Assignment Statement Assignment Statement Incrementing Incrementing int i = 1; i = i + 1; Assign the value of expression i + 1 to i Evaluates to 2 i 1 2 i
Compound Assignment Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object ( See text book page 851, the appendix A.2 ) <<= >>= &= ^= |= += -= *= /= Examples int i = 3; i += 4; // i is now 7 cout << i << endl; float a = 3.2; a *= 2.0; // a is now 6.4 cout << a << endl;

You've reached the end of your free preview.

Want to read all 30 pages?

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern