類和對象(C++)

類和對象
初識

在我看來,類其實與結構體有點像,只不過不特殊說明時類中的成員變量或函數是私有的,而結構體則是公有的。
類中有三種成員訪問指示符,分別爲Private、Public、Protected。如下:
class Car
{
private:            //只允許類的成員函數訪問
    int num;
    
public:             //程序的任意函數均可訪問
    char * name;
    
protected:          //對於派生類來說是公有的,對於程序的其他部分來說是私有的
    int price;
        
};
一般我們不在類中對成員變量進行賦值。

類的數據成員的特例————靜態數據成員

在定義類對象時,不會爲每個類對象都複製一份靜態數據成員,而是所有的類對象共享一份靜態數據成員備份。其使用如下:
#include<iostream>
using namespace std;

class Car
{
public:
    static int n;
    void counter();
};
<span style="color:#ff0000;">int Car::n = 0;	</span>	//靜態成員的初始化,不可在類內直接初始化,否則會報錯
void Car::counter()
{
    n++;
}
int main(void)
{
    Car car1;
    car1.counter();
    cout<<"static n = "<<Car::n<<endl;
    Car car2;
    car2.counter();
    Car car3;
    car3.counter();
    cout<<"static n = "<<Car::n<<endl;

    return 0;
}
/*
結果:
    static n = 1
    static n = 3
*/

類體外定義賦值

上面出現了一種新的運算符 ::  ,符號::爲域運算符,語句 int Car::n = 0; 表示這裏所賦值的n屬於Car這個類;同樣語句void Car::counter()表示count函數是屬於Car類的。
const成員函數
const 放在函數的參數列表的函數體之間。const成員函數只能讀取的數據成員,而不能修改類成員數據,定義如下:
(1)類內定義時:
類型  函數名  (參數列表)  const
{
函數體
}

(2)類外定義時:
類型  類名::函數名  (參數列表)  const
{
函數體
}

構造函數
在創造類後,我們通常要對類成員進行初始化。這時構造函數就派上用場了。下面我們來看看如何用構造函數來初始化類成員。例子如下:
#include<iostream>

using namespace std;

class Student
{
public:
    int math;
    int english;
    int biology;
    Student(int math = 1,int english = 1,int biology = 1);  //這裏構造函數的默認初始值爲1
    void display()      //類體內定義函數
    {
        cout<<"Math = "<<math<<",English = "<<english<<",Biology = "<<biology<<endl;
    }
};
Student::Student(int m,int e,int b) //類體外定義構造函數,構造函數必須與類名相同
{
    math = m;
    english = e;
    biology = b;
}
int main(void)
{
    Student stu1;
    stu1.display();
    Student stu2(100,80,95);
    stu2.display();

    return 0;
}
/*
運行結果:
Math = 1,  English = 1, Biology = 1
Math = 100,English = 80,Biology = 95
*/
構造函數也可進行重載,其取決於參數的個數和參數的類型。

特殊的構造函數————複製構造函數

類名(類名  &對象名)
{
函數體
}

下面是自定義的複製構造函數例子:
#include<iostream>

using namespace std;
class Student
{
public:
    int a;
    int b;
    Student(Student &stu);
    Student(int aa,int bb);
    void show()
    {
        cout<<"a = "<<a<<", b = "<<b<<endl;
    }
};
Student::Student(Student &stu)
{
    a = stu.a+10;
    b = stu.b+20;
}
Student::Student(int aa,int bb)
{
    a = aa;
    b = bb;
}
int main(void)
{
    Student stu1(1,2);
    stu1.show();

    Student stu2(stu1);
    stu2.show();

    return 0;
}
/*
運行結果:
a = 1,  b = 2
a = 11, b = 22
*/

析構函數

析構函數的作用:釋放分配給對象的內存空間,並做一些善後工作。
要點:
(1)析構函數必須與類名相同,在函數名前加 ~ 號。
(2)析構函數沒有參數,沒有返回值,不能被重載,一個類中只能有一個析構函數。
(3)當撤銷對象時,系統會自動調用析構函數。

#include<iostream>

using namespace std;
class Car
{
    int n;
public:
    Car(int nn);    //構造函數
    ~Car();         //析構函數
    void display()
    {
        cout<<"Buding...."<<endl<<endl;
        cout<<"n = "<<n<<endl<<endl;
    }

};
Car::Car(int nn)
{
    n = nn;
}
Car::~Car() //析構函數類體外定義
{
    cout<<"Releasing...."<<endl;
}
int main(void)
{
    Car car1(100);
    car1.display();

    return 0;
}
/*
運行結果:
Buding....

n = 100

Releasing....
*/

this指針

this指針是隱含在成員函數內的一種指針,稱爲指向本對象的指針。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章