KEMBAR78
Intro to C++ Basic | PPTX
Intro to C++
Outline
 1. C basic review
 2. Basic I/O
 3. Pointer
 4. Array
 5. File processing
 6. Reference
C basic review
 A basic C program:
C basic review (cont.)
 preprocessor directives:
◦ #include<stdio.h>
◦ #define “student.h”
◦ #ifdef, #ifndef, #endif
 Variables(types) – case sensitive:
◦ int a = 0, _aaaaaa = 1; ok , ok
◦ float 123abc = 1; wrong
◦ double int = 2, douBle = 4.8; wrong ,
ok
C basic review (cont.)
 Functions:
◦ Function declaration vs. function definition
◦ int destroy(int, int);
◦ int destroy(int a, int b) { return a – b;}
◦ Function call: destroy(5, 6);
 Statements:
◦ printf(“5566n”);
◦ return (a-b);
◦ char *arm = “arm”;
◦ 一個statement 最後一定會有 ;
Basic I/O
 C output:
◦ printf();
◦ %d, %i, %f, %lld
◦ %e: 以科學記號輸出浮點數
◦ %p: 輸出pointer
◦ %x: 輸出十六進位的浮點數
Basic I/O (cont.)
 C input:
◦ Common usage: scanf()
◦ ex: scanf(“%d”, &input);
◦ reads data from stdin and stores them to
the parameter format into the locations
pointed by the additional arguments. -
(cplusplus.com)
 Formatted string
p.s. stream ?
 What is stream?
 A: A stream is an entity where a
program can either insert or extract
characters to/from.
 source/destination of characters, and
that these characters are
provided/accepted sequentially –
(cplusplus.com)
Basic I/O (cont.)
 C++ output:
◦ example program
◦ iostream 函式庫 – 類似C的stdio.h header
◦ std::out:接近C的printf function
 標準輸出串流物件(standard output stream
object)
 cout : 屬於std(standard)這個命名空間
(namespace)
 <<:stream insertion operator,資料流動的方
向
◦ std::endl:很像C的 n (end line character)
  上面的那個是小寫的L
Practice 1
 試著用std::cout 輸出一個簡單的自我介
紹
 ps. 每一項記得換行~
 Ex: 名字、學號、系級……
Basic I/O (cont.)
 C++ read input:
◦ std:cin:類似scanf
◦ 輸入串流物件(input stream object)
◦ >>: input stream extraction operator
◦ 不需要%d, %f…等等,不用&
◦ 不用加std:endl在句尾
◦ Ex:
Basic I/O (cont.)
 namespace?
◦ A: A namespace is a declarative region
that provides a scope to the identifiers
inside it.
◦ 怎麼使用呢? 這樣用:using
namespace OO;
◦ ex: using namespace std;
Practice 2
 試寫一個C++程式
◦ 讀取兩個數字(ex: a & b),對這兩個數字做
整數的四則運算,再輸出各個值
◦ ps. 要注意四則運算的規則
◦ Sample input:
 8 7
◦ Sample output:
 a + b = 15
 a - b = 1
 a * b = 56
 a / b = 1.14286
Pointer
 pointer: 能夠儲存位址  pointer variable
 &:取址運算子(address-of operator)  取
得某個變數的記憶體位置
 *:dereference operator  取得變數指向的
object的值
 宣告一個pointer: int *p = &a; (假設int a = 4)
◦ 意義:宣告一個變數p,p儲存了一個integer a的
位置(p 指向 a這個integer object)
 實際使用時:
◦ Function declaration: void func( int *p );
◦ Function call: func( &a );
Pointer (cont.)
 舉例:
Array
 A sequence of variables
 Array 在記憶體中是連續的
 當一個array被傳進一個function,其實是看
成pointer(指向array的第一個element)
 void func(int a[] , int b); = void func(int *a …);
 func( x );  array x 被當作指向第一個
element的pointer
 但其實array和pointer變數是不一樣的
Array (cont.)
 Multi-dimensional array:
◦ int arr[row][col] ;
◦ 而arr的type:int (*)[col]
◦ ex: void func( int arr[][3] );
 傳入array的名字  可以這樣寫
(*arr)[3]
◦ ex: func( arr );
Array (cont.)
 Vectors: (std::vector)
◦ template class
◦ 可以理解成dynamic array  可縮可放
◦ 自我管理記憶體,儲存的記憶體在destruct的
時候會被free
◦ 要include <vector>
◦ 也順便using namespace std;
◦ 但是記得不要用#define (不需要用macro)
◦ 用法: vector<type> variable_name
(elements);
◦ example: vector<int> vec (4);
Array (cont.)
 Vectors (cont.)
◦ 常用 function:
◦ 要印出vector的element:
 iterator, .size(), range-based for loop(C++11)
