Structs are a way to group several related variables into one place.
Structure
by 洪翠憶
使用時機

人參導師
感到困擾的導師
好困擾啊~
想儲存信徒的資料,
但是光一位信徒,也就是一筆資料,
就包括了很多型態,名字是字串、年齡是整數......,
好像沒辦法存在同個陣列裡,畢竟同個陣列只接受一種資料型態。
該怎麼辦呢?
使用時機

筍克里斯多夫安東尼奧班尼迪克席爾維特歐薩斯
人參導師的大弟子
有個名為結構(structure)的型態,
可以幫助導師建立自己的型態,
以儲存不同型態的資料在同個變數裡,
讓資料整理得更整齊。
當然也可以利用structure建立的自定義型態建立陣列,
儲存信徒名單。
使用時機
After
Before
名字(str) |
---|
蘿蔔1號 |
蘿蔔2號 |
蘿蔔3號 |
... |
年齡(int) |
---|
3 |
3 |
3 |
... |
...
struct radish |
---|
name |
age |
... |
信徒(radish) |
---|
name="蘿蔔1號", age=3, ... |
name="蘿蔔2號", age=3, ... |
... |
Basic Syntax
- 建立Structure
- 使用Structure
- 範例
Create a Structure
struct 結構名稱{
型態 名稱 = 初始值;
...
};
↑注意分號
←可以不設定初始值
↓這裡沒有小括號
struct radish{
std::string name;
int *age;
};
範例
可以不取名字↓
Declare a Structure Variable
struct{
...
}名稱1, 名稱2, ...;
這裡沒逗號↑
struct radish{
std::string name;
int age;
}theList[87];
範例
↑宣告多個時,
用逗號隔開
1. 直接在後面宣告
結構名稱 變數名稱 = {值1, 值2, ...};
2. 當成一個型態宣告
radish theList[87];
範例
↑宣告的同時可在大括號內賦值
Access Structure Members
結構變數名稱.成員 = 值;
賦值/修改成員
std::cout << theList[0].name;
範例
變數 = 結構變數名稱.成員;
獲取成員的值
結構變數名稱.成員 = 值;
theList[0].name = "蘿蔔1號";
範例
Example
#include <iostream>
struct radish{
std::string name;
int age;
};
int main(){
radish theList[87];
for(int i = 0; i < 87; i++){
theList[i].name += "蘿蔔";
theList[i].name += std::to_string(i+1);
theList[i].name += "號";
theList[i].age = 3;
}
std::cout << "以下為信徒名單:\n";
for(int i = 0; i < 87; i++){
std::cout << theList[i].name << ", " << theList[i].age << '\n';
}
return 0;
}
/*output:
以下為信徒名單:
蘿蔔1號, 3
蘿蔔2號, 3
蘿蔔3號, 3
蘿蔔4號, 3
蘿蔔5號, 3
蘿蔔6號, 3
蘿蔔7號, 3
蘿蔔8號, 3
蘿蔔9號, 3
蘿蔔10號, 3
蘿蔔11號, 3
蘿蔔12號, 3
蘿蔔13號, 3
蘿蔔14號, 3
蘿蔔15號, 3
蘿蔔16號, 3
蘿蔔17號, 3
蘿蔔18號, 3
蘿蔔19號, 3
蘿蔔20號, 3
蘿蔔21號, 3
蘿蔔22號, 3
蘿蔔23號, 3
蘿蔔24號, 3
蘿蔔25號, 3
蘿蔔26號, 3
蘿蔔27號, 3
蘿蔔28號, 3
蘿蔔29號, 3
蘿蔔30號, 3
蘿蔔31號, 3
蘿蔔32號, 3
蘿蔔33號, 3
蘿蔔34號, 3
蘿蔔35號, 3
蘿蔔36號, 3
蘿蔔37號, 3
蘿蔔38號, 3
蘿蔔39號, 3
蘿蔔40號, 3
蘿蔔41號, 3
蘿蔔42號, 3
蘿蔔43號, 3
蘿蔔44號, 3
蘿蔔45號, 3
蘿蔔46號, 3
蘿蔔47號, 3
蘿蔔48號, 3
蘿蔔49號, 3
蘿蔔50號, 3
蘿蔔51號, 3
蘿蔔52號, 3
蘿蔔53號, 3
蘿蔔54號, 3
蘿蔔55號, 3
蘿蔔56號, 3
蘿蔔57號, 3
蘿蔔58號, 3
蘿蔔59號, 3
蘿蔔60號, 3
蘿蔔61號, 3
蘿蔔62號, 3
蘿蔔63號, 3
蘿蔔64號, 3
蘿蔔65號, 3
蘿蔔66號, 3
蘿蔔67號, 3
蘿蔔68號, 3
蘿蔔69號, 3
蘿蔔70號, 3
蘿蔔71號, 3
蘿蔔72號, 3
蘿蔔73號, 3
蘿蔔74號, 3
蘿蔔75號, 3
蘿蔔76號, 3
蘿蔔77號, 3
蘿蔔78號, 3
蘿蔔79號, 3
蘿蔔80號, 3
蘿蔔81號, 3
蘿蔔82號, 3
蘿蔔83號, 3
蘿蔔84號, 3
蘿蔔85號, 3
蘿蔔86號, 3
蘿蔔87號, 3
*/
Function in Struct
- 語法
- 範例
Syntax
struct 結構名稱{
型態 函式名稱(型態 參數1, 型態 參數2, ...){
...
}
...
};
稱為「成員函式」或者「方法」
→
struct 結構名稱{
函式型態 函式名稱(型態 參數1, 型態 參數2, ...);
...
};
函式型態 結構名稱::函式名稱(型態 參數1, 型態 參數2, ...){
...
}
#include <iostream>
struct radish{
std::string name;
int age;
void print(){
std::cout << name << ", " << age << '\n';
}
};
int main(){
radish theList[87];
for(int i = 0; i < 87; i++){
theList[i].name += "蘿蔔";
theList[i].name += std::to_string(i+1);
theList[i].name += "號";
theList[i].age = 3;
}
std::cout << "以下為信徒名單:\n";
for(int i = 0; i < 87; i++){
theList[i].print();
}
return 0;
}
/*output:
以下為信徒名單:
蘿蔔1號, 3
蘿蔔2號, 3
蘿蔔3號, 3
蘿蔔4號, 3
蘿蔔5號, 3
蘿蔔6號, 3
蘿蔔7號, 3
蘿蔔8號, 3
蘿蔔9號, 3
蘿蔔10號, 3
蘿蔔11號, 3
蘿蔔12號, 3
蘿蔔13號, 3
蘿蔔14號, 3
蘿蔔15號, 3
蘿蔔16號, 3
蘿蔔17號, 3
蘿蔔18號, 3
蘿蔔19號, 3
蘿蔔20號, 3
蘿蔔21號, 3
蘿蔔22號, 3
蘿蔔23號, 3
蘿蔔24號, 3
蘿蔔25號, 3
蘿蔔26號, 3
蘿蔔27號, 3
蘿蔔28號, 3
蘿蔔29號, 3
蘿蔔30號, 3
蘿蔔31號, 3
蘿蔔32號, 3
蘿蔔33號, 3
蘿蔔34號, 3
蘿蔔35號, 3
蘿蔔36號, 3
蘿蔔37號, 3
蘿蔔38號, 3
蘿蔔39號, 3
蘿蔔40號, 3
蘿蔔41號, 3
蘿蔔42號, 3
蘿蔔43號, 3
蘿蔔44號, 3
蘿蔔45號, 3
蘿蔔46號, 3
蘿蔔47號, 3
蘿蔔48號, 3
蘿蔔49號, 3
蘿蔔50號, 3
蘿蔔51號, 3
蘿蔔52號, 3
蘿蔔53號, 3
蘿蔔54號, 3
蘿蔔55號, 3
蘿蔔56號, 3
蘿蔔57號, 3
蘿蔔58號, 3
蘿蔔59號, 3
蘿蔔60號, 3
蘿蔔61號, 3
蘿蔔62號, 3
蘿蔔63號, 3
蘿蔔64號, 3
蘿蔔65號, 3
蘿蔔66號, 3
蘿蔔67號, 3
蘿蔔68號, 3
蘿蔔69號, 3
蘿蔔70號, 3
蘿蔔71號, 3
蘿蔔72號, 3
蘿蔔73號, 3
蘿蔔74號, 3
蘿蔔75號, 3
蘿蔔76號, 3
蘿蔔77號, 3
蘿蔔78號, 3
蘿蔔79號, 3
蘿蔔80號, 3
蘿蔔81號, 3
蘿蔔82號, 3
蘿蔔83號, 3
蘿蔔84號, 3
蘿蔔85號, 3
蘿蔔86號, 3
蘿蔔87號, 3
*/
Example
Struct with Pointer
- 使用指標Structure
- 範例
Access the Members
triangle->b = 12;
範例
(*指標結構變數名稱).成員
↓
指標結構變數名稱->成員
Example
#include <cmath>
#include <iostream>
struct pythagoras{
double a = 3;
double b = 4;
double c = 5;
void answer(){
c = sqrt(a*a + b*b);
std::cout << "hypotenuse = " << c << '\n';
}
};
int main(){
pythagoras *triangle = new pythagoras;
if(triangle == NULL)
return -1;
(*triangle).a = 5;
triangle->b = 12;
triangle->answer();
delete triangle;
return 0;
}
//hypotenuse = 13
Object-Oriented Programming(OOP)
- 介紹
- Class vs Structure
物件導向程式語言(Object-oriented programming, OOP)
-
將物件作為程式的基本單元,可能包含資料(data)、屬性(attribute)、程式碼(code)與方法(method)
-
每一個物件都應該能夠接受資料、處理資料並將資料傳達給其它物件,因此它們都可以被看作一個小型的「機器」
-
物件則指的是類別(class)的實例,可以把類別想成是設計物件的藍圖
-
物件導向程式語言包含Python、C++、Java等
三大特性:封裝(Encapsulation)、繼承(Inheritance)、多型(Polymorphism)
Introduction
類別(class) | 結構(structure) |
---|---|
成員預設是private | 成員預設是public |
reference類型的資料型態 | value類型的資料型態 |
用class關鍵字宣告 | 用struct關鍵字宣告 |
Class vs Structure
Encapsulation
- 介紹
封裝
-
物件內部的資料隱藏起來,只能透過物件本身所提供的介面(interface)取得物件內部屬性或者方法
-
對一件事情只需要理解他的外在就好,不需要了解裡面內部的構造。
Introduction
學生
自我介紹()
嘿!學生,自我介紹。
我的名字不叫吉良吉影,16歲。住在exe執行後跳出的小黑框內,未婚......
Inheritance
- 介紹許可權
- 語法
- 範例
許可權
-
public
-
可被大家存取
-
可被繼承
-
-
protected
-
只可被子類別存取
-
可被繼承
-
-
private
-
不可被存取
-
不可繼承
-
Visibility Modes
public | protected | private | |
---|---|---|---|
可被繼承 | O | O | X |
存取權 | 大家都有 | 僅限子類別 | X |
父類別
子類別
Syntax
struct 子結構名稱 : 存取權 父結構{
private:
...
protected:
...
public:
...
};
←也可以每個成員前面都打一次
建立子結構
↓沒打的話預設為public
Example
#include <iostream>
struct parent{
private:
void paPri(){
std::cout << "This is parent's private.\n";
}
protected:
void paPro(){
std::cout << "This is parent's protected.\n";
}
public:
void paPub(){
std::cout << "This is parent's public.\n";
}
};
struct child : parent{
private:
void chPri(){
//paPri();
std::cout << "This is child's private.\n";
}
protected:
void chPro(){
paPro();
std::cout << "This is child's protected.\n";
}
public:
void chPub(){
paPub();
std::cout << "This is child's public.\n";
}
};
struct grandchild : child{
private:
void grPri(){
//paPri();
std::cout << "This is grandchild's private.\n";
}
protected:
void grPro(){
paPro();
std::cout << "This is grandchild's protected.\n";
}
public:
void grPub(){
paPub();
std::cout << "This is grandchild's public.\n";
}
};
int main(){
parent A;
child B;
grandchild C;
//A.paPri();
//A.paPro();
A.paPub();
//B.chPri();
//B.chPro();
B.paPub();
B.chPub();
//C.grPri();
//C.grPro();
C.paPub();
C.chPub();
C.grPub();
return 0;
}
/*output:
This is parent's public.
This is parent's public.
This is parent's public.
This is child's public.
This is parent's public.
This is parent's public.
This is child's public.
This is parent's public.
This is grandchild's public.
*/
Example
#include <iostream>
struct parent{
private:
void paPri(){
std::cout << "This is parent's private.\n";
}
protected:
void paPro(){
std::cout << "This is parent's protected.\n";
}
public:
void paPub(){
std::cout << "This is parent's public.\n";
}
};
struct child : protected parent{
private:
void chPri(){
//paPri();
std::cout << "This is child's private.\n";
}
protected:
void chPro(){
paPro();
std::cout << "This is child's protected.\n";
}
public:
void chPub(){
paPub();
std::cout << "This is child's public.\n";
}
};
struct grandchild : private child{
private:
void grPri(){
//paPri();
std::cout << "This is grandchild's private.\n";
}
protected:
void grPro(){
paPro();
std::cout << "This is grandchild's protected.\n";
}
public:
void grPub(){
paPub();
std::cout << "This is grandchild's public.\n";
}
};
int main(){
parent A;
child B;
grandchild C;
//A.paPri();
//A.paPro();
A.paPub();
//B.chPri();
//B.chPro();
//B.paPub();
B.chPub();
//C.grPri();
//C.grPro();
//C.paPub();
//C.chPub();
C.grPub();
return 0;
}
/*output:
This is parent's public.
This is parent's public.
This is child's public.
This is parent's public.
This is grandchild's public.
*/
Polymorphism
- 多載
- 複寫
- 範例
多載
-
同個類別內同名的方法,其參數不同,即可依據填入的參數選擇要使用的方法
Overloading
struct eng{
void say(std::string a, std::string b){
std::cout << "One is " << a << ", and the other is " << b << ".\n";
}
void say(std::string a, std::string b, std::string c){
std::cout << "One is " << a << ", another is " << b << ", and the other is " << c << ".\n";
}
};
範例
複寫
-
當子類別中有與父類別名稱相同的方法時,子類別會覆蓋之。
Overriding
struct parent{
int age;
int birth(){
return (2022 - age);
}
};
struct child : parent{
int birth(){
return 2022;
}
};
範例
Example
#include <iostream>
struct eng{
void say(std::string a, std::string b){
std::cout << "One is " << a << ", and the other is " << b << ".\n";
}
void say(std::string a, std::string b, std::string c){
std::cout << "One is " << a << ", another is " << b << ", and the other is " << c << ".\n";
}
};
struct child : eng{
void say(){
std::cout << "I don't know!\n";
}
};
int main(){
eng hi;
child hey;
hi.say("an apple", "a banana");
hi.say("a cat", "a dog", "an eagle");
//hey.say("a flower", "a guitar");
hey.say();
return 0;
}
/*output
One is an apple, and the other is a banana.
One is a cat, another is a dog, and the other is an eagle.
I don't know!
*/
Constructor & Destructor
- 介紹
- 語法
- 範例
建構式(Constructor)
-
進行物件初始化的成員函式
解構式(Destructor)
-
在物件被釋放時處理善後(釋放動態配置的記憶體、撰寫紀錄檔等等)的成員函式
-
不具參數
它們皆不具有回傳值,名稱與類別名稱相同(解構式前會多一個「~」)
Introduction
Syntax
struct 名稱{
public:
名稱(){ //建構式
...
}
~名稱(){ //解構式
...
}
...
};
Example
#include <iostream>
struct cre{
public:
int *me;
char *andme;
cre(){
me = new int;
andme = new char;
std::cout << "created\n";
}
~cre(){
delete me;
delete andme;
std::cout << "deleted\n";
}
};
int main(){
cre hi;
*hi.me = 87;
*hi.andme = 'D';
std::cout << *hi.me << *hi.andme << '\n';
return 0;
}
/*output:
created
87D
deleted
*/
Kahoot!
Reference
Structure
By zsisc28_0okatrinao0
Structure
- 143