c++學習之路【物件導向】

來源:互聯網
上載者:User

C++ 在 C 語言的基礎上增加了物件導向編程,C++ 支援物件導向程式設計。類是 C++ 的核心特性。

類的例子:

class Box{public:    int length;    int width;    int height;};int main(int argc, const char * argv[]) {    Box box1;    }

需要注意的是,私人的成員和受保護的成員不能使用直接成員訪問運算子 (.) 來直接存取。 類成員函數

類的成員函數是指那些把定義和原型寫在類定義內部的函數,就像類定義中的其他變數一樣。類成員函數是類的一個成員,它可以操作類的任意對象,可以訪問對象中的所有成員。

寫法1:(在class內定義成員函數)

class Box{public:    int length;    int width;    int height;    int getVolume(){        return length * width * height;    }};int main(int argc, const char * argv[]) {    Box box1;    box1.length = 10;    box1.width = 10;    box1.height = 10;    cout << box1.getVolume() << endl;}

寫法2:(僅在class內聲明成員函數,在class外使用::符號定義成員函數)

class Box{public:    int length;    int width;    int height;//    成員函式宣告    int getVolume();};//成員函數定義int Box::getVolume(){    return length * width * height;}int main(int argc, const char * argv[]) {    Box box1;    box1.length = 10;    box1.width = 10;    box1.height = 10;    cout << box1.getVolume() << endl;}

定義class box的完整代碼如下:

class Box{public:    int length;    int width;    int height;    int getVolume();    void setLength(int l);    void setHeight(int h);    void setWidth(int w);};void Box::setWidth(int w){    width = w;}void Box::setHeight(int h){    height = h;}void Box::setLength(int l){    length = l;}int Box::getVolume(){    return length * width * height;}int main(int argc, const char * argv[]) {    Box box;    box.setLength(10);    box.setHeight(20);    box.setWidth(15);    cout << box.getVolume() << endl;}

PS:
::叫範圍區分符,指明一個函數屬於哪個類或一個資料屬於哪個類。
:: 可以不跟類名,表示全域資料或全域函數(即非成員函數)。

int month;//全域變數int day;int year;void Set(int m,int d,int y){    ::year=y; //給全域變數賦值,此處可省略    ::day=d;    ::month=m;}Class Tdate{    public:        void Set(int m,int d,int y) //成員函數        {            ::Set(m,d,y); //非成員函數        }    private:        int month;        int day;        int year;}
類存取修飾詞 資料封裝是物件導向編程的一個重要特點,它 防止函數直接存取類類型的內部成員。類成員的訪問限制是通過在類主體內部 對各個地區標記 public、private、protected 來指定的。關鍵字 public、private、protected 稱為存取修飾詞。 一個類可以有多個 public、protected 或 private 標記地區。每個標記地區在下一個標記地區開始之前或者在遇到類主體結束右括弧之前都是有效。 成員和類的預設存取修飾詞是 private。
class Base {   public:  // 公有成員   protected:  // 受保護的成員   private:  // 私人成員};
公有成員在程式中類的外部是可訪問的。可以不使用任何成員函數來設定和擷取公有變數的值。就是說可以在class外直接操作class內的成員變數。 私人成員變數或函數在類的外部是不可訪問的,甚至是不可查看的。只有類和友元函數可以訪問私人成員。私人變數只能通過class中的成員函數來進行操作。 保護成員變數或函數與私人成員十分相似,但有一點不同,保護成員在衍生類別(即子類)中是可訪問的。

public與private區別如下:

class Student{private:    int age;public:    string name;    void setAge(int a);    int getAge();};void Student::setAge(int a){    age = a;}int Student::getAge(){    return age;}int main(int argc, const char * argv[]) {    Student stu;    stu.name = "Bob";    stu.setAge(24);    cout << stu.name << endl;    cout << stu.getAge() << endl; }
繼承中的特點

有public, protected, private三種繼承方式,它們相應地改變了基類成員的訪問屬性。
1.public 繼承:基類 public 成員,protected 成員,private 成員的訪問屬性在衍生類別中分別變成:public, protected, private
2.protected 繼承:基類 public 成員,protected 成員,private 成員的訪問屬性在衍生類別中分別變成:protected, protected, private
3.private 繼承:基類 public 成員,protected 成員,private 成員的訪問屬性在衍生類別中分別變成:private, private, private
但無論哪種繼承方式,上面兩點都沒有改變:
1.private 成員只能被本類成員(類內)和友元訪問,不能被衍生類別訪問;
2.protected 成員可以被衍生類別訪問。 類的建構函式

類的建構函式是類的一種特殊的成員函數,它會在每次建立類的新對象時執行。
建構函式的名稱與類的名稱是完全相同的,並且不會返回任何類型,也不會返回 void。建構函式可用於為某些成員變數設定初始值。

不僅成員函數可以用::定義在class外,建構函式也可以這樣定義在class外。

從以上代碼可以看出,建構函式可以來初始化成員變數的值,還有另一種寫法也可以做到這個功能。


解構函式

就是刪除建立的對象的函數。
解構函式的名稱與類的名稱是完全相同的,只是在前面加了個波浪號(~)作為首碼,它不會返回任何值,也不能帶有任何參數。解構函式有助於在跳出程式(比如關閉檔案、釋放記憶體等)前釋放資源。

簡單例子:

包含了建構函式和解構函式。

建立對象時調用了建構函式,程式結束時,自動調用了解構函式。解構函式預設存在。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.