Game Development Reference
In-Depth Information
Chapter 3
Creating Calculators
with Operators
C++ provides operators that allow you to express all sorts of mathematical formulae in computer
programs. You have access to operators that allow us to carry out algebra, compare values, and
even manipulate the individual bit pattern of a variable's value directly. This chapter is going to cover
these C++ operators and end by having you create a simple command line calculator.
The Assignment Operator
The first operator we are going to look at is the assignment operator. We have already been using
the assignment operator in the previous chapters as it is the operator that allows us to assign a value
to a variable. This operator uses the = symbol:
int number1 = 1;
As you can see we have assigned the value of 1 to an integer variable named number1 . We can also
assign variable values to other variables:
int number2 = number1;
Here the program will read the value stored in number1 and assign that value to the variable number2 .
As we saw in the previous chapter, the assignment operator can also automatically convert from one
variable type to another.
char charNumber = 1;
int intNumber = charNumber;
The assignment is one of the most widely used operators in C++; thankfully, it's also easy to use and
fairly obvious in nature.
17
 
Search WWH ::




Custom Search