Operations: Coding and Programming in C++ Language II (7261-229) - Operations
Operations: Coding and Programming in C++ Language II (7261-229) - Operations
OPERATIONS
Aims
2· handle constants
3· declare enumerators
4· manipulate arrays
C++ has many in built data types, but in this course we are only really
concerned with integers (int), characters (char) and floats (float).
When manipulating values that can not have fractional portions, such as
calendar dates, an integer is used. The following code sets ivalue1 to 17
and ivalue2 to 34:
int iValue1;
int iValue2;
iValue1 = 17;
iValue2 = iValue1 * 2;
Values that can have fractional portions, such as a persons height, are stored
in floats. The following code will leave fValue set to 8.5, note when
assigning a constant value (such as 17.0) to a float the number should have a
suffix of l otherwise the compiler will treat it as a double type and generate a
warning:
float fValue;
fValue = 17.0f;
fValue = fValue / 2;
Characters are used to store individual letters, such as ‘A’, ‘F’, etc. The
following code leaves cValue set to ‘A’ (integer value 65).
char cValue;
cValue = 'Z';
cValue = cValue - 'Y' + 64;
Handle constants
The latter is generally preferred in C++ circles, but both are equally
acceptable.
Declare enumerators
So far we have met the standard data types char, int and float. C++
provides many more, but even these may not meet the requirements of the
programmer.
As an example, a diary application needs to store the current day of the week
- it is possible to use and integer with 0 representing Sunday and 6 Saturday
but this is unfriendly. It would be very useful if a type existed that could take
the values sun, mon, tue, etc. through to sat. This would permit the
developer to refer to a given day as tue instead of 2 - far friendlier.
In fact it is possible for developers to define their own data types and achieve
exactly this flexibility using enumeration types. In the days of the week
example an enumeration type is declared as follows:
enum day {sun, mon, tue, wed, thu, fri, sat};
C++ stores enumeration types as integers, the first enumerator (in the above
example sun) has a value of 0 with each successive enumerator being one
larger than the previous (i.e. mon is 1, tue is 2, etc.)
yesterday = mon;
results in an output of 1.
Manipulate arrays
In the real world problems are rarely made up from single values,
temperatures over several days need to be averaged out, people have names
longer than one character, football results need a pair of numbers, etc.
Single instances of floats, chars and integers can not meet these
requirements. Instead several values need to be treated as a whole - this can
be achieved through arrays.
Any data type can be used in an array, but perhaps the most common use of
arrays is to store a series of characters (a string). For example to hold a
weeks worth of temperatures the following declaration is made:
float fTemperatures [7];
Likewise, the following sets the fifth element to the value 22.45:
fTemperatures [4] = 22.45f;
szName1 = szName2;
As integers can only hold whole numbers, division operations can produce
unexpected results. For example the following code set i3 to 3 - the fractional
part is lost.
int i1 = 10;
int i2 = 3;
int i3 = i1 / i2;
In order to discover the fractional portion of the division operation the modulus
operator is used, as in:
int i4 = i1 % i2;
When dealing with floats these operators behave differently, for example the
following code sets f3 to 3.33333:
float f1 = 10.0f;
float f2 = 3.0f;
float f3 = f1 / f2;
i1 = i1 + i2; // Add i1 to i2
i1 += i2; // Add i1 to i2
i1 = i1 + 1; // Add 1 to i1
++i1; // Add 1 to i1
i2 = i2 - 1; // Subtract 1 from i2
--i2; // Subtract 1 from i2
Note, the ++ and -- operators can be placed in front or behind the variable to
be incremented. The position of the operator has a subtle effect on the order
of execution, when in front (++i1) the value of i1 is first incremented and
then used, when after (i1++) the value of i1 is used and then incremented. In
the example above the positioning of the increment or decrement operator
make no difference, but in some cases (such as loop control) it is critical.
Operator Precedence
( ) Highest, quantities in brackets are evaluated first, nested
brackets are evaluated innermost first.
++ -- Second highest
* / % Third highest
+ - Lowest
iValue = 2 * 3 + (4 - 2) / 2;