当前位置: 首页 > news >正文

惠州酒店网站建设专做负面的网站

惠州酒店网站建设,专做负面的网站,公司网站建设方案所需素材,知更鸟wordpress 怎样目录 【设计模式专题之代理模式】7-小明买房子【设计模式专题装饰模式】8-咖啡加糖 【设计模式专题之代理模式】7-小明买房子 文章链接:卡码网设计模式 题目链接:7-小明买房子 这里注意在HomeAgent类里需要定义对象成员变量HomeBuyer,这里在实…

目录

  • 【设计模式专题之代理模式】7-小明买房子
  • 【设计模式专题装饰模式】8-咖啡加糖

【设计模式专题之代理模式】7-小明买房子

文章链接:卡码网设计模式
题目链接:7-小明买房子

这里注意在HomeAgent类里需要定义对象成员变量HomeBuyer,这里在实例化HomeAgent类时就会自动在内部实例化一个HomeBuyer类;若要定义一个指针变量HomeBuyer*,则需要写构造函数对HomeBuyer指针进行初始化,斗则在实例化HomeAgent类后,HomeBuyer会变成一个空指针,导致越界问题。

#include<iostream>
#include<bits/stdc++.h>using namespace std;class HomePurchase{
public:    virtual void homepurchase(int area) = 0;  
};class HomeBuyer : public HomePurchase{
public:void homepurchase(int area) override{cout << "YES" << endl;}
};class HomeAgent : public HomePurchase{
private:HomeBuyer homebuyer;public://HomeAgent(HomeBuyer* homebuyer): homebuyer(homebuyer){}void homepurchase(int area) override{if (area > 100) homebuyer.homepurchase(area);else cout << "NO" << endl;}
};int main(){int N;cin >> N;//HomeBuyer* buyerori = new HomeBuyer();HomePurchase* buyer = new HomeAgent();for (int i = 0; i < N; i++){int area;cin >> area;buyer->homepurchase(area);}delete buyer;//delete buyerori;return 0;
}

【设计模式专题装饰模式】8-咖啡加糖

文章链接:卡码网设计模式
题目链接:8-咖啡加糖

