Operator & Expressions
-
Upload
kim-binuya -
Category
Documents
-
view
228 -
download
0
Transcript of Operator & Expressions
-
8/6/2019 Operator & Expressions
1/46
Modifying objects
Operators and Expressions
JPC and JWD 2002 McGraw-Hill, Inc.
-
8/6/2019 Operator & Expressions
2/46
Memory Depiction
float y = 12.5;12.5y
1001100210031004
-
8/6/2019 Operator & Expressions
3/46
Memory Depiction
float y = 12.5;
int Temperature = 2;12.5
2
y
Temperature
1001100210010041005
1006
-
8/6/2019 Operator & Expressions
4/46
Memory Depiction
float y = 12.5;
int Temperature = 2;
char Letter = 'c';
12.5
2'c'
y
TemperatureLetter
1001100210010041005
10061007
-
8/6/2019 Operator & Expressions
5/46
Memory Depiction
float y = 12.5;
int Temperature = 2;
char Letter = 'c';
int Number;
12.5
2'c'
y
TemperatureLetter
1 11 2111 5
11
-Number 1 81 9
-
8/6/2019 Operator & Expressions
6/46
-
8/6/2019 Operator & Expressions
7/46
Definition
int NewStudents = ; NewStudents
-
8/6/2019 Operator & Expressions
8/46
Definition
int NewStudents = ;
int OldStudents = 21; 21
NewStudents
OldStudents
-
8/6/2019 Operator & Expressions
9/46
Definition
int NewStudents = ;
int OldStudents = 21;
int TotalStudents;
21
NewStudents
OldStudents
TotalStudents
-
8/6/2019 Operator & Expressions
10/46
Assignment Statement
int NewStudents = ;
int OldStudents = 21;
int TotalStudents;
TotalStudents = NewStudents + OldStudents;
21
NewStudents
OldStudents
?TotalStudents
-
8/6/2019 Operator & Expressions
11/46
Assignment Statement
int NewStudents = ;
int OldStudents = 21;
int TotalStudents;
TotalStudents = NewStudents + OldStudents;
21
NewStudents
OldStudents
2TotalStudents
-
8/6/2019 Operator & Expressions
12/46
Assignment Statement
int NewStudents = ;
int OldStudents = 21;
int TotalStudents;
TotalStudents = NewStudents + OldStudents;
OldStudents = TotalStudents;
?
NewStudents
OldStudents
2TotalStudents
-
8/6/2019 Operator & Expressions
13/46
Assignment Statement
int NewStudents = ;
int OldStudents = 21;
int TotalStudents;
TotalStudents = NewStudents + OldStudents;
OldStudents = TotalStudents;
2
NewStudents
OldStudents
2TotalStudents
-
8/6/2019 Operator & Expressions
14/46
Consider
int Value1 = 1 ; 1Value1
-
8/6/2019 Operator & Expressions
15/46
-
8/6/2019 Operator & Expressions
16/46
Consider
int Value1 = 1 ;
int Value2 = 2 ;
int Hold = Value1;
1
2
Value1
Value2
1Hold
-
8/6/2019 Operator & Expressions
17/46
Consider
int Value1 = 1 ;
int Value2 = 2 ;
int Hold = Value1;
Value1 = Value2;
2
Value1
Value2
1Hold
-
8/6/2019 Operator & Expressions
18/46
Consider
int Value1 = 1 ;
int Value2 = 2 ;
int Hold = Value1;
Value1 = Value2;
2
2
Value1
Value2
1Hold
-
8/6/2019 Operator & Expressions
19/46
Consider
int Value1 = 1 ;
int Value2 = 2 ;
int Hold = Value1;
Value1 = Value2;
Value2 = Hold;
2Value1
Value2
1Hold
-
8/6/2019 Operator & Expressions
20/46
Consider
int Value1 = 1 ;
int Value2 = 2 ;
int Hold = Value1;
Value1 = Value2;
Value2 = Hold;
We swapped the values of objects Value1 and Value2 usingHold as temporary holder for Value1s starting value!
2
1
Value1
Value2
1Hold
-
8/6/2019 Operator & Expressions
21/46
Incrementing
int i = 1;i 1
-
8/6/2019 Operator & Expressions
22/46
-
8/6/2019 Operator & Expressions
23/46
Const Definitions
Modifier const indicates that an object cannot be changed
Object is read-only
Useful when defining objects representing physical andmathematical constants
const float Pi = .1 15;
Value has a name that can be used throughout the program
const int SampleSize = 1 ;
Makes changing the constant easy
Only need to change the definition and recompile
-
8/6/2019 Operator & Expressions
24/46
Assignment Conversions
Floating-point expression assigned to an integer object istruncated
Integer expression assigned to a floating-point object isconverted to a floating-point value
Consider
float y = 2. ;
int i = 15;
int j = 1 ;i = y; // i is now 2
cout
-
8/6/2019 Operator & Expressions
25/46
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
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 theirown types and classes
-
8/6/2019 Operator & Expressions
26/46
Examples
Standard Template Library (STL) provides class string
EzWindows library provides several graphical types and classes
SimpleWindow is a class for creating and manipulatingwindow objects
RectangleShape is a class for creating and manipulating
rectangle objects
-
8/6/2019 Operator & Expressions
27/46
-
8/6/2019 Operator & Expressions
28/46
Nonfundamental Types
To access a library use a preprocessor directive to add itsdefinitions to your program file
#include
The using statement makes syntax less clumsy Without it
std::string s = "Sharp";
std::string t = "Spiffy";
With it
using namespace std; // std contains string
string s = "Sharp";
string t = "Spiffy";
-
8/6/2019 Operator & Expressions
29/46
EzWindows Library Objects
Definitions are the same form as other objects
Example
SimpleWindow W;
Most non-fundamental classes have been created so that anobject is automatically initialized to a sensible value
SimpleWindow objects have member functions to process
messages to manipulate the objects
Most important member function is Open() which causesthe object to be displayed on the screen
Example
W.Open();
-
8/6/2019 Operator & Expressions
30/46
-
8/6/2019 Operator & Expressions
31/46
An EzWindows Program
#include
using namespace std;
#include "ezwin.h"
int ApiMain() {
SimpleWindow W("A Window", 12, 12);
W.Open();
cout a;
return ;
}
-
8/6/2019 Operator & Expressions
32/46
An EzWindows Project File
-
8/6/2019 Operator & Expressions
33/46
-
8/6/2019 Operator & Expressions
34/46
Sample Display Behavior
-
8/6/2019 Operator & Expressions
35/46
RectangleShape Objects
EzWindows also provides RectangleShape for manipulatingrectangles
RectangleShape objects can specify the following attributes
SimpleWindow object that contains the rectangle (mandatory)
Offset from left edge of the SimpleWindow
Offset from top edge of the SimpleWindow
Offsets are measured in centimeters from rectangle center
Width in centimeters
Height in centimeters
Color
color is an EzWindows type
-
8/6/2019 Operator & Expressions
36/46
RectangleShape Objects
Examples
SimpleWindow W1("My Window", 2 , 2 );
SimpleWindow W2("My Other Window", 15, 1 );
RectangleShape R(W1, , 2, Blue, , 2);
RectangleShape S(W2, 5, 2, Red, 1, 1);
RectangleShape T(W1, , 1, Black, , 5);
RectangleShape U(W1, , );
-
8/6/2019 Operator & Expressions
37/46
RectangleShape Objects
Some RectangleShape member functions for processingmessages
Draw()
Causes rectangle to be displayed in its associatedwindow
GetWidth()
Returns width of object in centimeters
GetHeight()
Returns height of object in centimeters
SetSize()
Takes two attributes -- a width and height -- that areused to reset dimensions of the rectangle
-
8/6/2019 Operator & Expressions
38/46
Another EzWindows Program
#include
using namespace std;
#include "rect.h"
int ApiMain() {
SimpleWindow W("Rectangular Fun", 12, 12);
W.Open();
RectangleShape R(W, 5. , 2.5, Blue, 1, 2);
R.Draw();
cout Response;
return ;
}
-
8/6/2019 Operator & Expressions
39/46
Sample Display Behavior
-
8/6/2019 Operator & Expressions
40/46
Compound Assignment
C++ has a large set of operators for applying an operation toan object and then storing the result back into the object
Examples
int i = ;
i += ; // i is now 7
cout
-
8/6/2019 Operator & Expressions
41/46
Increment and Decrement
C++ has special operators for incrementing or decrementing anobject by one
Examples
int k = ;++k; // k is 5
k++; // k is 6
cout
-
8/6/2019 Operator & Expressions
42/46
Class string
Some string member functions
size() determines number of characters in the string
string Saying = "Rambling with
Gambling";
cout
-
8/6/2019 Operator & Expressions
43/46
Class string
Auxiliary functions and operators
getline() extracts the next input line
string Response;
cout
-
8/6/2019 Operator & Expressions
44/46
Class string
Auxiliary operators
+ string concatenation
string Part1 = "Me";
string Part2 = " and ";
string Part = "You";
string All = Part1 + Part2 + Part ;
+= compound concatenation assignment
string ThePlace = "Brooklyn";
ThePlace += ", NY";
-
8/6/2019 Operator & Expressions
45/46
#include
using namespace std;
int main() {cout
-
8/6/2019 Operator & Expressions
46/46