1 / 23

Objects

Objects. Types, Variables, and Constants. Chapter 3. 1. Overview of C++ Statements. C++ Statements Declarations; Expressions; Other Objects Operations. 2. Expressions.

Download Presentation

Objects

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. Objects Types, Variables, and Constants Chapter 3 1

  2. Overview of C++ Statements C++ Statements Declarations; Expressions; Other Objects Operations 2

  3. Expressions In a C++ program, any finite sequence of ________ and __________ that combine to produce a value is called an expression. Examples from our temperature problem: __________ 1.8 * celsius 1.8 * celsius + 32 ________________________________________ Even: 1.8;32;"Hello";1 + 1;;;;; Aside #1: expression;is a statement.) Aside #2: expressions may be empty We focus now on C++ objectsand will look at operations later. 3

  4. Object Categories See Footnoteon p.46 There are three kinds of objects: ___________: unnamed objects having a value (0, -3, 2.5, 2.998e8, 'A', "Hello\n", ...) __________: named objects whose values can change during program execution __________: named objects whose values cannot change during program execution Have memory allocated to them 4

  5. Not inter-changeable Literals 5 basic types Some Variations: unsigned, long, short Variations: float, long double • _______ literals are integers: -27, 0, 4, +4 • _______ literals are real numbers, and can be: fixed-point: -0.333, 0.5, 1.414, ... floating-point: 2.998e8, 0.2998e9, ... • There are just two _____ literals: false, true • _______ literals are single characters: 'A', 'a', '9', '$', '?', ... • _______ literals are sequences of characters: "Hello", "Goodbye", "Goodbye\n", ... or E Enclose in ' Enclose in " 5

  6. Variable Declarations Variables are used to store values, and can be either uninitialized or initialized. They must be _______________before they are used. Declare only once Examples: ___________________ ___________________ ___________________ ___________________ cin >> celsius; _____________________________________________________ char letterGrade = 'A'; bool ok, done = false; • Pattern: type name;type name = expression; Allocates a memory location for values of this type and associates its address with name 6

  7. Assignment Statements The value of a variable can be changed during execution by an assignment statement: Examples: _______________ ___________________________ letterGrade = 'B'; done = true; Pattern: name = expression; Changes value in name's memory location to value of expression 7

  8. Constant Declarations Constants are used to represent a value with a meaningful name, and must be initialized. They cannot be changed later. Examples: ____________________________________________ ____________________________________________ const char MIDDLE_INITIAL = 'A'; const string PROMPT = "Enter a number: "; Pattern: const type NAME = expression; Could use in Proj. 1.3 Allocates a memory location for values of this type, associates its address with NAME, puts value of expression in it, and locks it. 8

  9. Identifiers The name of an object is called an _________ (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. Valid: age, r2d2, myGPA, MAX_SCORE Invalid: 123go, coffee-time, sam's, $name May not be C++keywords(See Appendix B) 9

  10. Use meaningful identifiers for readability! Conventions 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., ___________) • Variable names: all lowercase, with the first letter of each word after the first capitalized (e.g., ___________) “camelback” notation 10

  11. char Objects p.60 App. A Represented in memory by a ________ • ASCII uses 8 bits (1 byte) to represent a character, allowing for 28 = 256 different characters • Unicode's first version used 16 bits to represent a character, allowing for 216 = 65,536 different characters. Now, 16, 24, or 32 bits. Most commonly used code in C++ is ASCII: 'A' = ________________ 'a' = 97 = 01100000 '0' = 48 = 00110000 Java chars can be treated as small integers; e.g,. char ch1 = 'A', ch2; int x = 2*ch1 + 1; ch2 = ch1 + 1; cout << x << ' ' << ch2 << '\n'; Output? ___________ 11

  12. Escape Characters C++ provides a number of _________characters: '\n' newline character '\t' horizontal tab '\v' vertical tab '\f' form feed '\a' alert/bell '\\' backslash char '\'' apostrophe '\"' double quote '\ooo' char with octal code ooo '\xhhh' char with hex code hhh See Appendix A 12

  13. int Objects Three forms: • decimal (base-10) begin with 0 or a _________ digit or sign (-45, -2, 0, +21, 36, 65536, ...) • octal (base 8): begin with a ___ followed by octal digits (01, 02, 03, 04, 05, 06, 07, • ________________________________________ ...) • hexadecimal(base 16): begin with ____followed by digits with a, b, c, d, e, f = 10, 11, 12, 13, 14, 15 (0x1, 0x2, ..., 0x7, 0x8, 0x9, 0x , 0x , 0x , 0x , 0x , 0x ,0x , 0x , ...) See second item on OtherCourse Information class page 13

  14. unsigned Objects For integer objects whose values are never negative, C++ provides the ______________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

  15. INT_MAX INT_MIN UINT_MAX Ranges of Integer Values Using 32 bits, int values range from -231 (-2147483648) to 231-1 (2147483647), whereas unsigned values range from 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 specifyranges of C++ integers; <cfloat> does thisfor C++ real values. (See App. D) 15

  16. overflow Strange Behavior of Integer Overflow Overflow: when a value gets out of range; for example, an integer exceeds INT_MAX = 231 - 1 = 2147483647. Output: 2 20 200 2000 20000 200000 2000000 20000000 200000000 2000000000 -1474836480 -1863462912 -1454759936 -1662697472 552894464 //-- Program to demonstrate the effects of overflow #include <iostream> using namespace std; int main() { int number = 2; for (int i = 1; i <= 15; i = i + 1) { cout << number << '\n'; number = 10 * number; } } 16

  17. INT_MAX INT_MIN 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:214748364421474836452147483646 2147483647 -2147483648-2147483647-2147483646 Arithmetic is ___________________ INT_MIN . . . INT_MAX 17

  18. Sec. 3.3 int Representation Integers are often represented internally in the ____________________ format, where the high-order (leftmost) bit indicates the number’s sign: 210 = 00000000000000102 110 = 00000000000000012 010 = 00000000000000002 -110 = 11111111111111112 -210 = 11111111111111102 Here we show 16 bits, but 32 or 64 are common. 18

  19. Twos-Complement To find twos-complement representation of a negative number: Represent its absolute value in binary: (_________________) Invert (complement) the bits: (_________________) Add 1 (_________________) Simpler: Flip all bits ________________________ Trypracticeexercises— Week 3of schedule 19

  20. + 0 1 0 0 1 1 1 10 * 0 1 0 0 0 1 0 1 Why Use Two's Complement? Because usual algorithms for +, * work! • 5 + 7: • 0000000000000101 • +0000000000000111 111¬¾¾ carry bits 0000000000001100 • 5 + –6: • 0000000000000101 • +1111111111111010 1111111111111111 20

  21. See Ch. 3,pp. 58-60 double Objects Real values are often represented in 64 bits using the IEEE floating point standard: 5.75 = = Trypracticeexerciseson course schedule exponent (11 bits) 0 10000000001 01110000000000000000000000000000000000000000000000000 sign (1 bit) mantissa (52 bits) float (single precision): bias = 127; exponent: 8 bits mantissa: 23 bits 21

  22. Problem: Binary representations of most real numbers do not terminate; e.g., 13.1 = 1101.00011001100110011… So they can't be stored exactly; error in this representation is called ____________________ Execution: 0 0 0.1 0.104873 0.2 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 . . . //-- Effect of roundoff error #include <iostream> #include <cmath> using namespace std; int main() { for (double x = 0; x != 50.0; x = x + 0.1) { double y = x * sqrt(1 + sin(x)); cout << x << " " << y << '\n'; //if (x > 60) break; } } Patriot missile failureSee Other Course Information 22

  23. /* temperature.cpp converts a Celsius temperature to Fahrenheit. John Doe Lab 1 Jan. 6, 2011 CPSC 104X Input: A Celsius temperature Output: Corresponding Fahrenheit temperature -----------------------------------------------*/ #include <iostream> // cin, cout, <<, >> using namespace std; int main() { cout << "John Doe CPSC 104X -- Lab 1\n\n"; 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

More Related