C++代码学习之一:组合模式例子

 1 #include"AbstractFile.h" void AbstractFile::add(AbstractFile*) 
 2 { 
 3 } void AbstractFile::remove() 
 4 { 
 5 } void AbstractFile::display() 
 6 { 
 7 } #include"Folder.h" 
 8 folder::folder(string filename) 
 9 { 
10     this->filename=filename; 
11 } void folder::remove() 
12 { 
13 } void folder::add(AbstractFile *p) 
14 { 
15     m_vAbstractfile.push_back(p); 
16 } void folder::display() 
17 { 
18     cout<<"+"<<filename<<endl; 
19     vector<AbstractFile*>::iterator it=m_vAbstractfile.begin(); 
20     for(;it!=m_vAbstractfile.end();++it) 
21     { 
22         (*it)->display(); 
23     } 
24 } 
25  #include"imagefile.h" 
26  void imagefile::add(AbstractFile* p) 
27 { 
28 } 
29 imagefile::imagefile(string filename) 
30 { 
31     this->filename=filename; 
32 } void imagefile::remove() 
33 { 
34 } void imagefile::display() 
35 { 
36     cout<<"图片输出 "<<this->filename<<endl; 
37 } 
38  #include"videofile.h" 
39 videofile::videofile(string filename) 
40 { 
41     this->filename=filename; 
42 } void videofile::add(AbstractFile* p) 
43 { 
44 } void videofile::remove() 
45 { 
46 } void videofile::display() 
47 { 
48     cout<<"影像输出"<<this->filename<<endl; 
49 } 
50  #include<stdio.h> 
51 #include"AbstractFile.h" 
52 #include"Folder.h" 
53 #include"imagefile.h" 
54 #include"videofile.h" int main() 
55 { 
56     AbstractFile *p=new folder("folder"),*p3,*p2,*p4; 
57     folder *p1; 
58     p3=new imagefile("imagefile"); 
59     p2=new videofile("vediofile"); 
60     p4=new imagefile("imagefile2"); 
61     p1=new folder("folder1"); 
62     p->add(p3); 
63     p->add(p2); 
64       
65     p1->add(p4); 
66     p->add(p1); 
67     p->display(); 
68   
69     return 0; 
70 } 

 

转载于:https://www.cnblogs.com/chip/p/3971732.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值