Develop - Cpp - 类、变量

Posted on Sep 16, 2024
  • 1.5 类 在头文件person.h中声明类
#ifndef PERSON_H // 防止头文件重复包含
#define PERSON_H
#include <string>
class Person {
public:
    // 构造函数,参数列表包括名字和年龄
    Person(const std::string& name, int age);

    // 获取名字
    std::string getName() const;

    // 获取年龄
    int getAge() const;

    // 设置名字
    void setName(const std::string& name);

    // 设置年龄
    void setAge(int age);

private:
    std::string name;
    int age;
};

#endif // PERSON_H
成员函数即定义在类/方法中的函数。

变量和基本类型

  • 2.1 基本内置类型 2.1.1 算数类型 2.1.2 类型转换 2.1.3 字面值常量
  • 2.2 变量 如果是内置类型的变量未被显式初始化,它的值由定义的位置决定。定义于任何函数体之外的变量被初始化为0,定义在函数体内部的内置类型变量将不被初始化,访问它会出现错误。
    • 声明变量
    extern int i;
    

`