The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. |
Home Structures Operations on Structures | ||
See also: Order of Operations | ||
Operations on Structures
Most of the operators we have been using on other types, like mathematical operators ( +, %, etc.) and comparison operators (==, >, etc.), do not work on structures. Actually, it is possible to define the meaning of these operators for the new type, but we won't do that in this book.
The values in squiggly braces get assigned to the instance variables of the structure one by one, in order. So in this case, x gets the first value and y gets the second. Unfortunately, this syntax can be used only in an initialization, not in an assignment statement. So the following is illegal. Point blank;blank = { 3.0, 4.0 }; // WRONG !! You might wonder why this perfectly reasonable statement should be illegal, and there is no good answer. I'm sorry. On the other hand, it is legal to assign one structure to another. For example: Point p1 = { 3.0, 4.0 };Point p2 = p1; cout << p2.x << ", " << p2.y << endl; The output of this program is 3, 4.
|
||
Home Structures Operations on Structures |