当前位置:网站首页>Common means of modeling: combination
Common means of modeling: combination
2022-07-02 06:13:00 【Camellia Populus】
Combine
explain : Combination and aggregation , No C++ The grammar requirements of , It is a common means in application .
demand :
Build a computer class , A computer , from CPU chip , Hard disk , Memory, etc .
CPU Chips also use classes to represent .
CPU.h
#pragma once
#include <string>
using namespace std;
class Cpu{
public:
Cpu(string brand = "Inter", string model = "i5");
~Cpu();
private:
string brand; // brand
string model; // model
};
CPU.cpp
#include "Cpu.h"
#include <iostream>
Cpu::Cpu(string brand, string model){
this->brand = brand;
this->model = model;
cout << "Cpu Start " << endl;
}
Cpu::~Cpu(){
cout << "Cpu end " << endl;
}
Computer.h
#pragma once
#include <string>
#include <iostream>
#include <Windows.h>
#include "Cpu.h"
using namespace std;
class Computer{
public:
Computer(string cpuBrand, string cpuModel, int hardDisk, int memory);
~Computer();
private:
Cpu cpu; //Cpu Classes and Computer Between classes , Namely " Combine "
//Cpu *cpu; // Pointer mode
int hardDisk; // Hard disk
int memory; // Memory
};
Computer.cpp
#include "Computer.h"
//cpu The parameters of are initialized with a list
Computer::Computer(string cpuBrand, string cpuModel,
int hardDisk, int memory) : cpu(cpuBrand, cpuModel)
{
// Initialize with pointer
//this->cpu = new Cpu(cpuBrand, cpuModel);
this->hardDisk = hardDisk;
this->memory = memory;
cout << " Computer Start " << endl;
}
Computer::~Computer(){
// Pointer free memory
//delete cpu;
cout << " Computer end " << endl;
}
main.cpp
#include "Computer.h"
using namespace std;
void test() {
Computer computer(" Intel ", "i9 11900k", 512, 32);
}
int main(void) {
// When test At the end of the function
// Computers and Cpu Will be completely destroyed
test();
system("pause");
return 0;
}

Summary :
The object of being owned (cpu chip ) Its life cycle and its owner ( Computer ) The life cycle of is consistent .
When the computer is created ,cpu The chip is also created .
When the computer is destroyed ,cpu The chip is also destroyed .
The owner needs to be responsible to the owner , It's a strong relationship , It's the relationship between the whole and the part .
Specific combination mode :
1) The combined object directly uses the member object .( Commonly used )
2) Use pointers to represent the combined objects , In the constructor , Create combined objects ; In destructors , Release the combined objects .
UML The combination in represents :
Note that the inclusion uses a solid diamond
【 Add 】UML drawing tools :starUML
边栏推荐
- Redis key value database [primary]
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
- 经典文献阅读之--SuMa++
- Sumo tutorial Hello World
- STC8H8K系列匯編和C51實戰——數碼管顯示ADC、按鍵串口回複按鍵號與ADC數值
- Mathematical statistics and machine learning
- Verifying downloaded files using sha256 files
- Error creating bean with name 'instanceoperatorclientimpl' defined in URL when Nacos starts
- 神机百炼3.52-Prim
- Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
猜你喜欢

LeetCode 90. 子集 II

LeetCode 78. subset

Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')

TI毫米波雷达学习(一)

51单片机——ADC讲解(A/D转换、D/A转换)

Monitoring uplink of VRRP

Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)

网络相关知识(硬件工程师)

【C语言】简单实现扫雷游戏

Flutter 混合开发: 开发一个简单的快速启动框架 | 开发者说·DTalk
随机推荐
51单片机——ADC讲解(A/D转换、D/A转换)
Generic classes and parameterized classes of SystemVerilog
LeetCode 40. 组合总和 II
LeetCode 77. combination
Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
如何使用MITMPROXy
servlet的web.xml配置详解(3.0)
Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)
亚马逊aws数据湖工作之坑1
MUI底部导航的样式修改
Scheme and implementation of automatic renewal of token expiration
LeetCode 39. Combined sum
Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
Detailed explanation of BGP message
Sumo tutorial Hello World
经典文献阅读之--Deformable DETR
Servlet web XML configuration details (3.0)
LeetCode 90. 子集 II
从设计交付到开发,轻松畅快高效率!
步骤详解 | 助您轻松提交 Google Play 数据安全表单