Fundamentals of C++ Programming - 01: CplusplusFundamental - Dustin Nguyen, PhD

Program structure
❖ Data types and operators
❖ Variable declarations
❖ Integers
❖ Problem solving 

Program structure
❖ Data types and operators
❖ Variable declarations
❖ Integers
❖ Problem solving 

pdf 39 trang xuanthi 27/12/2022 1220
Bạn đang xem 20 trang mẫu của tài liệu "Fundamentals of C++ Programming - 01: CplusplusFundamental - 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_dustin.pdf

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

  1. Outcomes ❖ Be able to explain the source code using comments ❖ Writing clear code with indent and rules 2
  2. Today’s outline ❖ Program structure ❖ Data types and operators ❖ Variable declarations ❖ Integers ❖ Problem solving 4
  3. Program structure ❖ Example #include #include using namespace std; int main() { string name = ""; cout int main() { char name[20] = ""; printf(“Login (please input your name): “); gets(name); printf(“Hello %s! Welcome to C world\n.", name); return 0; } 6
  4. Program structure ❖ Global variables definition: these variable are visible to all classes and functions in the program ❖ Scope: global vs. local ❖ Different between C and C++ in variable declaration ❖ Statements: instructions, operations, variable declarations, or a call to other functions. Each statement ends with “;” ❖ Structure vs. Class definition and implementation 8
  5. Program structure ❖ Simple template #include // define data structure // define functions // declare global variables int main(int narg, char* argv[]) { // local variables /* Your code should be put here. * Remember that in C++ you can declare variables where you need it. * However, it is not good to declare variables that way since it * is hard to maintenance, modify, and expand your code in the future. * You should consider the scope of your variables before you declare * them. */ return 0; } 10
  6. Indents, coding style ❖ Use indents to enhance your code ❖ Easy to manage flow of code ❖ Easy to read code ❖ Coding requires skills and the programmer, in most of cases need to follow rules of their community. 12
  7. Data types and operators
  8. Data types and operators ❖ Basic data types ❖ Numeric types: integer, floating-point data ❖ Character/String: character, array of characters, struct/class ❖ Boolean: true/false ❖ Structure/Class: defined by user, fully customized ❖ Enum: names associated with integer values ❖ auto: must be associated with an assignment operation (only in modern C++) 16
  9. Data types and operators ❖ Compound assignments: ❖ +=, -=, *=, /=, %= ❖ >>=, = b is equivalent to a = a b ❖ E.g.: x += 8;// equivalent to x = x + 8 18
  10. Variable declaration
  11. Variable declaration ❖ Rules for variable name (identifiers, in general): ❖ A valid identifier is a sequence of one or more letters, digits, or underscore characters (_). ❖ Spaces, punctuation marks, and symbols cannot be part of an identifier. ❖ Identifiers shall always begin with a letter/_. ❖ Case sensitive ❖ Reserved keywords: ❖ alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, char16_t, char32_t, class, compl, const, constexpr, const_cast, continue, decltype, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, noexcept, not, not_eq, nullptr, operator, or, or_eq, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_assert, static_cast, struct, switch, template, this, thread_local, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while, xor, xor_eq 22
  12. Assignment statement ❖ = ; ❖ return ❖ can’t be constant ❖ Example: ❖ pi = 3.1415; ❖ keyPressed = ‘q’; 24
  13. Integers
  14. Integers ❖ There are 10 basic types of integers: ❖ char, unsigned char, short, unsigned short ❖ int, unsigned int, long, unsigned long, long long, unsigned long long ❖ Types definitions depend on different compilers ❖ Other types are usually new names of basic types ❖ E.g.: in Visual Studio we have ❖ __int8, __int16, __int32, __int64 (and their unsigned definitions) 28
  15. Integers ❖ Overflow, type casting problems #include #include using namespace std; int main() { int i = -30000000000000000; long j = -30000000000000000; cout << “Default integer value cannot hold \”-30000000000000000\”” << endl; cout << “i = ” << i << endl;// print out -1329790976 cout << “j = ” << j << endl;// print out -30000000000000000 return 0; } 30
  16. Integers ❖ Programming tips ❖ Consider if your software needs to be expanded in the future. If it does, we have to decide which variable can change and assign an appropriate size. ❖ Characteristic of your variable ❖ Decide which operation is permitted on your variable ❖ Be careful with logic on integer: use comparison in case you are not sure 32
  17. Example problem ❖ Write a program that solve the quadratic equation (second degree polynomial equation) of the following form: ax2 + bx + c =0 ❖ Prepare: ❖ Define problem, what are criteria and constraints? ❖ Gather information, explore, plan ❖ Act: write the algorithm, implement code ❖ Check, generalize, disseminate 34
  18. Example problem ❖ Gather information: ❖ known: constants a, b, c ❖ unknown: variable x ❖ Explore: existed solution! ❖ Plan: input constants, check condition, compute solution 36
  19. Summarise ❖ Understand basic elements of C/C++ ❖ Be able to read and explain the code with comments ❖ Know how to write clear code 38