Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types Declarations Expressions Data storage C++ Basics.

Similar presentations


Presentation on theme: "Data Types Declarations Expressions Data storage C++ Basics."— Presentation transcript:

1

2 Data Types Declarations Expressions Data storage C++ Basics

3 Data Types Integers – ordinal numbers Integers – ordinal numbers Floating – real numbers Floating – real numbers Address – memory location Address – memory location Structured - aggregates Structured - aggregates

4 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 ($, ¥, ‰) 010100010 Characters can be treated as integers -- a legacy from C cout << ‘B’ – 1 << ‘\n’ 011100010 010000010 011000010 011101010

5 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 *

6 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 42 - 43 in text Use sizeof ( ) to find amount of storage space used. See pages 42 - 43 in text * * * Floating point Floating point numbers:

7 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++

8 Constants Literal typed directly into the program as needed ex. y = 23.4pi = 3.1416 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 = 3.1416; Ex: const int CLASS_SIZE = 87; const double PI = 3.1416; * can change can NOT change

9 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.

10 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;

11 Arithmetic Expressions Syntax operand operator operand Example 7+15 34 - 189 92 *31 345 / 6.02 86 % 3 Example 7+15 34 - 189 92 *31 345 / 6.02 86 % 3 *

12 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. * 4 3 12 12 0 4 3 14 12 2

13 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 *

14 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 35 24 % 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 35 24 % 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 * * *

15 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. *

16 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 2.6667 The result must be an integer. The result is truncated to 2. *

17 Order of Operations 8 + 3 * 4 is ? 8 + 3 * 4 is ? Show associativity to clarify. ( 8 + 3 ) * 4 is 44 8 + ( 3 * 4 ) is 20 Without parentheses, 8 + 3 * 4 is 20 Show associativity to clarify. ( 8 + 3 ) * 4 is 44 8 + ( 3 * 4 ) is 20 Without parentheses, 8 + 3 * 4 is 20 * P E M D A S from left to right

18 Order of Operations Expression Value 10 / 2 * 3 10 % 3 - 4 / 2 5.0 * 2.0 / 4.0 * 2.0 5.0 * 2.0 / (4.0 * 2.0) 5.0 + 2.0 / (4.0 * 2.0) Expression Value 10 / 2 * 3 10 % 3 - 4 / 2 5.0 * 2.0 / 4.0 * 2.0 5.0 * 2.0 / (4.0 * 2.0) 5.0 + 2.0 / (4.0 * 2.0) 15 5.0 1.25 5.25 * * * * *

19 Evaluation Trees i i 10 / 2 * 3 * 10 % 3 - 4 / 2 i i i 5.0 * 2.0 / 4.0 * 2.0 r r r 5 * 2 / (4.0 * 2.0) i r r

20 Evaluation Trees 5.0 * 2.0 / 4.0 * 2.0 5.0 + 2.0 / (4.0 * 2.0) (5 + 2) / (4.0 * 2.0) i r r r r r r r r

21 Those who fail to plan, plan to fail


Download ppt "Data Types Declarations Expressions Data storage C++ Basics."

Similar presentations


Ads by Google