当前位置: 首页 > article >正文

UML类图示例-CPP

// 发动机类
class Engine {
public:
    void start() {
        std::cout << "Engine started." << std::endl;
    }
};
// 身份证类
class IDCard {
public:
    std::string idNumber;
};
// 抽象类车
class Vehicle {
public:
    virtual void drive() = 0;  // 纯虚函数,使类成为抽象类
};
// 小汽车类,实现车类
class SmallCar : public Vehicle {
private:
    Engine engine;

public:
    void drive() override {
        engine.start();
        std::cout << "Small car is driving." << std::endl;
    }
};
// SUV类,继承小汽车类
class SUV : public SmallCar {
public:
    void drive() override {
        std::cout << "SUV is driving to the mountains." << std::endl;
    }
};
// 自行车类,实现车类
class Bicycle : public Vehicle {
public:
    void drive() override {
        std::cout << "Bicycle is riding." << std::endl;
    }
};
// 班级类
class Class {
private:
    std::vector<Student*> students;  // 聚合学生类

public:
    void addStudent(Student* student) {
        students.push_back(student);
    }
};
// 学生类
class Student {
private:
    IDCard idCard;
    Class* classObj;  // 聚合班级类

public:
    Student(Class* class_) : classObj(class_) {}

    void goToSchool(Bicycle* bicycle) {  // 依赖自行车类
        std::cout << "Student is going to school by bicycle." << std::endl;
        bicycle->drive();
    }
};

完整代码
 

#include <iostream>
#include <vector>

// 发动机类
class Engine {
public:
    void start() {
        std::cout << "Engine started." << std::endl;
    }
};

// 身份证类
class IDCard {
public:
    std::string idNumber;
};

// 抽象类车
class Vehicle {
public:
    virtual void drive() = 0;  // 纯虚函数,使类成为抽象类
};

// 小汽车类,实现车类
class SmallCar : public Vehicle {
private:
    Engine engine;

public:
    void drive() override {
        engine.start();
        std::cout << "Small car is driving." << std::endl;
    }
};

// SUV类,继承小汽车类
class SUV : public SmallCar {
public:
    void drive() override {
        std::cout << "SUV is driving to the mountains." << std::endl;
    }
};

// 自行车类,实现车类
class Bicycle : public Vehicle {
public:
    void drive() override {
        std::cout << "Bicycle is riding." << std::endl;
    }
};

// 班级类
class Class {
private:
    std::vector<Student*> students;  // 聚合学生类

public:
    void addStudent(Student* student) {
        students.push_back(student);
    }
};

// 学生类
class Student {
private:
    IDCard idCard;
    Class* classObj;  // 聚合班级类

public:
    Student(Class* class_) : classObj(class_) {}

    void goToSchool(Bicycle* bicycle) {  // 依赖自行车类
        std::cout << "Student is going to school by bicycle." << std::endl;
        bicycle->drive();
    }
};

int main() {
    // 创建班级对象
    Class schoolClass;
    // 创建学生对象并关联班级
    Student student1(&schoolClass);
    // 创建自行车对象
    Bicycle bicycle;
    // 学生骑自行车上学
    student1.goToSchool(&bicycle);

    // 创建小汽车对象并驾驶
    SmallCar smallCar;
    smallCar.drive();

    // 创建 SUV 对象并驾驶
    SUV suv;
    suv.drive();

    return 0;
}


http://www.kler.cn/a/463528.html

相关文章:

  • C# OpenCV机器视觉:目标跟踪
  • 【数据结构】(Python)差分数组。差分数组与树状数组结合
  • vs2022编译opencv 4.10.0
  • Spring Boot项目启动时显示MySQL连接数已满的错误
  • 1Panel自建RustDesk服务器方案实现Windows远程macOS
  • 2021.12.28基于UDP同信的相关流程
  • Python-Pdf转Markdown
  • sudo mkdir -p /etc/docker其中的 -p 什么意思?
  • ubuntu 如何使用vrf
  • PyTorch快速入门教程【小土堆】之torchvision中的数据集使用
  • 1月第四讲:Java Web学生自习管理系统
  • C++ 基础概念: 未定义行为(Undefined Behavior)
  • 计算机创造的奇迹——C语言
  • GitHub Copilot免费上线!快速上手指南与功能解析
  • rouyi(前后端分离版本)配置
  • 【每日学点鸿蒙知识】动画主动停止、右滑左滑收拾、登录页跳转、Web组件拉起相册、怎么禁止侧滑
  • 快速增加ppt撤回次数的方法
  • 工厂模式与抽象工厂模式在Unity中的实际应用案例
  • mapper文件的解释
  • 【数据结构】数据结构简要介绍