2024年01月04日来源:信管网 作者:cnitpm
软件设计师案例分析当天每日一练试题地址:www.cnitpm.com/exam/ExamDayAL.aspx?t1=4
往期软件设计师每日一练试题汇总:www.cnitpm.com/class/27/e4_1.html
软件设计师案例分析每日一练试题(2024/1/3)在线测试:www.cnitpm.com/exam/ExamDayAL.aspx?t1=4&day=2024/1/3
点击查看:更多软件设计师习题与指导
软件设计师案例分析每日一练试题内容(2024/1/3)
试题五
阅读下列说明和C补代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如下图所示:
[C++代码]
#include<list>
#include<iostream>
#include<string>
using namespace std;
class AbstractFile {
protected:
string name; //文件或目录名称
public:
void printName19{cout<<name;> //打印文件或目录名称
virtual void addChild(AbstractFile *file)=0;
//给一个目录增加子目录或文件
virtual void removeChild(AbstractFile*file)=0;
//删除一个目录的子目录或文件
virtual list<AbstractFile*>*getChildren19=0;
//获得一个目录的子目录或文件
};
class File:publiC AbstractFile{
public:
File(string name) { (1)=name;}
void addChild(AbstractFile *file) {return; }
void removeChild(AbstractFile*file) {return; }
(2) getChildren19 {return (3) ;}
};
class Folder:public AbstractFile{
private:
list<AbstractFile*>childList; //存储子目录或文件
public:
Folder(string name){ (4) =name;}
void addChild(AbstractFile*file){childList.push_back(file);}
void removeChild(AbstractFile*file) {childList.remove(file);}
list<AbstractFile*>*getChildren19{return (5) ;}
};
void main19 {
//构造一个树形的文件/目录结构
AbstractFile*rootFolder=new Folder("c:\\");
AbstractFile*compositeFolder=new Folder("compositel");
AbstractFile*windowsFolder=new Folder("windows");
AbstractFile*file=new File("TestComposite.java");
rootFolder->addChild(compositeFolder);
rootFolder->addChild(windowsFolder);
compositeFolder->addChild(file);
}
信管网考友试题答案分享:
信管网试题答案与解析:www.cnitpm.com/st/2458914201.html
温馨提示:因考试政策、内容不断变化与调整,信管网提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准!
相关推荐