zoukankan      html  css  js  c++  java
  • C++设计模式之装饰者模式

    #include "HandCake.h"
    //手抓饼
    
    HandCake::HandCake()
    {
        this->price=10;
        this->name="手抓饼";
    }
    
    HandCake::~HandCake(void)
    {
    }
    int HandCake::GetPrice()
    {
        return 10;
    
    }
    string HandCake::GetName()
    {
    
        return name;
    }
    #include "Ham.h"
    
    
    Ham::Ham(void)
    {
    }
    
    Ham::Ham(HandCake *cake)
    {
        
        //this->cake=cake;
        int p=cake->price;
        this->name=cake->name+"加火腿";
        this->price=cake->price+1;
    }
    
    Ham::~Ham(void)
    {
    }
    string Ham::GetName()
    {
    
        return this->name;
    }
    int Ham::GetPrice()
    {
        return this->price;
    
    }
    // Decoration.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "HandCake.h"
    #include "Ham.h"
    #include <iostream>
    #include "vld.h"
    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
    {
        HandCake *cake=new HandCake();
        Ham *HamHandCake1=new Ham(cake);
        std::cout<<HamHandCake1->GetName()<<HamHandCake1->GetPrice()<<""<<endl;
        Ham *HamHandCake2=new Ham(HamHandCake1);
        std::cout<<HamHandCake2->GetName()<<HamHandCake2->GetPrice()<<""<<endl;
            
        getchar();
        delete cake;
        delete HamHandCake1;
        delete HamHandCake2;
        return 0;
    }

    运行结果:

  • 相关阅读:
    数据结构的入门
    Google 插件
    树莓派的第一次
    MySQL下载与安装
    SVN图标不显示问题
    excel 批量生成SQL语句
    版本管理工具
    RSA加密、解密、签名、验签的原理及方法
    获取客户端内网IP
    eclipse 添加svn插件
  • 原文地址:https://www.cnblogs.com/shencheng5721/p/3633586.html
Copyright © 2011-2022 走看看