当前位置:网站首页>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 :
边栏推荐
- Force deduction daily question 540 A single element in an ordered array
- Pat a-1165 block reversing (25 points)
- Redis cluster
- [liuyubobobo play with leetcode algorithm interview] [00] Course Overview
- 实现一个自定义布局的扫码功能
- [staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the
- [Chongqing Guangdong education] Sichuan University concise university chemistry · material structure part introductory reference materials
- Baohong industry | 6 financial management models at different stages of life
- Stdref and stdcref
- LeetCode刷题(十)——顺序刷题46至50
猜你喜欢

2022-2028 global encryption software industry research and trend analysis report

Systemserver service and servicemanager service analysis

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

AcWing 245. Can you answer these questions (line segment tree)
![[reading notes] programmer training manual - practical learning is the most effective (project driven)](/img/13/28116a74512895ad725dffed02f8bd.png)
[reading notes] programmer training manual - practical learning is the most effective (project driven)
![[pit] how to understand](/img/e9/f5315a03b6f3da07021f915bb18af8.jpg)
[pit] how to understand "parameter fishing"

Xiaomi, a young engineer, was just going to make soy sauce

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

Actual battle of financial risk control - under Feature Engineering

MVVM and MVC
随机推荐
Which brand of running headphones is good? How many professional running headphones are recommended
Possible causes of runtime error
PMP personal sprint preparation experience
Provincial election + noi Part IV graph theory
Connected block template and variants (4 questions in total)
Formatting logic of SAP ui5 currency amount display
Yyds dry goods inventory accelerating vacuum in PG
How to create an instance of the control defined in SAP ui5 XML view at runtime?
[punch in questions] integrated daily 5-question sharing (phase II)
What is the principle of bone conduction earphones and who is suitable for bone conduction earphones
[learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table
ZABBIX API creates hosts in batches according to the host information in Excel files
[road of system analyst] collection of wrong topics in enterprise informatization chapter
CoordinatorLayout + TabLayout + ViewPager2(里面再嵌套一个RecyclerView),RecyclerView的滑动冲突解决
离婚3年以发现尚未分割的共同财产,还可以要么
C write TXT file
Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
Batch detect whether there is CDN in URL - high accuracy
Delphi xe10.4 installing alphacontrols15.12
STM32__05—PWM控制直流电机