当前位置:网站首页>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
边栏推荐
- Spark overview
- Style modification of Mui bottom navigation
- Generic classes and parameterized classes of SystemVerilog
- 步骤详解 | 助您轻松提交 Google Play 数据安全表单
- 来自读者们的 I/O 观后感|有奖征集获奖名单
- I/o multiplexing & event driven yyds dry inventory
- 神机百炼3.53-Kruskal
- Data playback partner rviz+plotjuggler
- 深入了解JUC并发(一)什么是JUC
- Google Play Academy 组队 PK 赛,正式开赛!
猜你喜欢
随机推荐
VRRP之监视上行链路
LeetCode 40. Combined sum II
Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table
From design delivery to development, easy and efficient!
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
在uni-app中引入uView
LeetCode 83. 删除排序链表中的重复元素
LeetCode 78. subset
Shenji Bailian 3.54-dichotomy of dyeing judgment
Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
Let every developer use machine learning technology
Scheme and implementation of automatic renewal of token expiration
Eco express micro engine system has supported one click deployment to cloud hosting
加密压缩文件解密技巧
深入了解JUC并发(一)什么是JUC
I/o multiplexing & event driven yyds dry inventory
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
LeetCode 39. 组合总和
【C语言】简单实现扫雷游戏