Practice *
 1. 試著用vector去儲存費布納西數列的
前20個數字,再把vector的element一
個一個印出來
 2. 寫一個程式,使用者能夠自己輸入
長度不一的5個vector element,然後
倒著把那些element印出來
◦ 小提示:可以用 back() & pop_back()
File processing
 處理檔案的讀寫:
 檔名  開檔  讀/寫  關檔
 要#include<fstream>
ios::app 從檔案結尾寫入(輸出)資料
ios::in 檔案開啟為讀取狀態
ios::out 檔案開啟為寫入狀態
ios::ate 從檔案結尾讀取和輸入資料
ios::trunc 如果檔案存在,開啟時清除資料
ios::binar
y
以二進位模式開檔(預設文字模式)
File processing (cont.)
 輸出到檔案:
◦ 用ofstream輸出,和cout一樣用法<<
◦ 可以用contructor直接開檔輸出,或者用
open() 來做
◦ ex: ofstream test1(“test1.txt”, ios::out);
or: ofstream test1; test1().open(“test1.txt”,
ios::out);
◦ 記得用close()來關掉檔案
File processing (cont.)
File processing (cont.)
 從檔案輸入:
◦ 用ifstream輸出,和cin一樣用>>
◦ 可以用contructor直接開檔輸出,或者用
open() 來做
◦ ex: ifstream test1(“test1.txt”, ios::in);
or: ifstream test1; test1().open(“test1.txt”,
ios::in);
◦ 記得用close()來關掉檔案
File processing (cont.)
 輸入檔案
Practices
 1. 寫一個程式能夠不斷輸入不同學校
到檔案內,然後取名檔案叫
schools.txt(要能判斷輸入要做結束)
 2. 創造一個name.txt並至少打3行不同
名字;程式中一行一行讀取檔案內容直
到讀完為止
Q & A
Reference
 http://www.cprogramming.com/reference/preprocessor/ifndef.
html
 http://www.cprogramming.com/declare_vs_define.html
 http://www.cplusplus.com/reference/cstdio/printf/
 http://www.cplusplus.com/reference/cstdio/scanf/
 http://www.cplusplus.com/reference/ostream/endl/
 https://msdn.microsoft.com/en-us/library/5cb46ksf.aspx
 http://www.cplusplus.com/doc/tutorial/pointers/
 http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c40
27/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

Intro to C++ Basic

  • 1.
  • 2.
    Outline  1. Cbasic review  2. Basic I/O  3. Pointer  4. Array  5. File processing  6. Reference
  • 3.
    C basic review A basic C program:
  • 4.
    C basic review(cont.)  preprocessor directives: ◦ #include<stdio.h> ◦ #define “student.h” ◦ #ifdef, #ifndef, #endif  Variables(types) – case sensitive: ◦ int a = 0, _aaaaaa = 1; ok , ok ◦ float 123abc = 1; wrong ◦ double int = 2, douBle = 4.8; wrong , ok
  • 5.
    C basic review(cont.)  Functions: ◦ Function declaration vs. function definition ◦ int destroy(int, int); ◦ int destroy(int a, int b) { return a – b;} ◦ Function call: destroy(5, 6);  Statements: ◦ printf(“5566n”); ◦ return (a-b); ◦ char *arm = “arm”; ◦ 一個statement 最後一定會有 ;
  • 6.
    Basic I/O  Coutput: ◦ printf(); ◦ %d, %i, %f, %lld ◦ %e: 以科學記號輸出浮點數 ◦ %p: 輸出pointer ◦ %x: 輸出十六進位的浮點數
  • 7.
    Basic I/O (cont.) C input: ◦ Common usage: scanf() ◦ ex: scanf(“%d”, &input); ◦ reads data from stdin and stores them to the parameter format into the locations pointed by the additional arguments. - (cplusplus.com)  Formatted string
  • 8.
    p.s. stream ? What is stream?  A: A stream is an entity where a program can either insert or extract characters to/from.  source/destination of characters, and that these characters are provided/accepted sequentially – (cplusplus.com)
  • 9.
    Basic I/O (cont.) C++ output: ◦ example program ◦ iostream 函式庫 – 類似C的stdio.h header ◦ std::out:接近C的printf function  標準輸出串流物件(standard output stream object)  cout : 屬於std(standard)這個命名空間 (namespace)  <<:stream insertion operator,資料流動的方 向 ◦ std::endl:很像C的 n (end line character)   上面的那個是小寫的L
  • 10.
    Practice 1  試著用std::cout輸出一個簡單的自我介 紹  ps. 每一項記得換行~  Ex: 名字、學號、系級……
  • 11.
    Basic I/O (cont.) C++ read input: ◦ std:cin:類似scanf ◦ 輸入串流物件(input stream object) ◦ >>: input stream extraction operator ◦ 不需要%d, %f…等等,不用& ◦ 不用加std:endl在句尾 ◦ Ex:
  • 12.
    Basic I/O (cont.) namespace? ◦ A: A namespace is a declarative region that provides a scope to the identifiers inside it. ◦ 怎麼使用呢? 這樣用:using namespace OO; ◦ ex: using namespace std;
  • 13.
    Practice 2  試寫一個C++程式 ◦讀取兩個數字(ex: a & b),對這兩個數字做 整數的四則運算,再輸出各個值 ◦ ps. 要注意四則運算的規則 ◦ Sample input:  8 7 ◦ Sample output:  a + b = 15  a - b = 1  a * b = 56  a / b = 1.14286
  • 14.
    Pointer  pointer: 能夠儲存位址 pointer variable  &:取址運算子(address-of operator)  取 得某個變數的記憶體位置  *:dereference operator  取得變數指向的 object的值  宣告一個pointer: int *p = &a; (假設int a = 4) ◦ 意義:宣告一個變數p,p儲存了一個integer a的 位置(p 指向 a這個integer object)  實際使用時: ◦ Function declaration: void func( int *p ); ◦ Function call: func( &a );
  • 15.
  • 16.
    Array  A sequenceof variables  Array 在記憶體中是連續的  當一個array被傳進一個function,其實是看 成pointer(指向array的第一個element)  void func(int a[] , int b); = void func(int *a …);  func( x );  array x 被當作指向第一個 element的pointer  但其實array和pointer變數是不一樣的
  • 17.
    Array (cont.)  Multi-dimensionalarray: ◦ int arr[row][col] ; ◦ 而arr的type:int (*)[col] ◦ ex: void func( int arr[][3] );  傳入array的名字  可以這樣寫 (*arr)[3] ◦ ex: func( arr );
  • 18.
    Array (cont.)  Vectors:(std::vector) ◦ template class ◦ 可以理解成dynamic array  可縮可放 ◦ 自我管理記憶體,儲存的記憶體在destruct的 時候會被free ◦ 要include <vector> ◦ 也順便using namespace std; ◦ 但是記得不要用#define (不需要用macro) ◦ 用法: vector<type> variable_name (elements); ◦ example: vector<int> vec (4);
  • 19.
    Array (cont.)  Vectors(cont.) ◦ 常用 function: ◦ 要印出vector的element:  iterator, .size(), range-based for loop(C++11)
  • 20.
    Practice *  1.試著用vector去儲存費布納西數列的 前20個數字,再把vector的element一 個一個印出來  2. 寫一個程式,使用者能夠自己輸入 長度不一的5個vector element,然後 倒著把那些element印出來 ◦ 小提示:可以用 back() & pop_back()
  • 22.
    File processing  處理檔案的讀寫: 檔名  開檔  讀/寫  關檔  要#include<fstream> ios::app 從檔案結尾寫入(輸出)資料 ios::in 檔案開啟為讀取狀態 ios::out 檔案開啟為寫入狀態 ios::ate 從檔案結尾讀取和輸入資料 ios::trunc 如果檔案存在,開啟時清除資料 ios::binar y 以二進位模式開檔(預設文字模式)
  • 23.
    File processing (cont.) 輸出到檔案: ◦ 用ofstream輸出,和cout一樣用法<< ◦ 可以用contructor直接開檔輸出,或者用 open() 來做 ◦ ex: ofstream test1(“test1.txt”, ios::out); or: ofstream test1; test1().open(“test1.txt”, ios::out); ◦ 記得用close()來關掉檔案
  • 24.
  • 25.
    File processing (cont.) 從檔案輸入: ◦ 用ifstream輸出,和cin一樣用>> ◦ 可以用contructor直接開檔輸出,或者用 open() 來做 ◦ ex: ifstream test1(“test1.txt”, ios::in); or: ifstream test1; test1().open(“test1.txt”, ios::in); ◦ 記得用close()來關掉檔案
  • 26.
  • 27.
    Practices  1. 寫一個程式能夠不斷輸入不同學校 到檔案內,然後取名檔案叫 schools.txt(要能判斷輸入要做結束) 2. 創造一個name.txt並至少打3行不同 名字;程式中一行一行讀取檔案內容直 到讀完為止
  • 28.
  • 29.
    Reference  http://www.cprogramming.com/reference/preprocessor/ifndef. html  http://www.cprogramming.com/declare_vs_define.html http://www.cplusplus.com/reference/cstdio/printf/  http://www.cplusplus.com/reference/cstdio/scanf/  http://www.cplusplus.com/reference/ostream/endl/  https://msdn.microsoft.com/en-us/library/5cb46ksf.aspx  http://www.cplusplus.com/doc/tutorial/pointers/  http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c40 27/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm