Chapter 3

Performing Mathematical Operations

In This Chapter

arrow Defining mathematical operators in C++

arrow Using the C++ mathematical operators

arrow Identifying expressions

arrow Increasing clarity with special mathematical operators

C++ offers all the common arithmetic operations: C++ programs can multiply, add, divide, and so forth. Programs have to be able to perform these operations to get anything done. What good is a health insurance program if it can’t calculate how much you’re supposed to (over) pay?

C++ operations look like the arithmetic operations you would perform on a piece of paper, except you have to declare any variables before you can use them (as detailed in Chapter 2):

  int var1;int var2 = 1;var1 = 2 * var2;

This code snippet declares two variables, var1 and var2. It initializes var2 to 1 and then stores the results of multiplying 2 times the value of var2 into var1.

This chapter describes the complete set of C++ mathematical operators.

Performing Simple Binary Arithmetic

A binary operator is one that has two arguments. If you can say var1 op var2, op must be a binary operator. ...

Get C++ For Dummies, 7th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.