Data Types Declarations Expressions Data storage C++ Basics
Data Types Integers – ordinal numbers Integers – ordinal numbers Floating – real numbers Floating – real numbers Address – memory location Address – memory location Structured - aggregates Structured - aggregates
Integer Data Types char 256 possibilities; enclosed in single quotes char 256 possibilities; enclosed in single quotes int no decimal points no commas no special signs ($, ¥, ‰) int no decimal points no commas no special signs ($, ¥, ‰) Characters can be treated as integers -- a legacy from C cout << ‘B’ – 1 << ‘\n’
Int Data Types Int -- Signed vs. Unsigned short 65,536 numbers -32,768 to 32,767 short 65,536 numbers -32,768 to 32,767 long 4,294,967,296 numbers 2,147,483,648 long 4,294,967,296 numbers 2,147,483,648 enum – a collection of symbolic related constants enum – a collection of symbolic related constants *
Real Number Data Types Floating point numbers: signed or unsigned with decimal point Floating point numbers: signed or unsigned with decimal point Types: differ in amount of storage space; varies with machine Types: differ in amount of storage space; varies with machine float (single precision) double long double float (single precision) double long double Use sizeof ( ) to find amount of storage space used. See pages in text Use sizeof ( ) to find amount of storage space used. See pages in text * * * Floating point Floating point numbers:
Data Types Address pointer value is an actual address reference - an alias for another variable CONFUSING, don’t use (yet) Structured array - an ordered list of like items Address pointer value is an actual address reference - an alias for another variable CONFUSING, don’t use (yet) Structured array - an ordered list of like items strings are a type of an array struct – unordered heterogeneous set class – the basis of OO in C++ strings are a type of an array struct – unordered heterogeneous set class – the basis of OO in C++
Constants Literal typed directly into the program as needed ex. y = 23.4pi = Symbolic (Named) similar to a variable, but cannot be changed after it is initialized Syntax: const type VAR = value Symbolic (Named) similar to a variable, but cannot be changed after it is initialized Syntax: const type VAR = value Ex: const int CLASS_SIZE = 87; const double PI = ; Ex: const int CLASS_SIZE = 87; const double PI = ; * can change can NOT change
Operators An operator is a symbol that causes the compiler to take an action. An operator is a symbol that causes the compiler to take an action. * Categories: mathematical assignment boolean / logical etc.
Arithmetic Operators addition+ addition+ subtraction - subtraction - multiplication * multiplication * division / division / modulus( remainder ) % modulus( remainder ) % Unary (minus) operator Unary (minus) operator x = -3; x = -3; y = -x; y = -x;
Arithmetic Expressions Syntax operand operator operand Example * / % 3 Example * / % 3 *
Modulus 12/3 14/3 12%3 14%3 The modulus operator yields the remainder of integer division. The modulus operator yields the remainder of integer division. *
A Glimpse of Operator Overloading Operator overload Using the same symbol for more than one operation. Operator overload Using the same symbol for more than one operation. type int / type int 9 / 5 operator performs int division type double / type double 9.0 / 5.0 operator performs double division *
Modulus The modulus operator yields the remainder of integer division. The modulus operator yields the remainder of integer division. 18 % 4 is 2 13 % 4 is 1 17 % 3 is 2 35 % 47 is % 6 is 0 24 % 4 is 0 4 % 18 is 4 0 % 7 is 0 18 % 4 is 2 13 % 4 is 1 17 % 3 is 2 35 % 47 is % 6 is 0 24 % 4 is 0 4 % 18 is 4 0 % 7 is 0 12 % 2.5 error 6.0 % 6 error 12 % 2.5 error 6.0 % 6 error Requires floating point division, NOT the same Requires floating point division, NOT the same * * *
Mixed-Mode Expressions Operator overload Same operator will behave differently depending upon the operands. Operands of the same type give results of that type. Operator overload Same operator will behave differently depending upon the operands. Operands of the same type give results of that type. In mixed-mode, floating point takes precedence. In mixed-mode, floating point takes precedence. *
Integer Division int a, b; a = 8; b = 3; cout << “The result is “ << a / b << endl; The result is 2 int a, b; a = 8; b = 3; cout << “The result is “ << a / b << endl; The result is 2 8 / 3 is 2 and not The result must be an integer. The result is truncated to 2. *
Order of Operations * 4 is ? * 4 is ? Show associativity to clarify. ( ) * 4 is ( 3 * 4 ) is 20 Without parentheses, * 4 is 20 Show associativity to clarify. ( ) * 4 is ( 3 * 4 ) is 20 Without parentheses, * 4 is 20 * P E M D A S from left to right
Order of Operations Expression Value 10 / 2 * 3 10 % / * 2.0 / 4.0 * * 2.0 / (4.0 * 2.0) / (4.0 * 2.0) Expression Value 10 / 2 * 3 10 % / * 2.0 / 4.0 * * 2.0 / (4.0 * 2.0) / (4.0 * 2.0) * * * * *
Evaluation Trees i i 10 / 2 * 3 * 10 % / 2 i i i 5.0 * 2.0 / 4.0 * 2.0 r r r 5 * 2 / (4.0 * 2.0) i r r
Evaluation Trees 5.0 * 2.0 / 4.0 * / (4.0 * 2.0) (5 + 2) / (4.0 * 2.0) i r r r r r r r r
Those who fail to plan, plan to fail