跳到正文

个人记录

面对对象编程

shgokdkghsg约 2 分钟
原文

这是 2026年2月25日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年2月25日
更新
2026年2月25日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

模板

C++
#include <iostream>
#include <string>
using namespace std;

// 1. 定义一个类:Student (学生)
class Student {
public: // 访问权限:公开的
    // --- 属性 (成员变量) ---
    string name;
    int age;
    int score;

    // --- 行为 (成员函数) ---
    void introduce(); // 声明:方法原型,实现在类外部

    void study() {
        score++;
        cout << name << "正在努力学习,分数+1,现在分数是:" << score << endl;
    }
};

// 在类外部使用作用域解析符 :: 实现 introduce 方法
void Student::introduce() {
    cout << "大家好,我是" << name << ",今年" << age << "岁。" << endl;
}

int main() {
    // 2. 创建对象 (实例化)
    Student stu1;  // 创建了一个叫 stu1 的对象
    
    // 3. 使用对象 (访问属性和方法)
    stu1.name = "小明";
    stu1.age = 13;
    stu1.score = 90;
    
    stu1.introduce(); // 调用方法
    stu1.study();     // 调用方法
    
    // 创建另一个对象
    Student stu2;
    stu2.name = "小红";
    stu2.introduce(); // 小红和小明互不干扰

    return 0;
}