首页 > 编程语言 > 详细

C++设计模式之装饰者模式

时间:2014-03-30 15:42:33      阅读:614      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
#include "HandCake.h"
//手抓饼

HandCake::HandCake()
{
    this->price=10;
    this->name="手抓饼";
}

HandCake::~HandCake(void)
{
}
int HandCake::GetPrice()
{
    return 10;

}
string HandCake::GetName()
{

    return name;
}
bubuko.com,布布扣
bubuko.com,布布扣
#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;

}
bubuko.com,布布扣
bubuko.com,布布扣
// 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;
}
bubuko.com,布布扣

运行结果:

bubuko.com,布布扣

C++设计模式之装饰者模式,布布扣,bubuko.com

C++设计模式之装饰者模式

原文:http://www.cnblogs.com/shencheng5721/p/3633586.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!