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;
}
相关推荐
青木川崎2 小时前
UML统一建模
uml
aiden:)10 小时前
UML 状态图:陪伴机器人系统示例
机器人·软件工程·软件构建·uml
aiden:)14 小时前
手机状态:UML 状态图(State Diagram)的解析与绘画
软件工程·uml
aiden:)16 小时前
UML 状态图:解锁电子图书馆管理系统的高效设计
架构·软件工程·软件构建·uml
却道天凉_好个秋16 小时前
系统架构设计(一):UML与软件架构
系统架构·uml
aiden:)16 小时前
UML 状态图:以网络媒体教学系统为例解析
软件工程·uml
光头颜5 天前
UML之序列图的消息
软件工程·uml
未定义.2217 天前
UML-饮料自助销售系统(无法找零)序列图
设计模式·流程图·状态模式·软件工程·需求分析·uml
王伯爵7 天前
Unified Modeling Language,统一建模语言
uml
cooldream20097 天前
深入解析UML图:版本演变、静态图与动态图详解
uml·系统架构师