当前位置:网站首页>Common means of modeling: aggregation
Common means of modeling: aggregation
2022-07-02 02:56:00 【Camellia Populus】
polymerization
- explain : Combination and aggregation , No C++ The grammar requirements of , It is a common means in application .
- demand :
Equip the computer with a sound system .
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;
}
VoiceBox.h
#pragma once
class VoiceBox{
public:
VoiceBox();
~VoiceBox();
};
VoiceBox.cpp
#include "VoiceBox.h"
#include <iostream>
VoiceBox::VoiceBox(){
std::cout << " Loudspeaker box Start " << std::endl;
}
VoiceBox::~VoiceBox(){
std::cout << " Loudspeaker box end " << std::endl;
}
Computer.h
#pragma once
#include <string>
#include "Cpu.h"
using namespace std;
// Tell compiler , Yes VoiceBox This class
class VoiceBox;
class Computer{
public:
Computer(string cpuBrand, string cpuModel, int hardDisk = 256, int memory = 8);
~Computer();
void addVoiceBox(VoiceBox* box);
private:
Cpu cpu; //Cpu
int hardDisk; // Hard disk
int memory; // Memory
VoiceBox* box; // Speakers and computers are " polymerization " Relationship
};
Computer.cpp
#include "Computer.h"
#include <iostream>
Computer::Computer(string cpuBrand, string cpuModel,
int hardDisk, int memory) : cpu(cpuBrand, cpuModel)
{
// Use pointer mode
//this->cpu = new Cpu(cpuBrand, cpuModel);
this->hardDisk = hardDisk;
this->memory = memory;
cout << " Computer Start " << endl;
}
Computer::~Computer(){
//delete cpu;
cout << " Computer end " << endl;
}
void Computer::addVoiceBox(VoiceBox* box){
this->box = box;
}
main.cpp
#include "Computer.h"
#include "VoiceBox.h"
#include <Windows.h>
using namespace std;
void test(VoiceBox *box) {
Computer computer(" Intel ", "i9 11900k", 512, 32);
computer.addVoiceBox(box);
}
int main(void) {
VoiceBox box;
test(&box);
system("pause");
return 0;
}

- Aggregation is not a constituent relationship , Contained objects , It may also be contained by other objects .
The owner , You don't have to be responsible for the life cycle of the object you own .
UML The combination in represents :
边栏推荐
- 2022-2028 global nano abrasive industry research and trend analysis report
- [liuyubobobo play with leetcode algorithm interview] [00] Course Overview
- Websocket + spingboot realize code scanning login
- Which brand of sports headset is better? Bluetooth headset suitable for sports
- The video number will not be allowed to be put on the shelves of "0 yuan goods" in the live broadcasting room?
- [punch in questions] integrated daily 5-question sharing (phase II)
- Redis cluster
- Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched
- GB/T-2423.xx 环境试验文件,整理包括了最新的文件里面
- [staff] restore mark (Introduction to the use of restore mark | example analysis of Metaphone mark and restore mark)
猜你喜欢

多线程查询,效率翻倍
![[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)](/img/8e/a86a9724251718d98ce172a6a96e53.png)
[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)

How to turn off the LED light of Rog motherboard

Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched

How to develop digital collections? How to develop your own digital collections

query词权重, 搜索词权重计算

Baohong industry | what misunderstandings should we pay attention to when diversifying investment

2022-2028 global aluminum beverage can coating industry research and trend analysis report

SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding

Coordinatorlayout + tablayout + viewpager2 (there is another recyclerview nested inside), and the sliding conflict of recyclerview is solved
随机推荐
Baohong industry | four basic knowledge necessary for personal finance
Which brand of sports headset is better? Bluetooth headset suitable for sports
PHP notes - use Smarty to set public pages (include, if, else, variable settings)
New programmer magazine | Li Penghui talks about open source cloud native message flow system
使用 useDeferredValue 进行异步渲染
How to turn off the LED light of Rog motherboard
Use the open source project [banner] to achieve the effect of rotating pictures (with dots)
Force deduction daily question 540 A single element in an ordered array
ZABBIX API creates hosts in batches according to the host information in Excel files
MVVM and MVC
[liuyubobobo play with leetcode algorithm interview] [00] Course Overview
JVM面试篇
[staff] pitch representation (bass clef | C1 36 note pitch representation | C2 48 note pitch representation | C3 60 note pitch representation)
The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
JS slow animation
Connected block template and variants (4 questions in total)
GB/T-2423. XX environmental test documents, including the latest documents
Baohong industry | 6 financial management models at different stages of life
Is bone conduction earphone better than traditional earphones? The sound production principle of bone conduction earphones is popular science