Use a "good" name
Tell where the information is stored
Tell what value is kept there
Tell what kind of information is stored
Example:
int transactionCount;
transactionCount = 5;
Only alphabetic characters, digits and _ are allowed
The first character is a name cannot be a digit
Uppercase and lowercase are different
Cannot use a C++ keyword (e.g. return, main, int, ...)
No length limit
Example:
int sum;
int sum_of_records;
int sumOfRecords;
A short integer is at least 2 bytes
A int integer is at least as big as short
A long integer is at least 4 bytes and at least as big as int
Unsigned variety cannot hold negative values
Example:
unsigned short change;
unsigned int rovert;
All the number values in C++ are dependent on computer systems
For portability, use long for big integers
#include <iostream.h> int main(void) { char c = 'M'; // assign ASCII code for M to c int i = c; // store same code in an int cout << "The ASCII code for " << c << " is " << i << "\n"; cout << "Add one to the character code:\n"; c = c + 1; i = c; cout << "The ASCII code for " << c << " is " << i << '\n'; // using the cout.put() member function to display a char cout << "Displaying char c using cout.put(c): "; cout.put(c); // using cout.put() to display a char constant cout.put('!'); cout << "\nDone\n"; return 0; }// The ASCII code for M is 77 // Add one to the character code: // The ASCII code for N is 78 // Displaying char c using cout.put(c): N! // Done
bool done = true;
if (done)
{
...
}
const int Months = 12; // value set once, cannot be changed later, works like a constant
We have float and double
Example:
float a = 2.34E+22;
Represent a greater range of values (than integer)
Might be slower in computation
Might lose precision (consider 0.0000000000000000001 - 0.00000000000000000000000095)
A data from that can hold several values of all of one type
An array declaration should indicate three things
The type of value to be stored in each element
The name of the array
The number of elements in the array
Example:
int months[12]; // Create an array of 12 int
int yams[3]; // creates array with three elements yams[0] = 7; // assign value to first element yams[1] = 8; yams[2] = 6;cout << "Total yams = "; cout << yams[0] + yams[1] + yams[2] << "\n";
A string is a series of characters stored in consecutive bytes of memory. All string ends with \0
char name[5]= {'s', 'i', 'n', 'n', 's'}; // not a stringchar name[5]= {'s', 'i', 'n', 'n', '\0'}; // a string// More ways of using itchar bird[10] = "Mr. Chunk"; // the \0 is impliedchar fish[] = "HelloFish"; // The C++ compile will internally know what the length is
#include <iostream.h> int main(void) { const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; cout << "Enter your name:\n"; cin.get(name, ArSize); // reads to newline cout << "Enter your favorite dessert:\n"; cin.get(dessert, ArSize); cout << "I have some delicious " << dessert; cout << " for you, " << name << ".\n"; return 0; }
// Enter your name: // Richard Sinn // Enter your favorite dessert: // I have some delicious for you, Richard Sinn
Array can only store elements of one type
Structure can store multiple of them
Example:
// structur.cpp -- a simple structure #include <iostream.h> struct inflatable // structure template { char name[20]; float volume; double price; }; int main(void) { inflatable guest = { "Glorious Gloria", // name value 1.88, // volume value 29.99 // price value }; // guest is a structure variable of type inflatable // It's initialized to the indicated values inflatable pal = { "Audacious Arthur", 3.12, 32.99 }; // pal is a second variable of type inflatable // NOTE: some implementations require using // static inflatable guest = cout << "Expand your guest list with " << guest.name; cout << " and " << pal.name << "!\n"; // pal.name is the name member of the pal variable cout << "You can have both for $"; cout << guest.price + pal.price << "!\n"; return 0; }