Download presentation
Presentation is loading. Please wait.
Published byAusten Clarke Modified over 8 years ago
1
Objects Variables and Constants
2
Our Scuba Problem #include // 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 (... )Literals: unnamed objects having a value ( 0, -3, 2.5, 2.998e8, 'A', "Hello\n",... ) Variables: named objects whose values can change during program executionVariables: named objects whose values can change during program execution Constants: named objects whose values do not change during program executionConstants: named objects whose values do not change during program execution
5
Literals – literals are whole numbers: –int literals are whole numbers: -27, 0, 4, +4 – literals are real numbers, and can be: –double literals are real numbers, and can be: fixed-point:,...fixed-point: -0.333, 0.5, 1.414,... floating-point:,...floating-point: 2.998e8, 0.2998e9,... –There are just two literals: –There are just two bool literals: false, true – literals are single ASCII characters:... –char literals are single ASCII characters: 'A', 'a', '9', '$', '?',... – literals are ASCII character sequences:,... –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: 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: 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: 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:,... Valid: age, r2d2, myGPA, MAX_SCORE,... Invalid:,... 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., )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., )Variable names are all lowercase, with the first letter of each word after the first capitalized (e.g., lastName )
11
Objects char Objects... are represented in memory by a code –ASCII code uses 8 bits to represent a character, allowing for 2 8 = 256 different characters. –Unicode uses 16 bits to represent a character, allowing for 2 16 = 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
Objects 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
Representation int Representation Integers are often represented in the twos-complement format, where the high- order bit indicates the number’s sign: 2 10 = 0000000000000010 2 1 10 = 0000000000000001 2 0 10 = 0000000000000000 2 -1 10 = 1111111111111111 2 -2 10 = 1111111111111110 2 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)Select your number (e.g., -12) Represent its absolute value in binary: (0000000000001100)Represent its absolute value in binary: (0000000000001100) Invert the bits (1111111111110011)Invert the bits (1111111111110011) Add 1 (1111111111110100)Add 1 (1111111111110100)
16
Objects unsigned Objects For objects whose values are never negative, C++ provides the type: For objects whose values are never negative, C++ provides the unsigned type: 0000000000000000 2 = 0 10 0000000000000001 2 = 1 10 0000000000000010 2 = 2 10... 1111111111111110 2 = 65534 10 1111111111111111 2 = 65535 10 With no sign bit, numbers can be twice as big.
17
vs. int vs. unsigned Using 32 bits, values range from -2 31 (-2147483648) to 2 31 -1 (2147483647), whereas values range from 0 to 2 32 -1 (4294967295). Using 32 bits, int values range from -2 31 (-2147483648) to 2 31 -1 (2147483647), whereas unsigned values range from 0 to 2 32 -1 (4294967295). An value “loses” one of its bits to the sign, and so the maximum value is about half of the maximum value. 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
Objects double Objects Real values are often represented in 64 bits using the IEEE floating point standard: sign (1 bit) mantissa (52 bits) exponent (11 bits)
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,,,,,,,, and. The C++ fundamental types include bool, char, int, short, long, unsigned, float, double, and long double.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.