C++ Types
C++ types are substantially the same as in C. All data has an associated type, which tells:
- The possible values it can have
- How it is stored internally in the MAC
- Which operations make sense on the data
Different Data Types
char
- Composed of 8 bits = 1byte
short
- Composed of 16 bits = 2 bytes
- As a signed integer the range is -32768 to +32767
int
- Composed of 32 bits = 4 bytes
- As a signed interger the range is -2,147,483,648 to +2,147,483,647
- This is what we will usually (but not always) use
long
- Same as the int on the MAC
- The long is longer on some other computers
Writing Integer Values
An integer is a sequence of digits with optional leading sign.
There is no leading 0 unless we want just 0. Commas
should also be omitted when establishing integer values.
Floating Data Types
- Used to express scientific notation, e.g. 6.02*10^23. This would be represented as:
- Sign would be +
- Mantissa would be 6.02
- Exponent would be 23
- Used for numbers that:
- Have fractional parts
- Are too large to store in an int
- The exponent can be signed, so we can store numbers with large or small absolute values, as well as 0
Some floating data types are:
float
- Composed of 4 bytes
- The range is (+)(-)1.17549*10^-38 to (+)(-)3.40282*10^38
double
- Composed of 10 bytes
- The range is (+)(-)1.6805*10^-4932 to (+)(-)1.18973*10^4932
long double
- Same as the double on a MAC
Some things to note about writing floating values!!
- Always write them with a decimal point
- 3.14159
- 3.0 (Different from int 3)
- Can use C++ version of scientific notation
- Always use double rather than float
- More precision
- But takes more memory
Since float and double are so much more flexible, why use integer
types at all? First of all, integers are quicker to use than floats
because of the brevity of internal representation, but they are not
capable of giving the amount of precision that a float can offer. So
it is a trade-off between speed and expressive power. See Demo
Precision.cpp
Some things to note about writing char values!!
- They are enclosed in single quotes
- 'C', 'Y', '+', '2' <- Not the same as integer 2
- Quoted char is just shorthand for the code used to store it internally as an integer which ranges from 0 to 255 = 2^8 - 1. See Demo charcode.cpp
- Special chars to watch out for:
- '\n' - newline character
- '\'' - single quote
- '\\' - gives one backslash
Variables
Variables are names by which you store and retrieve data. The rules for creating variables are:
- Name begins with a letter
- Followed only by uppercase, lowercase, digits, or the underscore
- NOTE: All variables are case sensitive
Declaration Examples
int rocky, Bullwinkle;
double Agent007, James_Bond;
Declarations do 2 things:
- Creates a place to put the variables and gives a name to it.
- Says what kind (type) of value you'll find there.
Other variable stuff
- Must declare variables before using them.
- Until you put a value in the variable, the value is undefined or garbage because what is in that memory location is any old bunch of bits
- You can put a value into a variable with an assignment which can be done in either of two ways:
int rocky;
rocky = 5;
int rocky = 5;
If you intend the value to never change declare it to be a const
- const double PI = 3.14159;
- const int DEADLY_SINS = 7, DALMATIONS = 101;
- A MATTER OF STYLE: const names are rule-of-thumb in uppercase
Can also put a value in a variable with cin.
See Demo geometry.cpp
To Index
Previous
Next