Today's "Class"
Today's chapter covered variables and constants in a bit more depth. Including the different types of variables and what their limitations were and appropriate uses.
Variable Types
According to the book, there are 3 basic types of variables: integers, characters, and floating-point variables. The chapter mentioned that you can define your own variable types as well, but we won't discuss that until the end of the week looks like.
bool
bool (no doubt short for boolean) variables are true or false, 0 or 1. They take up 1 byte on your the RAM.
unsigned short {int}
short integers are 2 bytes on the RAM. unsigned short integers have a range of 0 to 65,535. The book had a few example programs of what would happen if you assign a value that exceeds this max. As it turns out, the value is "wrapped" around like an odometer in a car. Good to know in case you get some strange values in a program in the future.
{signed} short {int}
short integers default to signed and have a range of -32,768 to 32,767. You can explicitly define a variable as a signed short, or just type short. When the values "wrap" on a signed integer, it goes from the highest positive value (32,767) to the highest negative value (-32,768) and then keeps adding to the value (which would bring the negative number down toward 0, and then once 0 is passed it would add the numbers to the positive until we reach the max again). If this is confusing... try to write a program that adds to a maxed out variable and see what happens.
unsigned long {int}
long integers set aside 4 bytes of space on RAM. unsigned long integers have a range of 0 to 4,294,967,295. If you exceed the max, it wraps around... look at the unsigned short type for specifics.
{signed} long {int}
signed long integers have a range of -2,147,483,648 to 2,147,483,647.
int
int can be either a short or a long, depending on the machine running it. It's good practice to specify the variable as either short or long instead of using int because of this very detail. I think on any new computer or OS, it would be a long. It is on my computer and it was on the author's computer as well. int can be either signed or unsigned, and it defaults on signed.
char
1 byte and it is any of the ASCII default text characters. The book had an interesting little program that cycled through the characters that are assigned to the numbers 32 - 128. A char would be "a", or "A", "0", "8", etc. Any character on your keyboard is an example. It is a "character". Hopefully that is clear enough for you. I understand it fine, but don't know how else to describe it.
float
float variables are interesting. They let you go to some extremely large values and they take up 4 bytes. But, there is a definite problem with them. They are NOT accurate. If you need a decimal number, use a float (or a double). Range is from 1.2e-38 to 3.4 e38. If my scientific notation is remembered correctly, that is from 0.000000000000000000000000000000000000012 to 340,000,000,000,000,000,000,000,000,000,000,000,000. But you better not quote me on that, High School was a long time ago. The accuracy is NOT perfect for float variables. It remembers the first 7 digits and the rest are "lost".
double
double variables are "double" the size of float variables. They are basically the same as float variables. They have a range of 2.2e-308 to 1.8e308. I will not type out what that translates into. It is a ridiculously huge range though. Instead of remembering 7 digits, double remembers 19 digits.
Constants
Constants are variables that NEVER change in the program (well, never change until you recompile the program at least). There are two ways in the book on how to define constants, one is labeled as obsolete, so I am going to ignore that it exists. So, then the one way you define a constant goes something like this:
const unsigned short daysInWeek = 7;
You can obviously define the constant as something other than an unsigned short. This sets up a "symbolic constant", which is basically saying that "daysInWeek" is synonymous with the value "7". Incidentally, a numerical value like "7" is also a constant of sorts. Those sort of constants are called "literal constants" (since they are LITERALLY constants).
Summary and Workshop
The lesson wrapped up by doing a brief overview of what was covered. The author brought out that C++ doesn't REQUIRE you to know how the physical components in a computer handles data, but that knowledge would come in handy. C++ has a lot of power and has a ton of control over your computer.
The workshop mostly dealt with trying to decide which type of variable you need in order to store certain values provided in the book. Wasn't that difficult to me and I was able to fly through it.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment