当前位置:网站首页>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 :
边栏推荐
- OSPF LSA message parsing (under update)
- What kind of good and cost-effective Bluetooth sports headset to buy
- [staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
- ZABBIX API creates hosts in batches according to the host information in Excel files
- Use usedeferredvalue for asynchronous rendering
- What is the difference between an intermediate human resource manager and an intermediate economist (human resources direction)?
- Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
- How to run oddish successfully from 0?
- Websocket + spingboot realize code scanning login
- Possible causes of runtime error
猜你喜欢
Face++ realizes face detection in the way of flow
![[learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table](/img/f4/298f64c4b4f8674eda4e8fb19a976a.png)
[learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table

A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high

2022-2028 global deep sea generator controller industry research and trend analysis report

ZABBIX API creates hosts in batches according to the host information in Excel files

Soul app released the annual report on generation Z behavior: nearly 20% of young people love shopping in the vegetable market

Mongodb base de données non relationnelle

Redis cluster

Formatting logic of SAP ui5 currency amount display

2022-2028 global encryption software industry research and trend analysis report
随机推荐
[JVM] detailed description of the process of creating objects
2022-2028 global wood vacuum coating machine industry research and trend analysis report
Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
Questions d'entrevue
What are the characteristics of common web proxy IP
JVM面试篇
2022-2028 global soft capsule manufacturing machine industry research and trend analysis report
QT实现界面跳转
设置状态栏颜色
高并发场景下缓存处理方案
LFM signal denoising, time-frequency analysis, filtering
Stdref and stdcref
Jvm-01 (phased learning)
Share the basic knowledge of a common Hongmeng application
What kind of good and cost-effective Bluetooth sports headset to buy
Ten minutes will take you in-depth understanding of multithreading - multithreaded teamwork: synchronous control
Leetcode question brushing (10) - sequential question brushing 46 to 50
SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
Redis cluster
Après le mariage