1 / 19

Objects

Objects. Variables and Constants. Our Scuba Problem. #include &lt;iostream&gt; // cin, cout, &lt;&lt;, &gt;&gt; using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM = 14.7; cout &lt;&lt; &quot;<br>Scuba pressure calculator!!&quot;

nibaw
Télécharger la présentation

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 Variables and Constants

  2. Our Scuba Problem #include <iostream> // cin, cout, <<, >> using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM = 14.7; cout << "\nScuba pressure calculator!!" << "\n Enter the depth (feet): "; double depth; cin >> depth; double pressure = ((depth / FEET_PER_ATM) + 1) * LBS_PER_SQ_IN_PER_ATM; cout << "\nThe pressure at " << depth << " feet is " << pressure << "lbs/sq.in." << endl; }

  3. Expressions In a C++ program, any sequence of objects and operations that combine to produce a value is called an expression. Here is an example from our scuba problem: double pressure = ((depth / FEET_PER_ATM) + 1) * LBS_PER_SQ_IN_PER_ATM; Today, we’re going to focus on C++ objects...

  4. Object Categories There are three kinds of objects: • Literals: unnamed objects having a value (0, -3, 2.5, 2.998e8, 'A', "Hello\n", ...) • Variables: named objects whose values can change during program execution • Constants: named objects whose values do not change during program execution

  5. Literals • int literals are whole numbers: -27, 0, 4, +4 • double 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 bool literals: false, true • char literals are single ASCII characters: 'A', 'a', '9', '$', '?', ... • string literals are ASCII character sequences: "Hello", "Goodbye", "Goodbye\n", ...

  6. Variable Declarations Variables are used to store values, and can be either initialized or uninitialized... Examples: int age = 18; double GPA = 3.25, credits; char letterGrade = 'A'; bool ok, done = false; Pattern: Type Name [ = Expression ] ;

  7. Assignment Statements The value of a variable can be changed using an assignment statement... Examples: age = 19; credits = hours * 3.0; letterGrade = 'B'; done = true; Pattern: Name = Expression;

  8. Constant Declarations Constants are used to represent a value with a meaningful name, and must be initialized. Examples: const int MAX_SCORE = 100; const double PI = 3.14159; const char MIDDLE_INITIAL = 'A'; const string PROMPT = "Enter a number: "; Pattern: const Type Name = Expression;

  9. Identifiers Technically, the name of an object is called an identifier (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,...

  10. Conventions To keep variable and constant objects distinct: • Constant names are all uppercase, with multiple words separated by underscores (e.g., MAX_SCORE) • Variable names are all lowercase, with the first letter of each word after the first capitalized (e.g., lastName)

  11. char Objects ... are represented in memory by a code • ASCII code uses 8 bits to represent a character, allowing for 28 = 256 different characters. • Unicode uses 16 bits to represent a character, allowing for 216 = 65,536 different characters. ASCII is the most commonly used code: '0' = 48 = 00110000 'A' = 65 = 01000001 'a' = 97 = 01100001

  12. Escape Characters C++ provides a number of escape 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 '\hhh' char with hex code hhh

  13. int Objects Three forms: • decimal (base-10): begin with a non-zero or sign (-45, -2, 0, +21, 36, 65536, ...) • octal (base-8): a zero followed by digits (01, 02, 03, 04, 05, 06, 07, 010, 011, 012, ...) • hexadecimal (base-16): zero-x followed by digits with a, b, c, d, e, f = 10, 11, 12, 13, 14, 15 (0x1, 0x2, ..., 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, ...)

  14. int Representation Integers are often represented in the twos-complement format, where the high-order bit indicates the number’s sign: 210 = 00000000000000102 110 = 00000000000000012 010 = 00000000000000002 -110 = 11111111111111112 -210 = 11111111111111102 We show 16 bits, but 32 or 64 are common.

  15. Twos-Complement To find twos-complement representation of a negative number: • Select your number (e.g., -12) • Represent its absolute value in binary: (0000000000001100) • Invert the bits (1111111111110011) • Add 1 (1111111111110100)

  16. unsigned Objects For objects whose values are never negative, C++ provides the unsigned type: 00000000000000002 = 010 00000000000000012 = 110 00000000000000102 = 210 ... 11111111111111102 = 6553410 11111111111111112 = 6553510 With no sign bit, numbers can be twice as big.

  17. int vs. unsigned 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.

  18. exponent (11 bits) sign (1 bit) mantissa (52 bits) double Objects Real values are often represented in 64 bits using the IEEE floating point standard:

  19. Summary C++ provides three kinds of objects: literals, variables and constants. Literals have a “built-in” type; a declaration statement is the means by which a type is associated with a variable or constant. The C++ fundamental types include bool, char, int, short, long, unsigned, float, double, and long double.

More Related