$\require{cancel}$
More on Integers.
- Overflow, underflow
- Your author defines overflow as when an arithmetic operator results in a number that is larger than the specified type can represent.
- This is for magnitude, so there can be overflow for negative numbers as well.
-
using
- in c++, the
using
construct allows us to "nickname" a type.
-
using identifier = type-id;
- This was done with
typedef typeid identifier;
in the past.
- See this reference for using.
- See this reference for typedef.
- An overflow program.
- see overflow.cpp.
- I added
using MyIntTypeT = int;
so I could change this easily.
- Remember, convention says to name types in Camel case, but start with an upper case letter, and end with a T.
- If I want, I can now change this to be any type I wish.
- I used limits to find the lowest and highest values, and added or subtracted one.
- make
- Make is a utility on *nix that allows us to compile programs more easily
- John will make extensive use of make next semester.
- I want to start using it right now.
- How many times have you messed up
g++ -o ...
- I can just type
make overflow
- And it will automatically do
g++ -o overflow overflow.cpp
, assuming that overflow.cpp exists in the directory.
-
- Output
- With integers, output is fairly straight forward.
-
setw(int)
works as expected.
-
setbase(int)
- Unfortunately only works for bases 8, 10, 16.
- Take a look at intOut.cpp.
- There are more output controls, but we will let those go for now.
- Integer input.
- The tool is stream extraction (>>)
- Like all stream extraction
- It skips whitespace.
- It attempts to read as much of an int as it can.
- When it stops,
- if it was successful it stores a value in the int and the stream tests as true.
- If it was not successful, and the stream tests as false.
- and stores
numeric_limits<type>::min()
.
-
- See intIn.cpp.