#include <iostream>
#include <memory>// 咖啡接口
class Coffee {
public:virtual ~Coffee() {}virtual void brew() = 0;
};// 具体的黑咖啡类
class BlackCoffee : public Coffee {
public:void brew() override {std::cout << "Brewing Black Coffee" << std::endl;}
};// 具体的拿铁类
class Latte : public Coffee {
public:void brew() override {std::cout << "Brewing Latte" << std::endl;}
};// 装饰者抽象类
class Decorator : public Coffee {
protected:std::unique_ptr<Coffee> coffee;public:Decorator(std::unique_ptr<Coffee> coffee) : coffee(std::move(coffee)) {}void brew() override {if (coffee) {coffee->brew();}}
};// 具体的牛奶装饰者类
class MilkDecorator : public Decorator {
public:MilkDecorator(std::unique_ptr<Coffee> coffee) : Decorator(std::move(coffee)) {}void brew() override {Decorator::brew();std::cout << "Adding Milk" << std::endl;}
};// 具体的糖装饰者类
class SugarDecorator : public Decorator {
public:SugarDecorator(std::unique_ptr<Coffee> coffee) : Decorator(std::move(coffee)) {}void brew() override {Decorator::brew();std::cout << "Adding Sugar" << std::endl;}
};// 客户端代码
int main() {int coffeeType, condimentType;while (std::cin >> coffeeType >> condimentType) {// 根据输入制作咖啡std::unique_ptr<Coffee> coffee;if (coffeeType == 1) {coffee = std::make_unique<BlackCoffee>();} else if (coffeeType == 2) {coffee = std::make_unique<Latte>();} else {std::cout << "Invalid coffee type" << std::endl;continue;}// 根据输入添加调料if (condimentType == 1) {coffee = std::make_unique<MilkDecorator>(std::move(coffee));} else if (condimentType == 2) {coffee = std::make_unique<SugarDecorator>(std::move(coffee));} else {std::cout << "Invalid condiment type" << std::endl;continue;}// 输出制作过程coffee->brew();}return 0;
}

这是示例代码,使用了智能指针unique_ptr;在C++中,std::unique_ptr 是一种智能指针,提供了自动内存管理的能力,确保在不再需要指针时,关联的对象被正确地释放。

std::unique_ptr<Coffee> coffee;

这行代码声明了一个名为 coffee 的智能指针(std::unique_ptr),它指向 Coffee 类型的对象。

Decorator(std::unique_ptr<Coffee> coffee) : coffee(std::move(coffee)) {}

这是 Decorator 类的构造函数的实现。这个构造函数采用一个 std::unique_ptr 类型的参数,然后使用 std::move 将传递进来的智能指针移动到成员变量 coffee 中。
std::move(coffee) 使用 std::move 函数将传递进来的智能指针的所有权转移到 Decorator 类的成员变量 coffee。这是因为在构造函数结束后,参数 coffee 将会超出作用域,如果我们想在 Decorator 对象的生命周期内继续使用这个指针,就需要使用 std::move。
通过这种方式,Decorator 类就可以使用传递进来的 std::unique_ptr 对象,而且由于 std::unique_ptr 具有独占所有权,它还负责在 Decorator 对象生命周期结束时正确地销毁关联的 Coffee 对象。

SugarDecorator(std::unique_ptr<Coffee> coffee) : Decorator(std::move(coffee)) {}

这是 SugarDecorator 类的构造函数的实现。构造函数采用一个 std::unique_ptr 类型的参数,然后通过 std::move 将传递进来的智能指针移动到 Decorator 类的构造函数中。

:Decorator(std::move(coffee)) 是成员初始化列表,用于调用 Decorator 类的构造函数,将 std::unique_ptr 移动到 Decorator 类的 coffee 成员变量中。

这意味着 SugarDecorator 在构造时会调用其基类 Decorator 的构造函数,将传递进来的 coffee 对象的所有权交给 Decorator。

coffee = std::make_unique<BlackCoffee>();

这行代码使用了C++标准库中的std::make_unique函数来动态创建一个BlackCoffee对象,并将其所有权赋给名为coffee的std::unique_ptr。

coffee = std::make_unique<MilkDecorator>(std::move(coffee));

这行代码使用 std::make_unique 创建一个新的 MilkDecorator 对象,并将先前创建的 coffee 对象的所有权通过 std::move 移动到 MilkDecorator 的构造函数中。

设计模式第五天,对智能指针的使用还不是很熟悉,操作系统看完整了,接下来先不刷设计模式了,先看排序,C++新特性和内存,然后数据库。加油!!!

http://www.yayakq.cn/news/115124/

相关文章:

  • 想开个网站怎么做软件项目管理方法
  • 做网站的公司哪家深圳建网站哪个济南兴田德润有活动吗
  • php构建网站如何开始做视频网站要多大的带宽
  • 网站建设课程设计报告范文室内设计专业个人简历
  • 网站设计的主要内容下列软件中属于网页制作工具
  • 免费生成图片的网站优秀网页设计代码
  • 如何做网站优化推广wordpress嵌入视频
  • 温州市手机网站制作番禺网站建设技术
  • 河南国基建设集团有限公司网站网站建设实训 课程标准
  • 红河网站制作苏州公司官网制作
  • 百度网站服务器宁德市医院东侨院区
  • 佛山厂家关键词网络推广seo排名优化有哪些
  • 电脑网站开发手机上可以打开吗北京东城区做网站的公司
  • 深圳外贸网站制作公司wordpress 免费 主题 下载
  • 喀什建设网站官方网站的作用
  • 商城维护工作内容网站建设网站维护收费
  • 网站做线上销售营销策划好的网站
  • 任丘网站建设公司丹阳官方网站建站
  • 长沙网站整站优化高端建站设计
  • 技术支持 网站建设网站建设咨询公司排名
  • 如何分析网站开发语言wordpress注册邮件接收不到
  • 西安网站建设现状新安网站开发
  • 茂名高端模板建站网站搭建平台选哪个
  • 展示型网站模板源码工作室起名大全免费取名
  • 企业宣传网站怎么做5000元做百度推广效果怎么样
  • 蚌埠网站建设专业公司网站开发课程报告心得
  • 可以做免费的网站吗莱芜百姓网
  • 网站短链接怎么做网页版微信怎么登陆
  • 忘了网站链接怎么做网站建设方案模板范文
  • 金耀网站建设阿里云iis放网站