跳轉到內容

C++/物件簡介

來自華夏公益教科書,開放的書籍,開放的世界

由於 C++ 是一種面向物件的程式語言,您將經常使用類、物件和方法。這一切背後的理念是建立一個“模板”,供您或其他程式設計師使用,而無需不斷地使用相同的實用性定義新的變數和函式。這就是物件被稱為物件的原因,它們代表類的例項。一個很好的例子是使用現實世界的參考,儘管它與任何物理物件都沒有字面意義。現在我教授類和物件的方法與我的老師截然不同。他們首先會教你如何使用物件,然後教你如何建立一個類。但我將以相反的方式進行,因為在類上有良好的基礎更有意義,因為物件依賴於類。現在,這裡是我建立的類的程式碼

class Book
{
    public:
        int Copies;

    private:
        string Name;
        string Author;
        int Pages;
}

對於第一行,class Book,您正在建立一個名為 Book 的新類。然後在這個程式碼塊中,有兩個關鍵元素,public 和 private。總共有三種封裝方法,public、private 和 protected。每個方法都使用  : 後面跟著它。這樣做是為了組織哪些變數位於哪裡。public 表示您可以在類外部編輯變數。private 表示您無法在類外部編輯變數。封裝將在後面討論。在每個不同的封裝中,都有不同數量的變數,它們具有不同的型別。注意每個變數的位置,Copies 位於 public 下,而 Name、Author 和 Pages 位於 private 下。由於您無法在類外部編輯私有資料,因此我們需要建立 **方法** 來更改這些值。實現方法是在類中建立一個新函式。我們將建立三個新方法來更改私有資料。

class Book
{
    public:
        int Copies;

        void editName(string inName) //this will edit the Name value while staying inside the class. Keep these kinds of functions inside public.
        {
            Name = inName;
        }

        void editAuthor(string inAuthor) //this will edit the Author value while staying inside the class.
        {
            Author = inAuthor;
        }


        void editPages(int inPages) //this will edit the Pages value while staying inside the class.
        {
            Pages = inPages;
        }

    private:
        string Name;
        string Author;
        int Pages;
}

現在我們已經設定了一個基本類,讓我們看看如何使用它。

int main()
{
    Book Pre_cal; //Creates a new instance of the class Book, with the variable named being called Pre_cal
    Pre_cal.Copies = 1000;//since Copies is public, we can just edit the value directly, without having to create/call a method to change it, although you may still do that.
    Pre_cal.editName("Precalculus with Trigonometry");//To use the method editName, we use this line, with the string argument as the name of the book
    Pre_cal.editAuthor("Paul A. Foerster");//same concept, uses the method editAuthor to change the value of Author
    Pre_Cal.editPages(848);//once again, does the same thing, but to Pages

    return 0;
}

每當我們想要訪問類內部的某些內容時,我們都會在變數名後面加上一個點,然後是我們要呼叫的方法。由於 Book 類中的 Copies 變數是公共的,因此我們不必透過建立方法來更改它而繞彎。我把它設為公共的,因為通常情況下,人們會購買書籍,從而更改該值。但這並不能阻止我們建立方法來更改該值。我們可以在類中建立以下方法

    public:
        void singlePurchase()
        {
            Copies--; // shorter way of saying Copies = Copies - 1
        }

因此,我們不再每次都更改它,而是直接呼叫該方法,它會自動更改該值。要使用它,我們只需像使用 edit 方法一樣呼叫它即可

    Pre_cal.singlePurchase();

就這樣完成了。當然,這並不能阻止該值變為負數。為了防止這種情況,我們只需在該方法中新增以下內容即可

    public:
        void singlePurchase()
        {
            if(Copies>0)
                Copies--; // shorter way of saying Copies = Copies - 1
        }

現在,只有當 Copies 的值大於 0 時,該值才會發生變化。

存在一個問題,我們可以更改私有變數的值,但我們無法讀取它。為了解決這個問題,我們可以這樣做

    public:
        string Name()
        {
            return Name;
        }

        string Author()
        {
            return Author;
        }

        int Pages()
        {
            return Pages;
        }

現在您可能已經理解了 return 語句的用處。讓我們看看它是如何使用的

int main()
{
    Book Pre_cal;
    Pre_cal.editPages(848);
    int PagesInMyBook = Pre_cal.Pages();

    return 0;
}

由於我們設定了 Pages 的值,所以 Pages 現在等於 848。因此,當我們返回 Pages 時,它會返回 Pages 的值,該值反過來用於設定 PagesInMyBook 的值。因此,在程式結束時,PagesInMyBook 的值等於 848。

此頁面將繼續完善。

華夏公益教科書