C++<第十四篇>:類

C++是面向?qū)ο笳Z言,每一個(gè)對象都可以用類來體現(xiàn),使用 class 關(guān)鍵字可以聲明一個(gè)類。

(1)類的一般形式
class 類名
{
    數(shù)據(jù)成員
}
(2)類的聲明和定義
class Person
{
    /*數(shù)據(jù)成員*/
    int mIndex;
    char mName[25];
    short mAge;

    /*成員函數(shù)*/
    int getIndex();
    void setIndex(int index);
    char getName();
    void setName(char name[25]);
    short getAge();
    void setAge(short age);
};

class 是定義類的關(guān)鍵字;
Person 是類名;
以及函數(shù)體內(nèi)的 數(shù)據(jù)成員成員函數(shù);

(3)成員函數(shù)的實(shí)現(xiàn)

什么叫成員函數(shù)的聲明?

int getIndex();

以上代碼直接使用分號結(jié)束,沒有大括號,這樣叫做函數(shù)的聲明。

什么叫成員函數(shù)的實(shí)現(xiàn)?

int getIndex(){}

像這樣,含有大括號的函數(shù)叫函數(shù)的實(shí)現(xiàn)。

(4)將函數(shù)的實(shí)現(xiàn)放在類內(nèi)
#include <iostream>
#include <string>

using namespace std;

class Person
{
private:
    /*數(shù)據(jù)成員*/
    int mIndex;
    char mName[25];
    short mAge;

public:
    /*成員函數(shù)*/
    int getIndex() {
        return mIndex;
    }
    void setIndex(int index) {
        mIndex = index;
    }
    char* getName() {
        return mName;
    }
    void setName(char name[25]) {
        strcpy_s(mName, strlen(name) + 1, name);
    }
    short getAge() {
        return mAge;
    }
    void setAge(short age) {
        mAge = age;
    }
};

int main()
{
    Person person;
    person.setIndex(1);
    char name[25] = "zhangsan";
    person.setName(name);
    person.setAge(11);

    cout << person.getIndex() << endl;
    cout << person.getName() << endl;
    cout << person.getAge() << endl;

    return 0;
}

類中的代碼可能比較多,我們將類相關(guān)代碼放入 Person.cpp 文件中,如果想要使用Person類,只需要添加以下代碼即可:

#include "Person.cpp"

比如:

#include <iostream>
#include "Person.cpp"

using namespace std;


int main()
{
    Person person;
    person.setIndex(1);
    char name[25] = "zhangsan";
    person.setName(name);
    person.setAge(11);

    cout << person.getIndex() << endl;
    cout << person.getName() << endl;
    cout << person.getAge() << endl;

    return 0;
}
(5)將函數(shù)的實(shí)現(xiàn)放在類外
#include <iostream>
#include <string>

using namespace std;

class Person
{
private:
    /*數(shù)據(jù)成員*/
    int mIndex;
    char mName[25];
    short mAge;

public:
    /*成員函數(shù)*/
    int getIndex();
    void setIndex(int index);
    char* getName();
    void setName(char name[25]);
    short getAge();
    void setAge(short age);
};

int Person::getIndex() {
    return mIndex;
}
void Person::setIndex(int index) {
    mIndex = index;
}
char* Person::getName() {
    return mName;
}
void Person::setName(char name[25]) {
    strcpy_s(mName, strlen(name) + 1, name);
}
short Person::getAge() {
    return mAge;
}
void Person::setAge(short age) {
    mAge = age;
}

int main()
{
    Person person;
    person.setIndex(1);
    char name[25] = "zhangsan";
    person.setName(name);
    person.setAge(11);

    cout << person.getIndex() << endl;
    cout << person.getName() << endl;
    cout << person.getAge() << endl;

    return 0;
}

以上代碼是放在同一個(gè)文件中的,為了簡化代碼,我們需要將類放入 Person.h 中,將函數(shù)的實(shí)現(xiàn)放入 Person.cpp 中。

如果想要使用 Person 類,那么需要添加以下代碼:

#include "Person.h"

下面分別貼出每個(gè)文件中的代碼:

Person.h文件

class Person
{
private:
    /*數(shù)據(jù)成員*/
    int mIndex;
    char mName[25];
    short mAge;

public:
    /*成員函數(shù)*/
    int getIndex();
    void setIndex(int index);
    char* getName();
    void setName(char name[25]);
    short getAge();
    void setAge(short age);
};

Person.cpp文件

#include "Person.h"
#include <string>


int Person::getIndex() {
    return mIndex;
}
void Person::setIndex(int index) {
    mIndex = index;
}
char* Person::getName() {
    return mName;
}
void Person::setName(char name[25]) {
    strcpy_s(mName, strlen(name) + 1, name);
}
short Person::getAge() {
    return mAge;
}
void Person::setAge(short age) {
    mAge = age;
}

main文件

#include <iostream>
#include "Person.h"

using namespace std;

int main()
{
    Person person;
    person.setIndex(1);
    char name[25] = "zhangsan";
    person.setName(name);
    person.setAge(11);

    cout << person.getIndex() << endl;
    cout << person.getName() << endl;
    cout << person.getAge() << endl;

    return 0;
}

打印結(jié)果是:

1
zhangsan
11
(6)對象訪問成員的方式

【方式一】 使用“.”訪問

使用普通對象訪問:

    Person person;
    person.setIndex(1);
    char name[25] = "zhangsan";
    person.setName(name);
    person.setAge(11);

    cout << person.getIndex() << endl;
    cout << person.getName() << endl;
    cout << person.getAge() << endl;

【方式二】 使用“->”引用

使用指針對象訪問:

Person p;
Person* person = &p;
person->setIndex(1);
char name[25] = "zhangsan";
person->setName(name);
person->setAge(11);

cout << person->getIndex() << endl;
cout << person->getName() << endl;
cout << person->getAge() << endl;

[本章完...]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容