1 / 20

Learning the C++ language 3. The Nuts and Bolts of C++ (4)

Learning the C++ language 3. The Nuts and Bolts of C++ (4). 3.2 Variables and Constants. Variables and Memory. A variable is actually a place to store information in a computer. It is a location (or series of locations) in the memory.

hattie
Download Presentation

Learning the C++ language 3. The Nuts and Bolts of C++ (4)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Learning the C++ language 3. The Nuts and Bolts of C++ (4)

  2. 3.2 Variables and Constants

  3. Variables and Memory • 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. Variables char a int b short int c bool d Memory 10 0A 21 3A 51 44 20 00 Address 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 One address for one byte.

  4. Size of Variables • 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. Type bool unsigned short int short int unsigned long int long int unsigned int int char float double Size 1 byte 2 bytes 2 bytes 4 bytes 4 bytes 2, 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.9 p.8

  5. Exercise 3.2a a. Build the project and note the results. b. Add lines for bool, unsigned int, unsigned long int, and unsigned int. #include <iostream> 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; }

  6. Declaring Variables • 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 • myVariableis NOT the same as myvariable. int myVariable;

  7. Declaring Variables • 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. int myVar1, myVar2, yourVar1, yourVar2; int myVar1 = 10, myVar2, yourVar1, yourVar2 = 5;

  8. Maximum and Minimum p.4 • Each data type has its own maximum and minimum limits. • When the value goes beyond those limits, unexpected behavior may result. #include <iostream> 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; }

  9. Maximum and Minimum p.4 • Signed integers do not behave in the same way. #include <iostream> 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; }

  10. 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. #include <iostream> 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; }

  11. ASCII Table Details of the table can be found in many places, for instance, http://web.cs.mun.ca/~michael/c/ascii-table.html

  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. 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. Defining Symbolic Constants • 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 keywordconst • Variable studentPerClass now has a fixed value 87. • It has a type now. This feature helps the compiler to debug the program. #define studentPerClass 87 const unsigned short int studentPerClass = 87;

  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. Give better readability • Modify once and apply to whole program #include <iostream> 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; }

  17. Enumerated Constants • 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 enum COLOR {RED, BLUE, GREEN, WHITE, BLACK}; enum COLOR2 {RED=100, BLUE, GREEN=500, WHITE, BLACK=700};

  18. Exercise 3.2b #include <iostream> 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; } a. Build the project and note the result. b. Use enumerated constants method to simplify the program.

  19. References • Teach Yourself C++ in 21 Days, Second Edition (http://newdata.box.sk/bx/c/)

  20. Acknowledgment • The slides are based on the set developed by Dr. Frank Leung (EIE).

More Related