当前位置:网站首页>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
边栏推荐
- Lambda 表达式 和 方法引用
- LeetCode 40. Combined sum II
- Mock simulate the background return data with mockjs
- 外部中断无法进入,删代码再还原就好......记录这个想不到的bug
- 格式校验js
- 从设计交付到开发,轻松畅快高效率!
- keepalived安装使用与快速入门
- Jetpack Compose 与 Material You 常见问题解答
- No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
- LeetCode 83. Delete duplicate elements in the sorting linked list
猜你喜欢

51 single chip microcomputer - ADC explanation (a/d conversion, d/a conversion)

Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes

穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!

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

Ros2 --- lifecycle node summary

uni-app开发中遇到的问题(持续更新)

State machine in BGP

步骤详解 | 助您轻松提交 Google Play 数据安全表单

数据回放伴侣Rviz+plotjuggler

Classic literature reading -- deformable Detr
随机推荐
从设计交付到开发,轻松畅快高效率!
Step by step | help you easily submit Google play data security form
LeetCode 77. combination
LeetCode 90. 子集 II
Database learning summary 5
Redis key value database [primary]
Error creating bean with name 'instanceoperatorclientimpl' defined in URL when Nacos starts
LeetCode 47. Full arrangement II
【C语言】筛选法求素数
神机百炼3.54-染色法判定二分图
Detailed steps of JS foreground parsing of complex JSON data "case: I"
[C language] simple implementation of mine sweeping game
Monitoring uplink of VRRP
LeetCode 40. 组合总和 II
深入了解JUC并发(二)并发理论
Redis Key-Value数据库 【秒杀】
Introduce uview into uni app
VRRP之监视上行链路
Picture clipping plug-in cropper js
ROS create workspace