lingege32
    • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- type: slide tags: C++ slideOptions: theme: white --- # C++ structure and class --- #### What is struct/class * What: Encapsulate multiple data to one. * Why: Give the structure a meaning. * How: ``` cpp struct People { std::string name; int age; int height; int weight }; ``` --- #### The expression of the struct/class * Keyword: `struct/class` * Member `variables/functions` * Access specifiers: `public/private/protected` * 表示外部的code是否可以訪問這個作用域 * 如果表示為private的函式跟變數,除了自己以外不能使用 * 如果表示為public的函式或變數則為公開變數,可以供人使用 * Remember add the semicolon at the end. --- #### The expression of the struct/class ``` cpp // use class to define class People { // Access specifiers public: // member functions std::string getName() {return name;} std::string getAge() {return age;} void setName(std::string n){name=n;} void setAge(int a){age=a;} void introduceMySelf() { std::cout<<"My name is "<<getName() <<", and age is "<<getAge()<<"\n"; } private: // member variables std::string name; int age; }; // <-- Add the semicolon ``` --- ### Example ``` cpp int main() { std::string name; int age; std::cin>>name>>age; People Ann; // Ann.name = name, compile error because name is a private member // Ann.age = age, compile error because age is a private member Ann.setName(name); Ann.setAge(age); Ann.introduceMySelf(); // My name is Ann, and age is 18 } ``` --- #### What is difference between class and struct. * In C language, there is only struct and no member functions in it. * In C++ language, only one difference. * The default access specifier is private for class and public for struct. --- #### LifeTime and Initialization * 所有的變數都有它的生命週期 * 全域變數會存活在整個程式中,生命週期無限大 * 區域變數會存活在某一個區間 * 兩個括號之間{} * 或者一個function之間 --- #### LifeTime and Initialization * 區域變數會存活在某一個區間 * 兩個括號之間{} * 或者一個function之間 ``` cpp int main() { int a = 0; // 在宣告時創造,在離開main時結束 if (a==0) { People Ann; // 在進入if後創造它,並在離開if時結束 Ann.setAge(18); std::cout<<Ann.getName()<<"\n"; // ↑--------------↑--- // getName回傳的std::string只存在於<<之間 } { People Ann; // 在進入這個{}後創造它,並在離開{}時結束 } } ``` --- #### LifeTime and Initialization * 所有的變數都有它的生命週期 * 全域變數會存活在整個程式中,生命週期無限大 * 區域變數會存活在某一個區間 * 兩個括號之間{} * 或者一個function之間 * 任何變數在使用前都要先思考要怎麼初始化,以及怎麼解構 * class has constructor/desturctor --- #### LifeTime and Initialization ![image](https://hackmd.io/_uploads/BkeYYN5WA.png) --- #### Initialization * 有兩種初始化的寫法 * 第一種直接在宣告member variable的時候給定初始值 * Constructor ```cpp= class People { int age=0; std::string name=""; } ``` --- #### Initialization * Constructor * 於class中特殊的寫法,使用跟class相同名稱當作開頭 * 從: 到 {} 中間為initialize list(初始化列表),以','分隔 * 初始化列表內的順序要跟member一致 * {}內可想像成function body,做一些初始化會做的事情 ```cpp= class People { public: People():age{0}, name{""}{} private: int age; std::string name; } ``` --- #### Initialization * Constructor 可接受多個變數 ``` cpp= class People { public: People(std::string n, int a):age{a}, name{n}{} private: int age; std::string name; } int main() { People Ann{"Ann", 18}; return 0; } ``` --- #### specifal constructor and destructor * default constructor * copy constructor * copy assignment * move constructor * move assignment * destructor --- #### specifal constructor and destructor ```cpp= int main() { People Ann; // Default constructor People Ann2 = Ann; // Copy constructor People Ann3; // Default constructor Ann3 = Ann; // Copy Assignment; Poeple Ann4 = std::move(Ann3) // move constructor Ann3 = std::move(Ann); // move assignment; } // Call destructor of Ann/Ann2/Ann3/Ann4 before leave main ``` --- #### specifal constructor and destructor ``` cpp= class People { public: // Default constructor People():age{0}{} // Copy constructor, use const People& in the parameters. People(const People& p):age{p.age}{} // Move constructor, like copy but use People&& People(People&& p):age{p.age}{} // Copy Assignment, redefine the = for People // Return type must be People& // Return *this at the end. People& operator=(const People& p){age=p.age; return *this;} // Move Assignment, like copy assignment but parameter use People&& People& operator=(People&& p){age=p.age; return *this;} // Destructor ~People(){} private: int age; } ``` --- #### Example * [Link](https://godbolt.org/z/1PPPPbE97) ``` cpp= class People { public: People():name{"Default"}{std::cout<<"I'm born!\n";} ~People(){std::cout<<"I'm "<<name<<", and I leave!\n";} void setName(std::string n){name=n;} private: std::string name; }; ``` ```cpp= int main() { People Ann; Ann.setName("Ann"); { People Terry; Terry.setName("Terry"); } } ``` --- #### Example 2 * 請問這邊會印出甚麼東西來. [Link](https://godbolt.org/z/dYnEbKzvv) ``` cpp= class People { public: People() {std::cout<<"Ctor\n";} People(const People& p) {std::cout<<"Copy Ctor\n";} People(People&& p) {std::cout<<"Move Ctor\n";} People& operator=(const People& p) {std::cout<<"Copy Ass\n"; return *this;} People& operator=(People&& p) {std::cout<<"Move Ass\n"; return *this;} ~People() {std::cout<<"Dtor\n";} }; int main() { People p1; People p2 = p1; People p3 = std::move(p2); p3 = p1; p3 = std::move(p1); } ``` --- #### Constructor and Destructor * 為什麼需要這麼複雜的constructor跟destructor呢 * 在未來會學到pointer,或者你用到別人設計的class時會用到 * pointer需要更為複雜的初始化跟解構 * 別人了class可能需要複雜的初始化以及解構 * Rule of 5 or Rule of 0 * 意思就是說,要馬全部的constructor都寫出來。要馬都不寫。 * 現階段妳可以先不寫這些東西 --- #### You can define everything outside the class * Why: 你的class應當簡潔有力,把定義包含其中會顯得混亂 * How: 在定義時加入 \<ClassType>:: ```cpp= class People { public: People(); People(int a); ~People(); int getAge(); void setAge(int a); private: int age; }; int main(){ //... } People::People():age{0} {} People::People(int a):age{a}{} People::~People(){} int People::getAge(){return age;} void People::setAge(int a){age=a;} ``` --- #### Exercise * 設計一個簡單的點餐系統。 1. 請輸入Steak, Fish, Chicken, IceCream, Cake的價格 2. 輸入預定人名以及想要預定的時間 3. 輸入想要吃的主餐是甚麼(Steak, Fish, Chicken, None擇一) 4. 輸入想吃的甜點是甚麼(IceCream, Cake, None擇一 5. 輸入是否有會員(Yes/No, 有會員的話打九折) 6. 輸出人名,時間,主餐,甜點,是否有折價,總價格 --- #### Exercise * [Link](https://godbolt.org/z/K3qrr1E4b) * 輸入如下 * ![image](https://hackmd.io/_uploads/r1NeQHc-A.png) * 輸出如下 * ![image](https://hackmd.io/_uploads/r10_MBqW0.png) * [ANS](https://godbolt.org/z/x981v9sE3)

    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
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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