C.A.Lee
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    物件導向程式設計----王昱舜 ===== `NCTU` `CS` `course` [回主頁](https://hackmd.io/s/ByOm-sFue) # Course ## ch15 串流輸入/輸出 Stream Input/Output #### user-defined stream manipulator - function名稱其實就是一個指標,指向這個function本體在記憶體中的位置 - 因為c++內建重載了<<運算子,所以我們可以自己定義自己的manipulator * ostream& operator<< (ostream& (*pf)(ostream&)); * ostream& operator<< (ios& (*pf)(ios&)); * ostream& operator<< (ios_base& (*pf)(ios_base&)); - 如果要讓manipulator可以接收數值,必須寫成額外的class形式,然後把實作寫成建構子 #### stream manipulator flags - stream的設定是用flag的形式儲存的 - 一個integer可以儲存很多個flag,每個bit都可以當成一個flag - flag的格式是ios_base::fmtflags - 使用cout.flags()可以用來取得和設定flags #### stream error states - `cin.rdstate()` 包含以下各種 states - `cin.eof()` 碰到eof就會是 1 - `cin.fail()` 格式錯誤就是 1 - `cin.bad()` - `cin.good()` 都沒問題就是 1 - `cin.clear()` 用來清除 states,回復為 good 狀態 - 要得到各種 state 的狀態也可以把`cin.rdstate()`和`ifstream::failbit`做&運算來得到 ## ch16 例外處理 Exception handling - 例外處理其實也可以寫進正常的程式邏輯裡,但會降低可讀性及效能,盡量避免 - 要`include <stdexcept>`,它裡面會自動再`include <exception>` - 每個exception裡面都含有error-message,使用what()來得到它 - 三個關鍵字:try catch throw - 一個try可以對應到多個catch,分別處理不同的exception狀況 - 如果丟出來的exception沒有對應的catch來處理它的話,程式就會終止 - 通常在寫catch會用reference來傳遞exceptions,效率較高 - 通常會把derived class的exception catch放前面,把base class的exception catch放較後面,先接特定的exception,都接不- 到再用比較general的base class來接 - synchronous error(同步)發生在CPU和記憶體等部分 - asynchronous error(非同步)發生在網路、硬碟、鍵盤等等IO部分 - Exception handling是設計來處理synchronous error的 - Exception handling也可以用來和各類Software elements做互動,包含constructors, destructors等,因為它們沒有回傳值,所以出問題就只能丟出exception - 大家都會發生的error,使用內建的exception handling來處理就好,如果是只有自己的程式碼才會發生的error,使用exception handling或if-else來處理可讀性都差不多 - catch裡面也可以throw,再把exception丟出去 - re-throw的話後面就不會再執行了 - catch(...)能接到所有exception - exception specification: * int myFunction() throw (exceptionA, exceptionB, exceptionC) { // body } * 指定這個function可以丟出來的exception種類 * 如果它丟出了其他exception,則unexpected會被觸發,程式會關掉 * 如果要它不要丟出任何exception,可以寫 throw() * 通常不建議使用exception specification,好好檢查code才是王道 * 可以使用set_unexpected來處理unexpected發生後要怎麼處理 - stack-unwinding: * 接到exception再往外丟,就叫stack-unwinding - bad_alloc exception: * `include <new>`的話,new不到物件的話就會回傳bad_alloc這個exception * 或是int *ptr = new( nothrow ) int[50000],來告訴系統不要throw * 或是set_new_handler(yourFunction)來指定handler,當new錯誤時就會觸發 - auto_ptr * 要`include <memory>` * auto_ptr會自動在離開該scope的時候幫你delete物件 * auto_ptr< Integer > ptrToInteger( new Integer(21) ); * auto_ptr重載了->和*,讓你可以像操作指標一樣操作它,但auto_ptr實際上只是個物件 * 缺點:只能幫你管理一個動態物件,不能用在array上 ## ch17 Random-Access Files - 每個element在檔案中的大小都一樣 - 可以很快速地選到你要的那個element - 好處是可以不用把整個檔案都load進記憶體,直接在檔案上操作 - 輸出檔案方法:outFile.write( reinterpret_cast< const char * >( &number ), sizeof( number ) ); - reinterpret_cast: 不會調整數值,只轉換型態,不同於static_cast - const_cast: 把const的物件變成non-const - dynamic_cast: - 使用reinterpret_cast有可能會有跨系統的問題,不過現在比較少了 ## ch18 String - 記憶體內的string處理 - 用法和cin cout一樣 - 可以減少io的使用次數,提升效率(io很慢) ## ch20 Data Structure 這些資料結構都需要用到self-referential class這種實作技巧 ### Linked List - Linked List的特點(相對於array): * 可動態新增、刪除元素 * 不可以直接選到想要的元素,必須一個一個跳過去 * 消耗比較多記憶體,但在記憶體上不用是連續的 ### Queue - 是Linear data structure - 可視為有限制的Array, Linked List ### Stack - 是Linear data structure - 可視為有限制的Array, Linked List ### Binary Tree - 非Linear data structure # Lab ## lab7 (4/12) GPE 11049 // 每次進行相加過後都要重新排序一次才行 // 可以用Priority queue ## lab8 (4/19) GPE 10443 Simple base conversion 把16進位數字換成10進位,或是把10進位數字換成16進位 // 有些測資好像不是用\n來分隔的!? ## lab9 (4/26) GPE 10421 All You Need is Love 輸入兩串二進位數字,判斷最大公因數有沒有大於1

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully