Guide PDF
Guide PDF
C++ Statements
Assigns what
is on the right
side of the =
to what is on
= a = a + 13;
the left. If a
was 12, then a
now becomes
25.
Asks whether
what is on the
right of the =
is the same as Operators in
what is on the order of Precedence
== (a == 56)
left. If a is
25, then the () Function call
statement is
evaluated as [] Array subscript
false .
-> Indirect component
Groups code to selector
clear up any
ambiguities in . Direct component
selector
the order in
which they are (3+6) * (-2-a) / 5 ! Logical negation
()
evaluated. random(6);
Also encloses + Plus
the
parameters of - Minus
a function call.
() Expression
Groups larger for (i = 0; i < 10; i++) parentheses
{} blocks of {
sum = sum + i; * Multiply
code. }
/ Divide
An array
subscript, the
elements % Remainder (modulus
[] x[23] = 6; divide)
within a list or
table. < Less than
Terminates <= Less than or equal to
; every x = x * x;
statement. > Greater than
is the same
-= c -= 10; c = c - 10;
as
switch statements
if/else commands
while loops
do-while loops
for loops
nested for loops
break statement
goto statement
continue statement
return statement
Note: The "break;" statements transfer control below and outside the scope of the "switch."
if / else commands execute blocks of code only if the value in parentheses is true:
A single block of code may be executed:
if (zipCode == 90290) {
Edit1->Text = "You live in Topanga.";
}
if (zipCode == 27708) {
Edit1->Text = "You live near Duke.";
}
if (zipCode == 90077) {
Edit1->Text = "You live in Beverly Glen.";
}
if (zipCode == 27278) {
Edit1->Text = "You live in Hillsborough.";
}
if (sex == 0) {
Edit2->Text = "You are female.";
}
You will never allow an aircraft to land as long as the runway is not clear.
do {
dance();
}
while (musicStopped == false);
You will always have one dance, even if the music has stopped.
for loops take three parameters enabling you to initialize, terminate and increment the loop
counter:
Assuming you wished to do something with each agent from id 23 to 79,
setting first to 23 and last to 79 would do the job.
If you wanted every odd numbered agent from 23 to 79, then the last parameter should be id = id
+ 2:
The goto statement may be used to break out of a double or more deeply nested loop:
for (row = 0; row < 500; row ++) {
for (column = 0; column < 500; column ++) {
// look for row and column containing 77
if (77 == cell[row][column]) {
goto exit;
}
}
}
exit:
// we have now found the row and column
// of the first cell containing "77"
C++ Functions
Think of functions as agents: they sense, think and act.
By convention, any functions that you declare and define should be named with a lower-case initial letter.
Functions are the jobs you need to coordinate in order to get a larger project done. As a programmer, think of yourself as
the manager of an enterprise, the general of an army or the director of a movie. We'll pursue the cinematic metaphor.
Imagine yourself as the director of a movie. You are in charge of everyone. You coordinate the work of the actors,
cameramen, focus pullers, extras, carpenters, electricians, set decorators, prop handlers and sound recorders. Each has a job
to do, and each may delegate smaller jobs to those beneath them. But you are in command of the big picture. It is your job
to decide what is to be done, how to do it and who to assign to each task.
When you write an application, you are the director of a complex set of functions, your agents to whom you have assigned
these specific tasks. Think of C++ functions as your agents. It is up to you to specify what each wil do and how. As as the
creator and programmer of an artificial world, it is your responsibility to identify the jobs that need to be done, to describe
each job or process as a C++ function, and to organize these functions and their relationships to one another in order to
accomplish the task at hand. Think of your application as an organized group of functions; think of it as the social
organization of your crew of employees:
For further flexibility, any function, may call any other function, just as any crew member may call upon another for
assistance. You must organize all of this. You must describe the "chain of command:"
The Director calls, "Lights 3." (A function call with one parameter and one return value.)
The lighting person throws the switch on one flood and two spots, and reports back when this is done.
The Director calls, "Camera 2, 5." (A function call with two parameters and one return value.)
The Director calls, "Action." (A function call with no parameters and no return value.)
The actors begin to follow their scripts, the key grip pushes the camerman in his dolly along the track, the sound
person moves her microphones overhead...
And each person cues others to begin their jobs: extras swarm around the lead actors, assistants keep the cables
from tangling, the continuity person takes notes...
No one responds directly to the Director...
An Abstract Overview
A function may accept parameters, do something in its body, and directly return only one value.
However, it may also change the values of any number of global variables and the properties of any number of components.
It might be helpful to think of a function as an agent with a name, who can sense, think, and act:
Or you may wish to think of a function as black box with a name, which has inputs and one output:
You call a function by calling its name followed by the parameters, if there are any, in parentheses.
In summary:
To indicate that the function expects no parameters and returns no returnValue, we insert the word void where the
parameters would otherwise be.
// Ready to shoot!
void shooTheScene (void) {
quietOnTheSet();
lights();
camera();
action();
}
shootTheScene();
To indicate that the function returns a returnValue but expects no parameters, we insert the data type
of the returnValue before the function name and the word void where the parameters would otherwise be.
Since the function has an integer return value, we can use the function call itself as a variable in another statement:
The function call will be replaced by its return value, the number of feet that are left.
In other words, if there are less than 50 feet of film left, tell the cameraman to reload.
To indicate that the function expects no returnValue but does take parameters, we insert the word void
where the returnValue would otherwise be and data types and variable names of the parameters .
prepareForNextScene(3, 4);
To indicate that the function expects one boolian (true or false) returnValue and two parameters, we insert the data
type
of the returnValue bool and data types and variables int of the two parameters .
Since the function has a boolean returnValue, we can use the function call itself as a variable in a larger statement:
The function call will be replaced by its return value, and we can call action().
In other words, as long as the male and female leads are not ready, they should rehearse.
When they are ready, we will call for action.
Color
Theoryxxxxxxx
Understanding
Color Spaces RED GREEN BLUE BGRX
Result Notation
value value value hex code
Color is
represented by 255 255 255 WHITE W 0xFFFFFF
three variables,
with values from 0 255 0 0 RED 0x0000FF
Additive
to 255:
0 255 0 GREEN Primaries: 0x00FF00
red, green, blue.
RGB
0 0 255 BLUE 0xFF0000
The gamut of color
is often displayed 0 255 255 CYAN 0xFFFF00
as a color cube Subtractive X
with three 255 0 255 MAGENTA Primaries: 0xFF00FF
dimensions: CMY
255 255 0 YELLOW 0x00FFFF
red, green, blue.
0 0 0 BLACK K 0x000000
The 8 corners of
the color cube are
shown in the
rightmost column ->
Color Constants
Windows recognizes these color constants, representing the corners of the color cube (top row), plus darker variants
(bottom row).
Windows also recognizes other colors (see the color combo-box in the properties tab of most visual components).
All coordinates on your display are measured in pixels from the top left corner of the component.
Form Component
The Form component is the entire Window of your application. You can draw anywhere on a Window using Form1-
>Canvas .
PaintBox Component
The PaintBox component defines a smaller region within the Window on which your application can draw.
All measurements are relative to the top left corner of the PaintBox.
A PaintBox is useful for maintaining multiple visualizations on the screen at one time. Use it as you would use
Form1->Canvas .
Getting Pixels
int color, red, green, blue;
declares variables to receive colors
color = Form1->Canvas->Pixels[X][Y];
retrieves the RGB color triplet
red = GetRValue(color);
Properties
Canvas->Pen->Color = clBlack;
where Pen is the outline color. Black is the default
Canvas->Pen->Style = psSolid;
where Style can be psSolid, psClear, psDash, psDot, psDashDot or psDashDotDot. Solid is the
default
Canvas->Pen->Width = 1;
if Width is greater than one, then Style may revert to psSolid. One is the default
Canvas->Brush->Color = clWhite;
where Brush is the fill color. White is the default
Canvas->Brush->Style = bsSolid;
where Style can be bsSolid, bsClear, bsHorizontal, bsVertical, bsFDiagonal, bsBDiagonal,
bsCross or bsDiagonalCross. Solid is the default
Variables
int Integer
4 - 2,147,483,648 2,147,483,647
long Long Integer
- 3.4E308 3.4E38
float Floating Point (Real) 4
- 3.4 x 10^308 -3.4 x 10^308
- 1.7E308 1.8E308
double Double Floating Point (Real) 8
- 1.7 x 10^308 1.8 x 10^308
Variable Arrays
A variable may hold a single value like: myAge
A one-dimensional list of the ages of 25 students in this class: studentAge[25] with indices ranging from 0 to 24.
A two-dimensional table of the position of pieces on a chess board: chessGame[8][8] with indices ranging from 0 to 7.
Indices are always set off by square brackets [] and we always begin counting index values at zero.
Variable Scope