As a simple example of a compound structure, consider the concept of a point in the x-y plane. A point is made up as a pair of two numbers that represent the x and y coordinates that can be treated collectively. In mathematical notation, points are often written in parentheses, with a comma separating the coordinates.
A natural way to represent a point in C++ is with two doubles, and we can represent that using a struct definition:
// Here we define a new "type" called point.
struct point {
double x, y; //point has two member variables.
};
|
| WARNING | It probably seems odd to put a semi-colon after the "}", but that is how a struct definition ends. A very common syntax error is to leave off the semi-colon at the end of a structure definition. |
|---|
int or float variables.
point myPoint; // declare a variable of type point myPoint.x = 3.5; // assign the x member variable the value 3.5 myPoint.y = 4.2; // assign the y member variable the value 4.2 |
| ||||||||
struct point {
double x;
double y;
};
int main( ) {
double z, x;
point myPoint;
myPoint.x = 3.5; // These two are the same as the statements above
myPoint.y = 4.2;
z = myPoint.x; // z would have the value 3.5 after this statement
x = myPoint.x; // x here is a different variable from myPoint.x
}
|
z = myPoint.x" goes to the object named myPoint, find
the member variable x, and assign the value in that member variable to
z.
A similar operation occurs for the last statement.
Note that local variable x and the member variable myPoint.x are
different; the dot notation uniquely determines which variable is referred to at
a point in time.
You can also use member variables in cout or mathematical operations,
such as:
// The first statement outputs the string: myPoint = (3.5, 4.2)
// The second statement adds 10 to what is in myPoint.x
// The last statement outputs the new value in myPoint.x: new myPoint.x = 13.5
cout << "myPoint = (" << myPoint.x << ", " << myPoint.y << ")\n";
myPoint.x = myPoint.x + 10;
cout << "new myPoint.x = " << myPoint.x << endl;
|
+, %, etc.) and comparison operators (==, >,
etc.), do not work on structures.
However, the assignment operator does work on a structure in two ways: to
initialize its member variables or to copy the values of its member variables to
another.
You can initialize the values of a structure's member variables by using the
following format:
structure_type identifier = { init_member1, init_member2, ... };
|
point myPoint = { 3.5, 4.2 };
|
point myPoint = { 3.5 };
| |||||||||||||||
|
| |||||||||||||||
Unfortunately, this syntax can be used only in an initialization and not in an assignment statement. The following is illegal.
point mypoint;
mypoint = { 3.5, 4.2 }; // WRONG !!
|
point p1 = { 3.5, 4.2 };
point p2 = p1;
cout << "(" << p2.x << ", " << p2.y << ")";
|
void printPoint( point p ) {
cout << "(" << p.x << ", " << p.y << ")";
}
|
printPoint(...).
Suppose the argument to the function was myPoint defined and initialized above,
it will output the string (3.5, 4.2)".
Changing the values of the structure p will not change the variable
myPoint.
If inside printPoint(...) there was the statement "p.x = 10.0",
the structures in the program will be:
|
| |||||||||||||||
reflect(...) function:
void reflect( point& p ) { // Note the ampersand before p
double temp = p.x;
p.x = p.y;
p.y = temp;
}
|
| Code Fragment | Description | |
|---|---|---|
point myPoint printPoint( myPoint ); reflect( myPoint ); printPoint( myPoint ); |
myPoint has the values: x=3.5 y=4.2 output:(3.5, 4.2) swap the x and y values output: (4.2, 3.5) |
bool.
It is a basic data type like int, but instead of storing integers
or doubles or characters, variables of type bool store either
the value true or the value false.
The words true and false are reserved
C++ words.
An example variable declaration and assignment of type bool is:
bool testvariable;
testvariable = true;
Create a program in a source file called YourLastName_306A12.cpp that
point structure as described above.
true is the two points are the same and
false otherwise.
double number.
Recall that distance between (x1, y1)
and (x2, y2) is given by the distance
formula
| WARNING: |
Do NOT name this function "distance" as it will cause conflicts with a built-in C++
distance(...) function that works differently.
|
|---|
printPoint(...) function that
was given above to display the point coordinates.
something is not a good name, but calculatesInverse
is.
// Course: CSC 306 Introduction to Programming with C++
// Name: Your Name
// Assignment #12: <Put a brief sentence about your program here.>
/*
Purpose: <Put a more in-depth description of the program here.>
*/
When you are finished writing and testing your assignment, drop your source code file into the CSC306_A12 dropbox on the Academic server.