C++ has operators for five basic arithmetic calculations: addition, subtraction, multiplication, division, and taking the modulus.
Each of these operators uses two values called operands to calculate a final answer.
Together, the operator and its operands constitute an expression.
For example, consider the following statement:
int result = 4 + 2;
The values 4 and 2 are operands, the + symbol is the addition operator, and 4 + 2 is an expression whose value is 6.
Here are C++'s five basic arithmetic operators:
% operator works only with integers.
The following code does some C++ arithmetic.
#include <iostream>
using namespace std;
int main() // www.ja va 2 s . co m
{
float hats, my_head;
cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point
cout << "Enter a number: ";
cin >> hats;
cout << "Enter another number: ";
cin >> my_head;
cout << "hats = " << hats << "; my_head = " << my_head << endl;
cout << "hats + my_head = " << hats + my_head << endl;
cout << "hats - my_head = " << hats - my_head << endl;
cout << "hats * my_head = " << hats * my_head << endl;
cout << "hats / my_head = " << hats / my_head << endl;
return 0;
}
The code above generates the following result.
The behavior of the division operator (/) depends on the type of the operands.
If both operands are integers, C++ performs integer division.
That means any fractional part of the answer is discarded, making the result an integer.
If one or both operands are floating-point values, the fractional part is kept, making the result floating-point.
The following code illustrates how C++ division works with different types of values.
#include <iostream>
int main() //from w ww . j a va 2s . com
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << "Integer division: 9/4 = " << 9 / 4 << endl;
cout << "Floating-point division: 9.0/4.0 = ";
cout << 9.0 / 4.0 << endl;
cout << "Mixed division: 9.0/4 = " << 9.0 / 4 << endl;
cout << "double constants: 1e7/9.0 = ";
cout << 1.e7 / 9.0 << endl;
cout << "float constants: 1e7f/9.0f = ";
cout << 1.e7f / 9.0f << endl;
return 0;
}
The code above generates the following result.
The modulus operator returns the remainder of an integer division.
The following code uses % operator to convert lbs to stone.
#include <iostream>
int main() /*from w w w . j a v a2 s . co m*/
{
using namespace std;
const int Lbs_per_stn = 14;
int lbs;
cout << "Enter your weight in pounds: ";
cin >> lbs;
int stone = lbs / Lbs_per_stn; // whole stone
int pounds = lbs % Lbs_per_stn; // remainder in pounds
cout << lbs << " pounds are " << stone
<< " stone, " << pounds << " pound(s).\n";
return 0;
}
The code above generates the following result.
You've already seen two: the increment operator (++), and the decrement operator (--).
Each operator comes in two varieties.
The prefix version comes before the operand, as in ++x.
The postfix version comes after the operand, as in x++.
The two versions have the same effect on the operand, but they differ in terms of when they take place.
The following code demonstrates this difference for the increment operator.
#include <iostream>
using std::cout;//from w ww. j a v a 2 s . co m
int main(){
int a = 20;
int b = 20;
cout << "a = " << a << ": b = " << b << "\n";
cout << "a++ = " << a++ << ": ++b = " << ++b << "\n";
cout << "a = " << a << ": b = " << b << "\n";
return 0;
}
a++ means "use the current value of a in evaluating an expression, and then increment the value of a."
++b means "first increment the value of b and then use the new value in evaluating the expression."
The code above generates the following result.