#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; }
运行结果: