聚合
- 说明:组合和聚合,不是C++的语法要求,是应用中的常用手段。
- 需求:
给计算机配一台音响类。
Cpu.h
#pragma once
#include <string>
using namespace std;
class Cpu{
public:
Cpu(string brand = "Inter", string model = "i5");
~Cpu();
private:
string brand; //品牌
string model; //型号
};
Cpu.cpp
#include "Cpu.h"
#include <iostream>
Cpu::Cpu(string brand, string model){
this->brand = brand;
this->model = model;
cout << "Cpu 开始"

本文探讨了聚合这一编程概念,特别是在C++中的应用。聚合不同于组合,它不涉及对象的生命周期管理,是后端开发中常见的一种结构方式。举例展示了如何为计算机模型配置音响组件。
最低0.47元/天 解锁文章
509

被折叠的 条评论
为什么被折叠?



