Objects Types Variables and Constants Chapter 3 1
Objects Types, Variables, and Constants Chapter 3 1
Overview of C++ Statements Declarations; Objects Expressions; Other Operations 2
Expressions In a C++ program, any finite sequence of objects and operations that combine to produce a value is called an expression. Examples from our temperature problem: 1. 8 * celsius + 32 fahrenheit = 1. 8 * celsius + 32 Aside #1: expression; is a statement. ) Aside #2: expressions may be empty We focus now on C++ objects and will look at operations later. Even: 1. 8; 32; "Hello"; 1 + 1; ; ; 3
Study § 3. 2 Object Categories There are three kinds of objects: See Footnote on p. 48 Literals: unnamed objects having a value (0, -3, 2. 5, 2. 998 e 8, 'A', "Hellon", . . . ) Variables: named objects whose values can change during program execution Have memory Constants: named objects whose values cannot change during program execution allocated to them 4
5 basic types Literals – int literals are integers: -27, 0, 4, +4 Some Variations: unsigned, long, short Variations: float, long double – double literals are real numbers, and can be: fixed-point: -0. 333, 0. 5, 1. 414, . . . or E floating-point: 2. 998 e 8, 0. 2998 e 9, . . . – There are just two bool literals: false, true Enclose in ' – char literals are single characters: 'A', 'a', '9', '$', '? ', . . . Not inter– string literals are sequences of characters: changeable "Hello", "Goodbyen" Enclose in " 5
Variable Declarations Variables are used to store values, and can be either uninitialized or initialized. They must be declared before they are used. Declare Examples: only once int id. Number; int age = 18; Not a declaration double celsius, kelvin = 0; cin >> celsius; double fahrenheit = 1. 8 * celsius + 32; char letter. Grade = 'A'; bool ok, done = false; Pattern: Allocates a memory location for values of this type and associates its address with name type name; type name = expression; 6
Assignment Statements The value of a variable can be changed during execution by an assignment statement. Examples: age = 19; celsius = 1. 1 * celsius; letter. Grade = 'B'; done = true; Changes value in name's memory location to value of expression Pattern: name = expression; 7
Constant Declarations Constants are used to represent a value with a meaningful name, and must be initialized. They cannot be changed later. Examples: const int MAX_SCORE = 100; const double PI = 3. 14159265359; Could use in Proj. 1. 3 const char MIDDLE_INITIAL = 'A'; const string PROMPT = "Enter a number: "; Pattern: const type NAME = expression; Allocates a memory location for values of this type, associates its address with NAME, puts value of expression in it, and locks it. 8
Identifiers The name of an object is called an identifier (because it identifies the object). C++ identifiers must begin with a letter (underscores are permitted, but discouraged) followed by zero or more letters, digits or underscores. May not be C++ Valid: age, keywords (See Appendix B) r 2 d 2, my. GPA, MAX_SCORE, . . . Invalid: 123 go, coffee-time, sam's, $name, . . . See slide 24 9
Conventions Use meaningful identifiers for readability! We will use the following commonly-used convention to keep variable and constant objects distinct. • Constant names: all uppercase, with multiple words separated by underscores (e. g. , MAX_SCORE) • Variable names: all lowercase, but the first letter of each word after the first capitalized (e. g. , last. Name) “camelback” notation 10
char Objects Represented in memory by a code. p. 61 – ASCII uses 8 bits (1 byte) to represent a App. A character, allowing for 28 = 256 different characters. – Unicode's first version used 16 bits to represent Java a character, allowing for 216 = 65, 536 different See Other characters. Now, 16, 24, or 32 bits. Course Information class page Most commonly used code is ASCII: 'A' = 65 = 01000001 'a' = 97 = 01100001 '0' = 48 = 00110000 chars can be treated as small integers; e. g, . char ch 1 = 'A', ch 2; int x = 2*ch 1 + 1; ch 2 = ch 1 + 1; cout << x << ' ' << ch 2 << 'n'; Output? 131 B 11
Escape Characters C++ provides a number of escape characters: 'n' 't' 'v' 'f' 'a' '\' ''' '"' 'ooo' 'xhhh' newline character horizontal tab vertical tab form feed alert/bell backslash char apostrophe double quote char with octal code ooo char with hex code hhh See Appendix A 12
int Objects Three forms: – decimal (base-10): 0 or begin with a nonzero digit or sign (-45, -2, 0, +21, 36, 65536, . . . ) – octal (base-8): begin with a 0 followed by octal digits (01, 02, 03, 04, 05, 06, 07, See second item on Other Course Information class page 010 , 011 , 012, . . . , 017, 020, 021, …) – hexadecimal (base-16): begin with 0 x followed by digits with a, b, c, d, e, f = 10, 11, 12, 13, 14, 15 (0 x 1, 0 x 2, . . . , 0 x 7, 0 x 8, 0 x 9, 0 xa, 0 xb, 0 xc, 0 xd, 0 xe, 0 xf, 0 x 10, 0 x 11, . . . ) 13
unsigned Objects For objects whose values are never negative, C++ provides the unsigned type. They also may be expressed in decimal, octal, and hexadecimal forms. The major advantage of using unsigned instead of int when values being processed are known to be nonnegative is that larger values can be stored. 14
Ranges of Integer Values INT_MAX INT_MIN Using 32 bits, int values range from -231 (-2147483648) to 231 -1 (2147483647); unsigned values range from UINT_MAX 0 to 232 -1 (4294967295). An int value "loses" one of its bits to the sign, and so the maximum int value is about half of the maximum unsigned value. <climits> defines INT_MIN, INT_MAX, UINT_MAX, and other constants that specify ranges of C++ integers; <cfloat> does this for C++ real values. (See App. D) 15
Strange Behavior of Integer Overflow: when a value gets out of range; for example, an integer exceeds INT_MAX = 231 - 1 = 2147483647. //-- Program to demonstrate the effects of overflow Output: #include <iostream> using namespace std; 2 20 200000 2000000000 -1474836480 -1863462912 -1454759936 -1662697472 552894464 int main() { int number = 2; for (int i = 1; i <= 15; i = i + 1) { cout << number << 'n'; number = 10 * number; overflow } } 16
Why this strange behavior? Check INT_MAX + 1, INT_MAX + 2, . . . //-- Program to demonstrate the modular "wrap-around" effect of overflow #include <iostream> #include <climits> using namespace std; int main() { int number = INT_MAX - 3; for (int i = 1; i <= 7; i = i + 1) { cout << number << 'n'; number = number + 1; } } Output: INT_MAX INT_MIN 2147483644 2147483645 2147483646 2147483647 -2147483648 -2147483647 -2147483646 Arithmetic is modular INT_MIN. . . INT_MAX 17
int Representation Sec. 3. 3 Integers are often represented internally in the twos-complement format, where the highorder (leftmost) bit indicates the number’s sign: 210 = 0000000102 110 = 0000000012 010 = 000000002 -110 = 111111112 -210 = 1111111102 Here we show 16 bits, but 32 or 64 are more common. 18
Two's-Complement To find twos-complement representation of a negative number (e. g. , -12): Represent its absolute value in binary: Stop here for (0000001100) nonnegative numbers Invert (complement) the bits: (1111110011) Add 1 (1111110100) Try practice exercises — Week 3 of schedule Simpler: Flip all bits to the left of rightmost 1. 19
Why Use Two's Complement? Because usual algorithms for +, * work! + 0 1 0 0 1 10 * 0 1 0 0 0 1 5 + 7: 111¬¾¾ carry bits 0000000101 +0000000111 0000001100 5 + – 6: 0000000101 +1111111010 11111111 20
double Objects See Ch. 3, pp. 59 -61 Real values are often represented in 64 bits using the IEEE floating point standard: 5. 75 = 101. 112 = 1. 01112 22 +1023 "bias" Try practice exercises on course schedule exponent (11 bits) 0 1000001 01110000000000000000000000000 sign (1 bit) float (single precision): bias = 127; exponent: 8 bits mantissa: 23 bits mantissa (52 bits) 21
Problem: Binary representations of most real numbers do not terminate; e. g. , 13. 1 = 1101. 000110011… So they can't be stored exactly; error in this representation is called roundoff error. //-- Effect of roundoff error #include <iostream> #include <cmath> using namespace std; int main() { for (double { doouble y cout << x //if (x > } } x = 0; x != 50. 0; x = x + 0. 1) = x * sqrt(1 + sin(x)); << " " << y << 'n'; 60) break; Patriot missile failure See Other Course Information Execution: 0 0 0. 104873 0. 218968. . . 22. 6 14. 7859 22. 7001 13. 4103 22. 8001 11. 9884. . . 49. 9998 42. 937 50. 0998 45. 7826 50. 1998 48. 5246. . . 100. 099 76. 3241. . . 22
/* temperature. cpp converts a Celsius temperature to Fahrenheit. John Doe CPSC 104 X Lab 1 Jan. 6, 2011 Input: A Celsius temperature Output: Corresponding Fahrenheit temperature ------------------------*/ #include <iostream> using namespace std; // cin, cout, <<, >> int main() { cout << "John Doe CPSC 104 X -- Lab 1nn"; cout << "** Convert Celsius temps to Fahrenheit **n"; cout << "Please enter a temperature in Celsius: "; double celsius; cin >> celsius; double fahrenheit = 1. 8 * celsius + 32; } cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit. n"; 23
The name of the song is called "Haddocks' Eyes". "Oh, that's the name of the song, is it? " Alice said, trying to feel interested. "No, you don't understand, " the Knight said, looking a little vexed. "That's what the name is called. The name really is 'The Aged Man'. " 'Then I ought to have said, "That's what the song is called"? ' Alice corrected herself. "No, you oughtn't: that's quite another thing! The song is called 'Ways and Means': but that's only what it's called, you know!" "Well, what is the song, then? " said Alice, who was by this time completely bewildered. "I was coming to that, " the Knight said. "The song really is 'A-Sitting On a Gate': and the tune's my own invention. " • Lewis Carroll, Through The Looking Glass The name of the student's letter grade is called "letter. Grade. " "Oh, that's the name of the student's letter grade, is it? " Alice said, trying to feel interested. "No, you don't understand, " the Knight said, looking a little vexed. "That's what the name is called. The name really is 0 x 123 abc, a memory location that stores the student's letter grade. " 'Then I ought to have said, "The student's letter grade is 0 x 123 abc"? Alice corrected herself. "No, you oughtn't: that's quite another thing! The student's letter grade is called 'A': but that's only what it's called, you know!" "Well, what is the letter grade, then? " said Alice, who was by this time completely bewildered. "I was coming to that, " the Knight said. "The student's letter grade really is 10000001 and it's my own initialization. " 24
int age = 18; int id. Number, x, y; double celsius = 0, kelvin = 0; double fahrenheit = 1. 8 * celsius + 32; char letter. Grade = 'A', a, b, c; bool ok, done = false, d, e; int age: int id. Number: int x: int y: double celsius: double kelvin: double fahrenheit: char letter. Grade: char a: char b: char c: bool ok: bool done: bool d: bool e: Unix/Linux 0 xbfe 0 b 82 c 0 xbfe 0 b 828 0 xbfe 0 b 824 0 xbfe 0 b 820 0 xbfe 0 b 818 0 xbfe 0 b 810 0 xbfe 0 b 808 0 xbfe 0 b 807 0 xbfe 0 b 806 0 xbfe 0 b 805 0 xbfe 0 b 804 0 xbfe 0 b 803 0 xbfe 0 b 802 0 xbfe 0 b 801 0 xbfe 0 b 800 int age: 0012 FF 60 int id. Number: 0012 FF 54 int x: 0012 FF 48 int y: 0012 FF 3 C double celsius: 0012 FF 2 C double kelvin: 0012 FF 1 C double fahrenheit: 0012 FF 0 C char letter. Grade: 0012 FF 03 char a: 0012 FEF 7 char b: 0012 FEEB char c: 0012 FEDF bool ok: 0012 FED 3 bool done: 0012 FEC 7 bool d: 0012 FEBB bool e: 0012 FEAF Press any key to continue. . . Visual C++ A
Enter character (* 01000001 to stop): A Enter character (* 01000010 to stop): B Enter character (* 01000011 to stop): C Enter character (* 01011010 to stop): Z Enter character (* 01100001 to stop): a Enter character (* 00111011 to stop): ; Enter character (* 01000000 to stop): @ Enter character (* to stop): * B
Enter number (-999 to stop): 1 00000000000000001 Enter number (-999 to stop): 2 00000000000000010 Enter number (-999 to stop): 3 00000000000000011 Enter number (-999 to stop): 1024 00000000000100000 Enter number (-999 to stop): -1 1111111111111111 Enter number (-999 to stop): -2 11111111111111110 Enter number (-999 to stop): -3 11111111111111101 Enter number (-999 to stop): -999 C
Enter number (-99 to stop): 5. 75 01000000101110000000000 Enter number (-99 to stop): 1 001111111000000000000 Enter number (-99 to stop): . 25 001111101000000000000 Enter number (-99 to stop): -. 25 101111101000000000000 Enter number (-99 to stop): 13. 1 010000010101000110011010 Enter number (-99 to stop): -. 1 1011100110011001101 Enter number (-99 to stop): . 3 001111101001100110011010 Enter number (-99 to stop): -99 D
#include <iostream> #include <climits> #include <cfloat> using namespace std; int main() { cout << << << << << } INT_MAX = 2147483647 INT_MIN = -2147483648 UINT_MAX = 4294967295 SHRT_MAX = 32767 SHRT_MIN = -32768 LONG_MAX = 2147483647 LONG_MIN = -2147483648 CHAR_MAX = 127 CHAR_MIN = -128 FLT_MAX = 3. 40282 e+038 FLT_MIN = 1. 17549 e-038 DBL_MAX = 1. 79769 e+308 DBL_MIN = 2. 22507 e-308 LDBL_MAX = 1. 79769 e+308 LDBL_MIN = 2. 22507 e-308 FLT_EPSILON = 1. 19209 e-007 DBL_EPSILON = 2. 22045 e-016 LDBL_EPSILON = 2. 22045 e-016 "INT_MAX = " << INT_MAX << endl "INT_MIN = " << INT_MIN << endl "UINT_MAX = " << UINT_MAX << endl "SHRT_MAX = " << SHRT_MAX << endl "SHRT_MIN = " << SHRT_MIN << endl "LONG_MAX = " << LONG_MAX << endl "LONG_MIN = " << LONG_MIN << endl "CHAR_MAX = " << CHAR_MAX << endl "CHAR_MIN = " << CHAR_MIN << endl "FLT_MAX = " << FLT_MAX << endl "FLT_MIN = " << FLT_MIN << endl "DBL_MAX = " << DBL_MAX << endl "DBL_MIN = " << DBL_MIN << endl "LDBL_MAX = " << LDBL_MAX << endl "LDBL_MIN = " << LDBL_MIN << endl "FLT_EPSILON = " << FLT_EPSILON << endl "DBL_EPSILON = " << DBL_EPSILON << endl "LDBL_EPSILON = " << LDBL_EPSILON << endl; E
#include <iostream> #include <cmath> using namespace std; int main() { for (double x = 0; x != 50. 0; x = x +. 01) { double y = x * sqrt(1 + sin(x)); cout << x << " " << y << endl; } } 0 0 0. 0100499 0. 020199 0. 0304466 0. 0407919 0. 0512342 0. 0617727 0. 0724066 0. 0831352 0. 0939575 0. 104873 0. 115881 0. 12698. . . 4. 33 1. 16366 4. 34 1. 1362 4. 35 1. 10858 4. 36 1. 08078 4. 37001 1. 05283 4. 38001 1. 0247. . . 49. 9576 41. 7073 49. 9676 42. 0003 49. 9776 42. 2923 49. 9876 42. 5834 49. 9976 42. 8735 50. 0076 43. 1627 50. 0176 43. 4509 50. 0276 43. 7381 50. 0376 44. 0244 50. 0476 44. 3096 50. 0576 44. 5939 50. 0676 44. 8772. . . F
- Slides: 30