建模的常用手段:组合和聚合 - 理想主义者光芒万丈
source link: https://www.cnblogs.com/ylww/p/16703536.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
说明:组合和聚合,不是c++的语法要求,而是应用中的常用手段。
组合
构建一个计算机类,一台计算机,由CPU芯片,硬盘,内存等组成。
CPU芯片也使用类来表示。
运行结果:
这是会调用析构函数的。
关于第二种组合模式: “组合Computer.h”文件中类中数据改写成 Cpu *cpu;,并将头文件改写成声明——>“组合Computer.cpp”文件中,在构造函数中
删去初始化列表部分,且在此函数中加入 this->cpu = new Cpu(cpuBrand, cpuVersion);——>“组合Computer.cpp”文件中,在
析构函数中delete cpu;。
聚合
给计算机配一台音响。
运行结果:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK