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();
    }
};

完整代码

cpp 复制代码
#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;
}
相关推荐
weixin_4569042715 天前
数据库设计与UML图
数据库·uml
希赛网18 天前
软考软件设计师常考知识点:(一)计算机组成与体系结构
软考·uml·编程语言·计算机基础·软件设计师
rolt19 天前
[pdf、epub]320道《软件方法》强化自测题业务建模需求分析共279页(202509更新)
产品经理·ddd·架构师·uml·领域驱动设计
攻心的子乐22 天前
软考 UML类图 泛化继承 实现 聚合 组合(最强) 依赖(最弱
uml
攻心的子乐23 天前
软考 UML 用例图 extend扩展关系 include包含关系 泛化继承inherit关系
uml
「QT(C++)开发工程师」1 个月前
UML | 最好的类图设计工具结合CSDN天启呈现-领路架构师
数据库·uml·类视图
小鱼儿LY1 个月前
软考系统架构设计师之UML统一建模语言
系统架构·软考·uml·架构设计师
workflower2 个月前
GitHub宕机自救指南
测试用例·需求分析·uml·敏捷流程·结对编程
青草地溪水旁2 个月前
Astah UML 中,状态机(State Machine)的建模最合适使用「UML 状态图(State Diagram)」
uml·状态机
Liquad Li2 个月前
UML(统一建模语言)详解
架构·uml