structure

☘️春遊🌸

春天就會想到野餐! 沒錯我們IZCC春遊要來野餐啦~

是不是非常心動呢? 心動不如馬上行動 還不趕快報名!

快來3/30的IZCC四校聯合秋遊,段考完後一起和同屆以及學長姐們玩得盡興!

報名期限到3/15(五)23:59 請學妹們把握機會報名!

物件導向

類別vs物件

物件 : 類別的實例一個記憶體群組並能獨立管理與運算自己的資料

類別 : 物件的藍圖或模板,定義了其物件的屬性(成員變數)與行為(成員函式)

#include <iostream>
using namespace std;

class Student {
private:
    string name;  // 私有變數,外部無法直接存取
    int age;

public:
    // 建構函式(初始化物件)
    Student(string n, int a) {
        name = n;
        age = a;
    }

    // 公有函式(成員函式),提供安全的存取方式
    void introduce() {
        cout << "Hi, my name is " << name << " and I am " << age << " years old." << endl;
    }

    void setName(string n) {
        name = n;
    }

    string getName() {
        return name;
    }
};

int main() {
    // 建立 Student 物件
    Student s1("Alice", 18);
    cout << "Student Name: " << s1.getName() << endl;  // 輸出: Student Name: Alice

    // 修改名稱
    s1.setName("Bob");

    // 呼叫物件的方法
    s1.introduce(); // 輸出: Hi, my name is Bob and I am 18 years old.

    return 0;
}
class Student {
private:
    string name;  // 私有變數,外部無法直接存取
    int age;

public:
    // 建構函式(初始化物件)
    Student(string n, int a) {
        name = n;
        age = a;
    }

    // 公有函式(成員函式),提供安全的存取方式
    void introduce() {
        cout << "Hi, my name is " << name;
        cout << " and I am " << age << " years old." << endl;
    }

    // Setter:設定姓名
    void setName(string n) {
        name = n;
    }

    // Getter:取得姓名
    string getName() {
        return name;
    }
};
  • class : 定義一個類別
  • Student : 類別名稱
class Student {
private: // 私有變數,外部無法直接存取
    string name;
    int age;

public:  
	// 建構函式(初始化物件)
    Student(string n, int a) {
        name = n;
        age = a;
    }

    // 公有函式(成員函式),提供安全的存取方式
    void introduce() {
        cout << "Hi, my name is " << name;
        cout << " and I am " << age << " years old." << endl;
    }

    // Setter:設定姓名
    void setName(string n) {
        name = n;
    }

    // Getter:取得姓名
    string getName() {
        return name;
    }
};

private vs. public

用來控制類別內部成員(變數和函式)的存取權限。

存取權限 作用範圍 影響
public 類別內、類別外都可存取 外部程式可以直接存取和修改
private 只能在類別內存取 外部程式無法直接存取,必須透過公有函式存取
class Student {
private:  // 私有變數,外部無法直接存取
    string name;  
    int age;

public:
    // 建構函式(初始化物件)
    Student(string n, int a) {
        name = n;
        age = a;
    }

    // 公有函式(成員函式),提供安全的存取方式
    void introduce() {
        cout << "Hi, my name is " << name;
        cout << " and I am " << age << " years old." << endl;
    }

    // Setter:設定姓名
    void setName(string n) {
        name = n;
    }

    // Getter:取得姓名
    string getName() {
        return name;
    }
};
  • 封裝(Encapsulation): OOP 的核心概念,宣告類別的資料(name 和 age)並將其設為 private,防止外部程式直接存取,確保數據安全。
class Student {
private:  // 私有變數,外部無法直接存取
    string name;  
    int age;

public:
    // 建構函式(初始化物件)
    Student(string n, int a) {
        name = n;
        age = a;
    }

    // 公有函式(成員函式),提供安全的存取方式
    void introduce() {
        cout << "Hi, my name is " << name;
        cout << " and I am " << age << " years old." << endl;
    }

    // Setter:設定姓名
    void setName(string n) {
        name = n;
    }

    // Getter:取得姓名
    string getName() {
        return name;
    }
};
  • 建構函式(Constructor)
  • 建構函式是特殊的函式,當物件被創建時自動執行。
  • 作用:負責初始化物件的屬性,確保物件建立時擁有正確數據。例如                                      ,   會自動執行Student() 來設定 name="Alice" 和 age = 18。 
Student s1("Alice", 18);
class Student {
private:  // 私有變數,外部無法直接存取
    string name;  
    int age;

public:
    // 建構函式(初始化物件)
    Student(string n, int a) {
        name = n;
        age = a;
    }

    // 公有函式(成員函式),提供安全的存取方式
    void introduce() {
        cout << "Hi, my name is " << name;
        cout << " and I am " << age << " years old." << endl;
    }

    // Setter:設定姓名
    void setName(string n) {
        name = n;
    }

    // Getter:取得姓名
    string getName() {
        return name;
    }
};
  • 成員函式(Member Function)
  • 成員函式(又稱方法)是類別內的函式,用來操作類別內的變數,introduce() 會讀取 name 和 age,並輸出學生的自我介紹。
  • 物件呼叫方式:
