Struct and Class === ###### tags: `code skill` # Struct ## 用途 將多個變數包裝再一起,方便管理 ## 宣告 - 結構可以同時存放不同型態的資料於同一個結構體 - 結構的定義及宣告格式如下 ![](https://i.imgur.com/CgzsWkp.png) ```C++=0 typedef struct Account { String id; String name; double balance; } Account; ``` - 下面的結構定義及宣告範例為合法的格式 ![](https://i.imgur.com/Oi6X2LT.png) ## 取值 - 結構內的成員可以利用小數點(.)來存取 ```C++=0 #include <stdio.h> typedef const char* String; struct Account { String id; String name; double balance; }; void printAcct(struct Account acct) { printf("Account(%s, %s, %f)\n", acct.id, acct.name, acct.balance); } int main() { struct Account acct; acct.id = "123-456-789"; acct.name = "Justin Lin"; acct.balance = 1000; printAcct(acct); // 顯示 Account(123-456-789, Justin Lin, 1000.000000) return 0; } ``` # class ## 用途: 相較於struct 多了 function 可以定義在內,並多了繼承、多形等不同的功能 ## 宣告 類別 (class) 用來設計自己需要的物件 (object) ,這是說,類別是物件的藍圖。 C++ 中設計類別使用關鍵字 class ,後面接大括弧宣告 (declare) 類別的成員 (member) ```C++=0 class Demo { int a; int b; int DoSomething(); }; ``` ## class -> 成員 - 成員可以是資料 (data) - 函數 (function) 與建構函數 (constructor) ### example ```C++=0 #include <iostream> using namespace std; // 宣告類別 class Demo { // 宣告 public 成員 public: int a; int b; int DoSomething(); }; // 實作 Demo 的 DoSomething() 成員函數 int Demo::DoSomething() { return a + b; } // 程式執行的 main() int main(void) { // 宣告並建立 Demo 型態的物件 t Demo t1; Demo t2; t1.a = 11; // 直接設定成員變數值 t1.b = 22; t2.a = 12; // 直接設定成員變數值 t2.b = 23; cout << endl; // 呼叫並印出 DoSomething() 的回傳值 cout << t1.DoSomething() << endl; cout << t2.DoSomething() << endl; cout << endl; return 0; } ``` # class 權限(public 與 private) public : 所有人皆可以存取 private : 只有class 內成員可以存取 ```C++=0 #include <string> using namespace std; class Account { private: string id; string name; double balance; public: void set_id(string); string get_id(); }; ``` ### example 系統顯示錯誤! 因為id是 private 成員 ```C++=0 #include<iostream> using namespace std; class Account { private: string id; string name; double balance; public: void set_id(string); string get_id(); }; int main(){ Account me; me.id = "123"; } ``` 應使用以下方式 ```C++=0 #include<iostream> using namespace std; class Account { private: string id; string name; double balance; public: void set_id(string); string get_id(); }; void Account::set_id(string data) { id = data; } string Account::get_id() { return id; } int main() { Account me; me.set_id("123"); cout << me.get_id(); } ``` ## constructor 再宣告時會執行一次的function 常用於附值 ```C++=0 #include<iostream> using namespace std; class Account { private: string id; string name; double balance; public: Account(); void set_id(string); string get_id(); }; Account::Account(){ id = "555"; name = "Eason"; } void Account::set_id(string data) { id = data; } string Account::get_id() { return id; } int main() { Account me; cout << me.get_id(); } ``` # Header file // my_class.h ``` C++=0 #ifndef MY_CLASS_H // include guard #define MY_CLASS_H namespace N { class my_class { public: void do_something(); }; } #endif /* MY_CLASS_H */ ``` // my_class.cpp ``` C++=0 #include "my_class.h" void my_class::do_something(){ cout << 123; }; ``` # Arduino Exercise step 1: Create a arduino project named tutorial step 2: type this in the file ```C++=0 #include "class_tutorial.h" void setup() { // put your setup code here, to run once: pinMode(13,OUTPUT); Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: class_tutorial hello; while(1){ hello.show(4); hello.led_on(); delay(500); hello.led_off(); delay(500); } } ``` step 3:click "新增標籤" twice ![](https://i.imgur.com/xHu0fsw.png) name them class_tutorial.cpp and class_tutorial.h class_tutorial.h ```C++=ㄢ #ifndef CLASS_TUTORIAL_H #define CLASS_TUTORIAL_H #include <Arduino.h> int hello(); class class_tutorial{ public: class_tutorial(); void show(int); void led_on(); void led_off(); private: int a; }; #endif ``` class_tutorial.cpp ```C++=0 #include <Arduino.h> #include "class_tutorial.h" class_tutorial::class_tutorial(){ a = 0; } void class_tutorial::led_on(){ digitalWrite(13,HIGH); }; void class_tutorial::led_off(){ digitalWrite(13,LOW); }; void class_tutorial::show(int count){ a+=count; Serial.println(a); }; ``` step4:run the code