33
Chapter 3: Modifying objects Operators and Expressions McGraw-Hill, Inc. Modified by S. Sudarshan

Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Embed Size (px)

Citation preview

Page 1: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Chapter 3: Modifying objects

Operators and Expressions

JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Page 2: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Memory Depiction

float y = 12.5;12.5y

1001100210031004

Page 3: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Memory Depiction

float y = 12.5;

int Temperature = 32;12.5

32

y

Temperature

100110021003100410051006

Page 4: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Memory Depiction

float y = 12.5;

int Temperature = 32;

char Letter = 'c';

12.5

32'c'

y

TemperatureLetter

1001100210031004100510061007

Page 5: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Memory Depiction

float y = 12.5;

int Temperature = 32;

char Letter = 'c';

int Number;

12.5

32

'c'

y

Temperature

Letter

1000100110021003100410051006

-Number

1007

10091008

101010111012101310131014

Unused space: integers/longs/floats/doubles should be word aligned on most CPUs, i.e. start at multiples of word size

Page 6: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Assignment Statement

Basic form object = expression ;

Celsius = (Fahrenheit - 32) * 5 / 9;

y = m * x + b;

Action Expression is evaluated Expression value stored in object

Target becomes source

Page 7: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Swapping Values

int Value1 = 10;

int Value2 = 20;

10

20

Value1

Value2

Suppose we want to swap Value1 and Value2?

Page 8: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Swapping Values

int Value1 = 10;

int Value2 = 20;

int Hold = Value1;

10

20

Value1

Value2

10Hold

Page 9: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Swapping Values

int Value1 = 10;

int Value2 = 20;

int Hold = Value1;

Value1 = Value2;

?

20

Value1

Value2

10Hold

Page 10: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Swapping Values

int Value1 = 10;

int Value2 = 20;

int Hold = Value1;

Value1 = Value2;

20

20

Value1

Value2

10Hold

Page 11: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Swapping Values

int Value1 = 10;

int Value2 = 20;

int Hold = Value1;

Value1 = Value2;

Value2 = Hold;

20

?

Value1

Value2

10Hold

Page 12: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Swapping Values

int Value1 = 10;

int Value2 = 20;

int Hold = Value1;

Value1 = Value2;

Value2 = Hold;

We swapped the values of objects Value1 and Value2 using Hold as temporary holder for Value1’s starting value!

20

10

Value1

Value2

10Hold

Page 13: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Const Definitions

Modifier const indicates that an object cannot be changed

Object is read-only

Useful when defining objects representing physical and mathematical constants

const float Pi = 3.1415;

Value has a name that can be used throughout the program

const int SampleSize = 100;

Makes changing the constant easy Only need to change the definition and recompile

Page 14: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Assignment Conversions

Floating-point expression assigned to an integer object is truncated

Integer expression assigned to a floating-point object is converted to a floating-point value

Considerfloat y = 2.7;int i = 15;int j = 10;i = (int) y; // i is now 2cout << i << endl; y = j; // y is now 10.0cout << y << endl;

Page 15: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

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

Examplesint i = 3;i += 4; // i is now 7cout << i << endl;

float a = 3.2;a *= 2.0; // a is now 6.4cout << a << endl;

Page 16: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Increment and Decrement

C++ has special operators for incrementing or decrementing an object by oneExamplesint k = 4;++k; // k is 5k++; // k is 6cout << k << endl;int i = k++; // i is 6, k is 7cout << i << " " << k << endl;int j = ++k; // j is 8, k is 8cout << j << " " << k << endl;

syntaxsemantics -- meaning

Page 17: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Nonfundamental Types

Nonfundamental as they are additions to the language

C++ permits definition of new types and classes A class is a special kind of type

E.g. Standard Template Library (STL) provides class string

Class objects typically have Data members that represent attributes and values Member functions for object inspection and

manipulation Members are accessed using the selection operator

(.)j = s.size();

Auxiliary functions for other behaviors

Libraries often provide special-purpose types and classes

Programmers can also define their own types and classes

Page 18: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Class string

Class string Used to represent a sequence of characters as a

single objectSome definitionsstring Name = "Joanne";string DecimalPoint = ".";

string empty = "";

string copy = name;

string Question = '?'; // illegal

Page 19: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Class String

To access a library use a preprocessor directive to add its definitions to your program file

#include <string>The using namespace std statement makes syntax less clumsy

Without itstd::string s = "Sharp";std::string t = "Spiffy";

With itusing namespace std; // std contains stringstring s = "Sharp";string t = "Spiffy";cin >> s;cout << t;“foo”.size() – illegals.size() -- legal

Page 20: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Class stringSome string member functions

size() determines number of characters in the stringstring Saying = "Rambling with Gambling";cout << Saying.size() << endl; // 22

substr() determines a substring (Note first position has index 0)string Word = Saying.substr(9, 4); // (pos, width)// returns “with”

find() computes the position of a substringint j = Saying.find("it"); // 10int k = Saying.find("its"); // ? If string not found, value >= string.size() is returned

string.find(substring, startpos) returns first position in string, after startpos, where the substring appears

Page 21: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Class string

Auxiliary functions and operators

getline() extracts the next input linestring Response;

cout << "Enter text: ";

getline(cin, Response, '\n');

cout << "Response is \"" << Response

<< "\"” << endl;

Example runEnter text: Want what you do

Response is "Want what you do"

character that terminates the line

Page 22: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Class string

Auxiliary operators

+ string concatenationstring Part1 = "Me";string Part2 = " and ";string Part3 = "You";string All = Part1 + Part2 + Part3;“foo “ + “bar” -- illegal

+= compound concatenation assignmentstring ThePlace = “IIT";ThePlace += ", Bombay"; == ThePlace + “, Bombay”“Bombay” + ThePlace -- also legal

Page 23: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

#include <iostream>using namespace std;int main() { cout << "Enter the date in American format: " << "(e.g., January 1, 2007) : "; string Date; getline(cin, Date, '\n'); int i = Date.find(" "); string Month = Date.substr(0, i); int k = Date.find(","); string Day = Date.substr(i + 1, k - i - 1); string Year = Date.substr(k + 2, Date.size() - 1);//-k-2 string NewDate = Day + " " + Month + " " + Year; cout << "Original date: " << Date << endl; cout << "Converted date: " << NewDate << endl; return 0;}

parse, parsing: breaking down a string into its parts

Page 24: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

End of Chapter

Page 25: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Definition

int NewStudents = 6; 6NewStudents

Page 26: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Definition

int NewStudents = 6;

int OldStudents = 21;

6

21

NewStudents

OldStudents

Page 27: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Definition

int NewStudents = 6;

int OldStudents = 21;

int TotalStudents;

6

21

NewStudents

OldStudents

-TotalStudents

Page 28: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Assignment Statement

int NewStudents = 6;

int OldStudents = 21;

int TotalStudents;

TotalStudents = NewStudents + OldStudents;

6

21

NewStudents

OldStudents

?TotalStudents

Page 29: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Assignment Statement

int NewStudents = 6;

int OldStudents = 21;

int TotalStudents;

TotalStudents = NewStudents + OldStudents;

6

21

NewStudents

OldStudents

27TotalStudents

Page 30: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Assignment Statement

int NewStudents = 6;

int OldStudents = 21;

int TotalStudents;

TotalStudents = NewStudents + OldStudents;

OldStudents = TotalStudents;

6

?

NewStudents

OldStudents

27TotalStudents

Page 31: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Assignment Statement

int NewStudents = 6;

int OldStudents = 21;

int TotalStudents;

TotalStudents = NewStudents + OldStudents;

OldStudents = TotalStudents;

6

27

NewStudents

OldStudents

27TotalStudents

Page 32: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Incrementing

int i = 1; i 1

Page 33: Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Incrementing

int i = 1;

i = i + 1;

Assign the value of expression i + 1 to i

Evaluates to 2

i 1

2i