C++<第十四篇>:类

C++是面向对象语言,每一个对象都可以用类来体现,使用 class 关键字可以声明一个类。

(1)类的一般形式
class 类名
{
    数据成员
}
(2)类的声明和定义
class Person
{
    /*数据成员*/
    int mIndex;
    char mName[25];
    short mAge;

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

class 是定义类的关键字;
Person 是类名;
以及函数体内的 数据成员成员函数

(3)成员函数的实现

什么叫成员函数的声明?

int getIndex();

以上代码直接使用分号结束,没有大括号,这样叫做函数的声明。

什么叫成员函数的实现?

int getIndex(){}

像这样,含有大括号的函数叫函数的实现。

(4)将函数的实现放在类内
#include <iostream>
#include <string>

using namespace std;

class Person
{
private:
    /*数据成员*/
    int mIndex;
    char mName[25];
    short mAge;

public:
    /*成员函数*/
    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;
}

类中的代码可能比较多,我们将类相关代码放入 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)将函数的实现放在类外
#include <iostream>
#include <string>

using namespace std;

class Person
{
private:
    /*数据成员*/
    int mIndex;
    char mName[25];
    short mAge;

public:
    /*成员函数*/
    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;
}

以上代码是放在同一个文件中的,为了简化代码,我们需要将类放入 Person.h 中,将函数的实现放入 Person.cpp 中。

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

#include "Person.h"

下面分别贴出每个文件中的代码:

Person.h文件

class Person
{
private:
    /*数据成员*/
    int mIndex;
    char mName[25];
    short mAge;

public:
    /*成员函数*/
    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;
}

打印结果是:

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;

[本章完...]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容