s1.introduce();
class Student {
private:  // 私有變數,外部無法直接存取
    string name;  
    int age;

public:
    // 建構函式(初始化物件)
    Student(string n, int a) {
        name = n;
        age = a;
    }

    // 公有函式(成員函式),提供安全的存取方式
    void introduce() {
        cout << "Hi, my name is " << name;
        cout << " and I am " << age << " years old." << endl;
    }

    // Setter:設定姓名
    void setName(string n) {
        name = n;
    }

    // Getter:取得姓名
    string getName() {
        return name;
    }
};
  • Getter 和 Setter(存取函式)
  • getter - getName():回傳 private 變數的值,允許外部安全存取變數。
  • setter - setName():修改 private 變數,但可以加入條件檢查
s1.name = "Bob"; ❌
s1.setName("Bob")✅
int main() {
    // 建立 Student 物件
    Student s1("Alice", 18);
    cout << "Student Name: " << s1.getName() << endl;  
    // 透過getter取得name的值。 // 輸出:Student Name: Alice

    // 透過 setter 修改 name。
    s1.setName("Bob");

    // 呼叫成員函式來顯示學生資訊。
    s1.introduce(); 
    // 輸出: Hi, my name is Bob and I am 18 years old.

    return 0;
}

structure

什麼是structure???

在物件導向程式設計(OOP)中,struct 可以視為簡單版本的 class,它仍然可以用來創建物件。在 C++ 裡,struct 可以用來定義一組相關的變數,也可以像 class 一樣包含成員函式、建構函式、繼承,甚至支援物件導向。

儲存很多個int / char                  陣列 

but…

要儲存一個int  和一個char 呢?     

甚至...是要儲存陣列?

 

structure

structure是一種 "資料形態"

可以將不同的資料形態同時儲存

定義

#include <iostream>
using namespace std;

struct checkup{
    string name;
    string personality;
    double height; 
    int weight;
    double eyesight[2];
    void BMI(){
        cout << weight/(height*height) << endl;
    }
}student2;

int main(){
}

(在主程式之外)

賦值

#include <iostream>
using namespace std;

struct checkup{
    string name;
    string personality;
    double height; 
    int weight;
    double eyesight[2];
    void BMI(){
        cout << weight/(height*height) << endl;
    }
}student2;

int main(){
	checkup student1={"嘎米", "暴躁吉娃娃", 1.88, 70, 1.0, 1.2};
	student1.BMI();
	
    cin >> student2.name;
	cout << "姓名" << student2.name << endl;
}

struct也可以用陣列

struct Grade {
    string name;
    int score;
};
    
Grade student[3] = {
    {"妘", 100},
    {"C.Y", 100},
    {"吱吱", 100}
};

struct也可以放在struct裡

struct id{
    int num;
    string name;
};
 
struct checkup{
    id stu;
    int height;
    int weight;
    int eyesight[2];
};

ps. 在自己裡面呼叫自己會CE

小練習

假設你是🍁資的總務,你要做一個今年社團的支出表!


假設收入10000,有n個項目,請分別輸入支出的項目、相應金額以及是否已經報銷(bool)。最後輸出剩餘的金額(bool = 1)

Quiz1

測資

5

食物 1500 1 

場地租金  2000  0 

獎品 800 0

交通費 300 1

傳單 700  1 

 

ans:

剩餘  7500

Ans

#include <iostream>
#include <string>
using namespace std;

// 定義支出項目結構
struct expenditure {
    string item;
    int amount;
    bool pay;
}expend[100];// 創建支出項目陣列

int main() {
    int income = 10000; // 收入
    int numItems; //支出項目數量
    cin >> numItems;

    // 輸入支出項目資料
    for (int i = 0; i < numItems; i++) {
        cin >> expend[i].item >> expend[i].amount >> expend[i].pay;
    }

    // 計算總支出
    int total = 0l;
    for (int i = 0; i < numItems; i++) {
        if (expend[i].pay) {
            total += expend[i].amount;
        }
    }
  
    // 輸出剩餘金額
    cout << "剩餘 " << income - total << endl;
    return 0;
}

Quiz2

設計一個社團成員管理系統,透過structure 來記錄社團成員的資訊,每位社團成員擁有一下成員資訊:姓名、學號、所屬社團資訊(社團名字、社團職位)(巢狀結構)

測資

呱呱 11230123 資訊研究社 文書

起飛 11230026 資訊研究社 教學

#include <iostream>
using namespace std;

// 定義社團資訊的 struct
struct Club {
    string name;      // 社團名稱
    string position;  // 社團職位
};
struct ClubMember {
    string name;  // 成員姓名
    int id;       // 學號
    Club club;    // 社團資訊(嵌套結構)
};

int main() {
    ClubMember member;
    cin >> member.name >> member.id >> member.club.name >> member.club.position;

    // 輸出社團成員資訊
    cout << "社團成員姓名:" << member.name << endl;
    cout << "學號:" << member.id << endl;
    cout << "所屬社團:" << member.club.name << endl;
    cout << "社團職位:" << member.club.position << endl;
    return 0;
}

KAHOOT

structure

By phoebe tsai

structure

  • 165