Chapter 3 The New Math C Data Types

  • Slides: 16
Download presentation
Chapter 3 The New Math

Chapter 3 The New Math

C++ Data Types simple integral address floating structured pointer array reference struct char short

C++ Data Types simple integral address floating structured pointer array reference struct char short float int long double union Long double class bool enum

Numeric Data Types o char n 1 byte o short n 2 bytes o

Numeric Data Types o char n 1 byte o short n 2 bytes o int n 4 bytes o long n 4 bytes

More Numeric Data Types o float n 4 bytes o double n 8 bytes

More Numeric Data Types o float n 4 bytes o double n 8 bytes o long double n 8 bytes

Declarations o Constant Examples n n n const float const int PI = 3.

Declarations o Constant Examples n n n const float const int PI = 3. 14159; E = 2. 71828; MAX_SCORE = 100; o Variable Examples n n n int char float student. Count; grade; average. Grade;

Simple Arithmetic Expressions o Expressions are made up of constants, variables and operators o

Simple Arithmetic Expressions o Expressions are made up of constants, variables and operators o Examples n n n alpha + 2 rate – 6. 0 4 – alpha rate alpha * num

Arithmetic Operators o - Unary Minus o + Unary Plus o - Subtraction o

Arithmetic Operators o - Unary Minus o + Unary Plus o - Subtraction o + Addition o * Multiplication o / Division o % Modulus

o Expression o Value 1. 3 + 6 1. 9 2. 3. 4 –

o Expression o Value 1. 3 + 6 1. 9 2. 3. 4 – 6. 1 2. -2. 7 3. 2 * 3 3. 6 4. 8 / 2 4. 4 5. 8. 0 / 2. 0 5. 4. 0 6. 8 / 8 6. 1 7. 8 / 9 7. 0 8. 8 / 7 8. 1 9. 8 % 8 9. 0 10. 8 % 9 10. 8 11. 8 % 7 11. 1 12. 0 % 7 12. 0 13. 5 % 2. 3 13. error

Oh yeah, don’t forget o Increment n ++ o Decrement n --

Oh yeah, don’t forget o Increment n ++ o Decrement n --

Let’s Get Tricky o Precedence Rules n n Highest: ++ -- Unary + Unary

Let’s Get Tricky o Precedence Rules n n Highest: ++ -- Unary + Unary – Middle: * / % Lowest: + See Appendix B for complete list

Type Coercion and Type Casting o Type Coercion n The implicit (automatic) conversion of

Type Coercion and Type Casting o Type Coercion n The implicit (automatic) conversion of a value from one data type to another o Type Casting n The explicit conversion of a value from one data type to another

Examples o float some. Float = 3. 4 / 2; o int some. Int

Examples o float some. Float = 3. 4 / 2; o int some. Int = 3. 4 / 2; o some. Float = 3 / 4; o some. Int = 3. 4 / 2. 2;

Casting o some. Float = double (3) / double (4); o some. Float =

Casting o some. Float = double (3) / double (4); o some. Float = <static_cast>(double) 3 / <static_cast> (double) 4;

Function Calls o Example n int some. Int = Cube(27); o Function Call (Function

Function Calls o Example n int some. Int = Cube(27); o Function Call (Function Invocation) n The mechanism that transfer control to the function o Argument List (Parameter List) n A mechanism by which functions communicate with each other

Formatting Output o setw() n Tells how many characters the next item outputted should

Formatting Output o setw() n Tells how many characters the next item outputted should have o fixed n Force all subsequent floating-point output to appear in decimal notation o showpoint n Force decimal points in all subsequent floatingpoint output; even whole numbers o setprecision() n Tells how many decimal places all subsequent floating-point output should have

String Functions o length() or size() o find() o substr()

String Functions o length() or size() o find() o substr()