Class

Text

Index

STruct

自定義資料型別

struct 名字
{
	變數1;
    變數2;
    變數3;
};
// 舉例
struct Pair
{
	int first;
    int second;
};
//宣告
Pair a;

物件導向三大特性

封裝

保護資料,避免直接被修改

同時提供介面讓使用者存取

-> 權限

 

繼承

一個物件繼承另外一個物件的屬性

-> 子物件會繼承父物件的東西

 

多型

相同的名稱,不同參數

權限

public

代表他無論誰都可以存取

 

private

only the function in class can be accessed
只有在class裡面的函式才可以存取

 

protected

只有class本身和繼承的可以存取

Struct v.s. class

class Person
{
public:
	string name;
private:
	int age;
    int height;
    int weight;
};

Struct中,預設所有成員是public

class中,則預設所有成員為private

這樣就無法直接輸出和改動了!

不能直接存取,那我要怎麼修改?

用函式! (Getter and setter)

存取的函式

class Person
{
public:
	string name;
    int getage()
    {
    	return age;
    }
private:
	int age;
    int height;
    int weight;
};

繼承

Animal

Cat

Dog

一些重複定義的變數/函式

string name;

int age;

...

class Dog : public Animal
{
public:
	int age()
    {
    	return age;
    }
}
class 子物件 : public 繼承的物件 (父物件)
{
//跟一般的物件一樣,但可以想成這裡已經包含了父物件的內容
}

初始化-destructor-建構子

class MyVector
{
private:
    int n;
    int* m;
public:
    MyVector(); // default constuctor
    MyVector(int n); // also a constuctor
    MyVector(const MyVector& a);
    ~MyVector(); // destructor
}
// the function will be like this
MyVector::MyVector()
{
    int n = 0;
    int* m = nullptr;
}

MyVector::~MyVector()
{
    delete []m;
}

Static members

class window
{
private:
    static int barColor;
public:
    static int getBarColor();
    static void setBarColor(int color); // 跟static有關的函數也要使用static
}
window a;
window b;
a.setBarColor(100); // 此時b的barColor也會變成100
window:: int getBarcolor()
{
    return window::barColor;
    // 對於static member, 用class name:: member name的方式表達
}

與整個類別有關,所有此類別的物件共用的參數

Friend

void test()
{
    MyVector v;
    v.n = 100; // 錯誤因為沒有存取權
}
//but if we declare test as friend, then it is available!
MyVector
{
friend void test();
}

const

避免改到函式裡面的數字

void function(const int &number)
{

}

&number -> 參考變數,跟原本一模一樣的變數,加上const以後變成唯讀

 

* 用參考可以避免大量物件的複製

const

避免改到函式裡面的數字-防呆機制

class Person
{
public:
	void show() const; // 這個show函式不能在裡面修改任何成員
    void show();
private:
	int age;
}

const Person constP;
constP.show(); // 只能呼叫const版本
Person P;
P.show(); // 優先呼叫一般版本

const物件-只能呼叫const函數

非const物件-均可呼叫,優先呼叫non-const

實作練習!

struct LinkedList
{
	struct node
    {
    	int val;
        node* next; // 下一個節點
        node* pre; // 上一個節點
    	//constructor and deconstructor
    };
    int listsize = 0;
    node* startNode;
    node* endNode;
    
    //請實作的內容
    node* begin(); // 回傳開始節點
    node* end(); // 回傳結尾節點
    int size(); // 回傳大小
    void erase(node* pos); // 把指定節點刪除
    void insert(node* pos, int val); // 插入新的節點在指定節點以後
    node* find(int val); // 找這個值第一次出現的節點
    void print(); // 輸出整個Linked List
}

要實作Linked list這個資料結構

多型

同樣的動作或函數,可能根據我們物件的不同,而各自定義行為

 

可以在父物件先用virtual(先不寫這個函數的定義)

那麼child的函式就會優先取代父物件的函式

class Parent {
public:
    void show() {  // 沒有 virtual
        cout << "Parent 的 show()" << std::endl;
    }
};

class Kids : public Parent {
public:
    void show() {  // 重寫基底類別函數
        cout << "Kids 的 show()" << std::endl;
    }
};

int main() {
    Kids obj;  // 建立 Kids 物件
    obj.show();  // 直接呼叫 Kids 的 show()
    Parent check = obj; // early binding
    check.show();
    return 0;
}

Operation overloading

this:c++的關鍵字,代表這個物件

class Person
{
public:
	int getAge()
    {
    	return age;
    }
private:
int age;

}
class Person
{
public:
	int getAge()
    {
    	return this->age;
    }
private:
int age;

}

上面兩個都是代表同樣的意思!

Made with Slides.com