chapter three

3 Smooth operator –exploring C++ operators

 

This chapter covers

  • Getting to know the C++ operators
  • The use of arithmetical operators
  • Unraveling the logic behind logical operators and how to use them
  • Using various comparisons using comparison operators
  • Taking a firsthand look into the new C++ 20 three-way-comparison operator
  • Assigning value using assignment operators
  • Bitwise operators and their role.

In chapter 1 and 2 we helped you set a solid ground in understanding C++’s linguistic and syntactic logic, combined with an in depth understanding of variables and their role in memory allocation. In this chapter, we will move some steps forward, using everything you learned so far and adding to this solid base for learning some additional C++ language components and elements. You will learn all about operators, which can perform various operation in your code, and which you learned briefly about in the previous chapter. You will now get a better understanding of their role and usage with a lot of examples and code exercises. We also introduce the brand-new C++20 three-way comparison operator, also known as the spaceship operator, which can be used with some exciting capabilities within your code and brings a simple yet powerful method for comparison between objects.

3.1 Get in the groove of the C++ operators

3.2 Arithmetical operators for numerical and other operands

3.2.1 Precedence day – how C++ follow precedence rules

3.2.2 The division operator good to know-hows

3.2.3 The % modus operator for integer division

3.2.4 Unary operators - increment and decrement has never been easier

3.3 Logical operators - decision making in your program

3.4 Comparison operators – do more than just compare

3.4.1 Less than or greater than

3.4.2 Is it a bird? is it a plane? No! it's a spaceship operator!

3.4.3 The spaceship operator – it's not rocket science

3.5 Assignment operators – assign values to objects and variables

3.5.1 The anatomy of rvalues and lvalues

3.5.2 L is for Location

3.5.3 Get to know all the other Assignment operators

3.5.4 The promise to never break a promise

3.6 Bitwise operators – get into the bit (literally)

3.6.1 Why do we need to micro-manage bits?

3.6.2 Let's think for a bit

3.7 Summary