Download presentation
Presentation is loading. Please wait.
Published byAllan Roberts Modified over 9 years ago
1
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
2
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 2 3.2 Variables and Constants
3
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 3 A variable is actually a place to store information in a computer. It is a location (or series of locations) in the memory. The name of a variable can be considered as a label of that piece of memory. 0000000100020003000400050006000700080009 Address Memory Variables char a int b short int c bool d 100A213A51442000 Variables and Memory One address for one byte.
4
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 4 In memory, all data are groups of ‘1’ and ‘0’, byte by byte. Depending on how we interpret the data, different types of variables can be identified in the memory. Size of Variables Type bool unsigned short int short int unsigned long int long int unsigned int int char float double Size 1 byte 2 bytes 4 bytes 2, 4 bytes 1 byte 4 bytes 8 bytes Values true or false (1 or 0) 0 to 65,535 -32,768 to 32,767 0 to 4,294,967,295 -2,147,483,648 to 2,147,483,647 256 character values (+/-)1.2e 38 to (+/-)3.4e38 (+/-)2.2e 308 to (+/-)1.8e308 p.8 p.9
5
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 5 #include using namespace std; int main() { cout << "The size of an int is:\t\t" << sizeof(int) << " bytes.\n"; cout << "The size of a short int is:\t" << sizeof(short) << " bytes.\n"; cout << "The size of a long int is:\t" << sizeof(long) << " bytes.\n"; cout << "The size of a char is:\t\t" << sizeof(char) << " bytes.\n"; cout << "The size of a float is:\t\t" << sizeof(float) << " bytes.\n"; cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n"; return 0; } Exercise 3.2a a.Build the project and note the results. b.Add lines for bool, unsigned int, unsigned long int, and unsigned int.
6
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 6 Before we use a variable, we need to declare its type, so that the compiler can reserve suitable memory space for it. Variables are declared in this way: It defines myVariable to be an integer. Hence 4 bytes will be reserved for its storage in memory. The name of the variable is case sensitive, hence myVariable is NOT the same as myvariable. Declaring Variables int myVariable;
7
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 7 We can declare one or more variables of the same kind at once It defines myVar1, myVar2, yourVar1, yourVar2 are all integers. Some names are keywords of the C++ language that cannot be used as variable names. e.g. return, while, for, if, etc. We can even initialize the value of the variables when making the declaration. Declaring Variables int myVar1, myVar2, yourVar1, yourVar2; int myVar1 = 10, myVar2, yourVar1, yourVar2 = 5;
8
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 8 #include using namespace std; int main() {unsigned short int smallNumber; smallNumber = 65535; // 1111 1111 1111 1111 = 65535 cout << "small number:" << smallNumber << endl; smallNumber++; //1 0000 0000 0000 0000 = 0 cout << "small number:" << smallNumber << endl; smallNumber++; // 0000 0000 0000 0001 = 1 cout << "small number:" << smallNumber << endl; return 0; } Maximum and Minimum Each data type has its own maximum and minimum limits. When the value goes beyond those limits, unexpected behavior may result. p.4
9
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 9 #include using namespace std; int main() { short int smallNumber; smallNumber = 32767; // 0111 1111 1111 1111 = 32767 cout << "small number:" << smallNumber << endl; smallNumber++; // 1000 0000 0000 0000 = -32768 cout << "small number:" << smallNumber << endl; smallNumber++; // 1000 0000 0000 0001 = -32767 cout << "small number:" << smallNumber << endl; return 0; } Maximum and Minimum Signed integers do not behave in the same way. p.4
10
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 10 #include using namespace std; int main() {// ASCII code of 5 is 53 char c = 53, d = '5'; // They are the same int e = 53; cout << "c = " << c << endl; // c = 5 cout << "d = " << d << endl; // d = 5 cout << "e = " << e << endl; // e = 53 return 0; } Characters Besides numbers, C++ has also a data type called char, i.e. character. If a variable is declared as char, the compiler will consider the variable as an 8-bit ASCII character.
11
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 11 ASCII Table 4848 0 4949 1 5050 2 5151 3 5252 4 5353 5 5454 65 7 5656 8 5757 9 : 6565 A6 B 6767 C 6868 D 6969 E 7070 F 7171 G 7272 H 7373 I 7474 J : 9797 a 9898 b9 c 100100 d 101101 e 102102 f 103103 g 104104 h 105105 i 106106 j Details of the table can be found in many places, for instance, http://web.cs.mun.ca/~michael/c/ascii-table.html
12
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 12 Special Printing Characters C++ compiler recognizes some special characters for formatting. Start with an escape character \ (e.g. \n means new line). Character \n \t \b \" \' \? \\ What it Means new line tab backspace double quote single quote question mark backslash escape sequence
13
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 13 Constants Unlike variable, constants cannot be changed. The value of a constant will be fixed until the end of the program. There are two types of constants: Literal Constants - come with the language e.g. a number 39 (you cannot assign another value to 39. ) Symbolic Constants - Like variables, users define a special name as a label for it. Unlike variables, it cannot be changed once it is initialized.
14
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 14 A symbolic constant can be defined in two ways. 1. Old way - use the preprocessor command #define No type needs to be defined for studentPerClass. Preprocessor just replaces the word studentPerClass with 87 whenever it is found in the program. 2. A better way - use the keyword const Variable studentPerClass now has a fixed value 87. It has a type now. This feature helps the compiler to debug the program. Defining Symbolic Constants #define studentPerClass 87 const unsigned short int studentPerClass = 87 ;
15
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 15 Why do we need Constants? Help debugging the program Compiler will automatically check if a constant is modified by the program later (and reports it if modified.) Improve readability Give the value a more meaningful name. e.g. rather than writing 360, can use degreeInACircle Easy modification If a constant really needs to be changed, only need to change a single line in the source program.
16
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 16 # include using namespace std; int main() {const int totalStudentNumber = 87; // Student no. must < 87 int num; cout << "Enter a student number: "; cin >> num; if (num > totalStudentNumber) cout << "\n Number not valid!!!\n"; : cout << "\n There are " << totalStudentNumber << " students in this class\n"; : return 0; } Give better readability Modify once and apply to whole program Give better readability Modify once and apply to whole program
17
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 17 Enumerated constants allow one to create a new type that contains a number of constants Makes COLOR the name of the new type. Set RED = 0, BLUE = 1, GREEN = 2, WHITE = 3, BLACK = 4 Another example Makes COLOR2 the name of the new type. Set RED = 100, BLUE = 101, GREEN = 500, WHITE = 501, BLACK = 700 Enumerated Constants enum COLOR {RED, BLUE, GREEN, WHITE, BLACK}; enum COLOR2 {RED=100, BLUE, GREEN=500, WHITE, BLACK=700};
18
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 18 # include using namespace std; int main() {const int Sunday = 0; const int Monday = 1; const int Tuesday = 2; const int Wednesday = 3; const int Thursday = 4; const int Friday = 5; const int Saturday = 6; int choice; cout << "Enter a day (0-6): "; cin >> choice; if (choice == Sunday || choice == Saturday) cout << "\nHave a nice weekend!\n"; else cout << "\nToday is not holiday yet.\n"; return 0; } Exercise 3.2b a.Build the project and note the result. b.Use enumerated constants method to simplify the program.
19
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 19 References Teach Yourself C++ in 21 Days, Second Edition (http://newdata.box.sk/bx/c/)
20
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 20 Acknowledgment The slides are based on the set developed by Dr. Frank Leung (EIE).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.