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;
}
相关推荐
toolhow3 天前
二、《重学设计模式》-UML类图
设计模式·uml
rolt6 天前
基于角色访问控制的UML 表示04
产品经理·架构师·uml
rolt6 天前
基于角色访问控制的UML 表示02
产品经理·架构师·uml
好奇的菜鸟14 天前
理解UML中的四种关系:依赖、关联、泛化和实现
uml
qq_429856571 个月前
UML-组件图
uml
rolt1 个月前
电梯系统的UML文档07
设计模式·产品经理·架构师·uml
rolt1 个月前
电梯系统的UML文档05
产品经理·架构师·uml
rolt1 个月前
电梯系统的UML文档04
产品经理·架构师·uml
小沈同学呀1 个月前
Java UML 类图绘制解析:结构与工具类型详解
java·开发语言·uml·plantuml
梳子烟YAN1 个月前
UML系列之Rational Rose笔记七:状态图
笔记·uml