Fundamentals of C++ Programming - 01: CplusplusFundamental ( PART 2) - Dustin Nguyen, PhD

Understand basic components of C++
❖ Using assignment operator
❖ How to format the output
❖ How to use libraries
❖ How to input values
❖ How to define macro, constants 
pdf 39 trang xuanthi 27/12/2022 3000
Bạn đang xem 20 trang mẫu của tài liệu "Fundamentals of C++ Programming - 01: CplusplusFundamental ( PART 2) - Dustin Nguyen, PhD", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.

File đính kèm:

  • pdffundamentals_of_c_programming_01_cplusplusfundamental_part_2.pdf

Nội dung text: Fundamentals of C++ Programming - 01: CplusplusFundamental ( PART 2) - Dustin Nguyen, PhD

  1. Outcomes ❖ Understand basic components of C++ ❖ Using assignment operator ❖ How to format the output ❖ How to use libraries ❖ How to input values ❖ How to define macro, constants 2
  2. Assignment operation
  3. Assignment operation ❖ Assign at the declaration instruction: ❖ int x = 10; ❖ int y{8}; ❖ float z(10.01f); ❖ AnimalC monkey(10.5, 30);// use Class construction ❖ float 8.0 = f; 6
  4. Assignment operation ❖ Default type of constants depend on how you declare it ❖ 10: decimal value, default type depends on context ❖ 012: octal value ❖ 0x64: hexadecimal value ❖ 3.1415: default type is double 8
  5. Assignment operation ❖ NOTE: compiler does not warn you when you assign a value with different type to variable (default setting) ❖ int a = 1024UL, c = 3.1415; ❖ unsigned int b = -4096; ❖ float x = 6.2830L; ❖ Compiler warns you when overflow problem occurs in the assignment op. ❖ Default cast operation will be applied without warning 10
  6. Auto type ❖ auto type appears from C++11 standard. ❖ Should we use auto? ❖ Where can we use auto? ❖ auto type: good or bad? Know what you are doing Make you code clear Debug 12
  7. Output format ❖ Text output format ❖ printf(“i = %d\n”, i); ❖ cout << “i = ” << i << endl; ❖ Using function is a convenient way to format output. ❖ Using I/O streams require a bit modification in the sequence. 14
  8. Output format specifier output example d/i signed decimal integer -2354 u unsigned decimal integer 3056 o unsigned octal 342 x/X unsigned hexadecimal integer 6f0c f/F decimal floating point 3.14159 e/E scientific notation 3.14159e-05 g/G use shortest representation 3.14159 a/A hexadecimal floating point -0xc.90dep-3 c character a s string damn it p pointer address b8000000 nothing will be printed, argument must be a pointer to a signed int. The number n of printed characters are stored location pointed by the pointer. % print ‘%’ character % 16
  9. Output format ❖ Using cout: require “iomanip” for formatting ❖ cout.width( ): set width of the output result, both text and number ❖ cout ) ❖ cout.precision( ): set maximum number of significant digits (set to 0 to reset this setting) ❖ cout ) ❖ cout << “*” << setw(4) << 8 << “*” << endl; ❖ cout << “*” << setprecision(4) << 12356.4 << “*” << endl; 18
  10. Output format ❖ Fixed format for floating point number ❖ cout.setf(ios::fixed, ios::floatfield); ❖ cout << fixed; ❖ Reset to default: cout.setf(0, ios::floatfield); ❖ E.g.: ❖ cout.setf(ios::fixed, ios::floatfield); cout.precision(2); cout << 3.14159 << “, ” << 0.8642e-3; 20
  11. Library functions ❖ Library is the place where you implement functions, classes to serve some specific tasks. ❖ Library contains: ❖ Definitions: constants, macro, structure, class ❖ Functions: implement specific algorithms, a unit of reusable code ❖ Class implementations 22
  12. Library functions ❖ Standard libraries: ❖ , ❖ , ❖ , ❖ , ❖ , ❖ , 24
  13. Input with cin
  14. Input with cin ❖ The operator is overloaded for various types ❖ Arithmetic types: int, float, unsigned, etc. ❖ Stream buffers ❖ Extract as many characters as possible from the stream and put them to the stream buffer ❖ Manipulators: ios/ios_base, are used to format the input stream ❖ ws, boolalpha/noboolalpha, skipws/noskipws, dec/hex/oct 28
  15. Input with cin ❖ String input: ❖ Spaces will be considered as stop condition ❖ Behaviour of continuously input operators are unpredictable with abnormal input from user ❖ istream::get(): equivalent to getc() in standard C ❖ istream::getline(): equivalent to gets() in standard C 30
  16. Macro definition
  17. Macro definition ❖ Define constants: #define ❖ #define MAX_LENGTH 50 ❖ #define MY_STRING “This is a constant string” ❖ #define pi_2 3.14159/2 ❖ #define pi_2 1.570785 ❖ Constants variables: ❖ const float x_2 = x / 2;// x_2 cannot be changed 34
  18. Macro definition ❖ More examples: ❖ #define NORMALIZE_FACTOR 50 float fx = sum / NORMALIZE_FACTOR; ❖ #define sub(a, b) float x = 0.5 * sub(z + 3.9, y + f(t)); 36
  19. Summarise ❖ Understand basic elements of C/C++ ❖ Assignment operator, default types, type casting, overflow problem ❖ Format the output ❖ Use library functions ❖ Input values ❖ Macro, constants 38