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;
}
相关推荐
自传.7 天前
B 站更新|软考架构师案例篇・软件工程篇第 2-4 集上线|UML 用例图|类图|顺序图真题精讲
架构·系统架构·软考高级·uml·案例分析·用例图·软考系统架构师
码匠许师傅13 天前
【设计模式精讲】29.行为型模式总结与对比(Behavioral Patterns Summary)
c++·设计模式·uml
码匠许师傅14 天前
【设计模式精讲】27.模板方法模式(Template Method)
c++·设计模式·uml·模板方法模式
码匠许师傅15 天前
【设计模式精讲】26.策略模式(Strategy)
c++·设计模式·策略模式·uml
码匠许师傅15 天前
【设计模式精讲】25.状态模式(State)
c++·ui·设计模式·状态模式·uml
rolt15 天前
用UML表示的行业标准02汽车-AUTOSAR
软件工程·ddd·uml·领域驱动设计·ontology·本体
东莞市永盛电气16 天前
CNC数控机床北美供电适配新实践:208V转380V变压器落地应用案例
uml
码匠许师傅16 天前
【设计模式精讲】24.观察者模式(Observer)
c++·观察者模式·设计模式·uml
码匠许师傅17 天前
【设计模式精讲】22.中介者模式(Mediator)
c++·设计模式·软件工程·uml·中介者模式
码匠许师傅17 天前
【设计模式精讲】23.备忘录模式(Memento)
c++·设计模式·软件工程·uml·备忘录模式