当前位置:网站首页>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 :
边栏推荐
- Delphi xe10.4 installing alphacontrols15.12
- tarjan2
- [question 008: what is UV in unity?]
- 实现一个自定义布局的扫码功能
- 2022安全员-C证考试题及模拟考试
- 【JVM】创建对象的流程详解
- QT uses sqllite
- 【无标题】
- [reading notes] programmer training manual - practical learning is the most effective (project driven)
- Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
猜你喜欢
Baohong industry | 6 financial management models at different stages of life
After marriage
结婚后
A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high
Redis cluster
[JVM] detailed description of the process of creating objects
AcWing 245. Can you answer these questions (line segment tree)
创业了...
Query word weight, search word weight calculation
2022-2028 global nano abrasive industry research and trend analysis report
随机推荐
STM32__ 05 - PWM controlled DC motor
实现一个自定义布局的扫码功能
Set status bar color
Basic 01: print string
The basic steps of using information theory to deal with scientific problems are
Share the basic knowledge of a common Hongmeng application
Leetcode question brushing (10) - sequential question brushing 46 to 50
[JSON] gson use and step on the pit
2022 safety officer-c certificate examination questions and mock examination
使用 useDeferredValue 进行异步渲染
Mmsegmentation series training and reasoning their own data set (3)
Questions d'entrevue
Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched
【做题打卡】集成每日5题分享(第二期)
只需简单几步 - 开始玩耍微信小程序
JVM面试篇
超图iServer rest服务之feature查询
C return multiple values getter setter queries the database and adds the list return value to the window
设置状态栏颜色
What are the common proxy servers and what are the differences?