6.19

本文探讨了C++中命名空间的应用,解释了其如何避免命名冲突,并介绍了组合设计模式在文件/目录树构建中的应用。同时,文章通过实例展示了面向对象系统中对象的概念及其属性封装。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1-1 通过命名空间可以区分具有相同名字的函数。 T

1-2 编译预处理指令,是C++语言中不可或缺的重要部分。 T

1-3 using namespace std; 这条语句的作用是将命名空间std内的所有标识符暴露在当前作用域内。T

1-4程序的编译是以文件为单位的,因此将程序分到多个文件中可以减少每次对程序修改所带来的编译工作量。T

2-1 在面向对象系统中,对象是基本的运行时实体,它 _ 。 C
A 只能包括数据(属性)
B 只能包括操作(行为)
C 把属性和行为封装为一个整体
D 必须具有显式定义的对象名

2-2 在面向对象系统中,对象的属性是__。 C
A 对象的行为特性
B 和其他对象相关联的方式
C 和其他对象相互区分的特性
D 与其他对象交互的方式

5-1阅读下列说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。

【说明】现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如下所示

【C++代码】

#include <list>
#include <iostream>
#include <string>
using namespace std; 

class AbstractFile {
protected :	
   string name;  // 文件或目录名称
public:
   void printName(){cout << name;}  // 打印文件或目录名称
   virtual void addChild(AbstractFile  *file)=0;   // 给一个目录增加子目录或文件
   virtual void removeChild(AbstractFile *file)=0;// 删除一个目录的子目录或文件
   virtual list<AbstractFile*> *getChildren()=0;// 获得一个目录的子目录或文件
};
class File : public AbstractFile {
public :

File(string name) { this->name = name; }
void addChild(AbstractFile *file) { return ; }
void removeChild(AbstractFile *file) { return ; }

list<AbstractFile *> * getChildren() { return NULL
; }
};

class Folder :public AbstractFile {
private :
list <AbstractFile*> childList; // 存储子目录或文件
public :
Folder(string name) {
this->name= name; }
void addChild(AbstractFile *file) { childList.push_back(file); }
void removeChild(AbstractFile file) { childList.remove(file);}
list<AbstractFile
> *getChildren() { return
&childList
; }
};

int main( ) {
   // 构造一个树形的文件/目录结构
   AbstractFile *rootFolder = new Folder("c:\\");
   AbstractFile *compositeFolder = new Folder("composite");
   AbstractFile *windowsFolder = new Folder("windows");
   AbstractFile *file = new File("TestComposite.java");
   rootFolder->addChild(compositeFolder);
   rootFolder->addChild(windowsFolder);
   compositeFolder->addChild(file);
   return 0}

5-2阅读程序并填空。

#include <iostream>
#include <cstdlib>
#include <map>
#include <string>

using namespace std;

class employee{
public:
employee(string name,string phoneNumber,string address){
this->name=name;
this->phoneNumber=phoneNumber;
this->address=address;
}
string name;
string phoneNumber;
string address;
};
int main()
{
map<int,employee*> employeeMap;
typedef pair<int,employee*>employeePair;
for(int employIndex=1001;employIndex<=1003;employIndex++){
char temp[10]; //临时存储单元
sprintf(temp,"%d",employIndex);//将转化为字符串存储在temp中
string tmp( temp ); // 通过temp构造string对象
employee* p=new employee
(“employee-”+tmp,“85523927-”+tmp, “address-”+tmp);
employeeMap.insert
(employeePair(employIndex,p));//将员工编号和员工信息插入到employeeMap对象中
}
int employeeNo=0;
cout<<“请输入员工编号:”;
cin>>employeeNo; // 从标准输入获得员工号

map<int,employee>*::iterator it;
it=employeeMap.find(employeeNo); // 根据员工编号查找员工信息
if(it==
employeeMap.end()
){
cout<<“该员工编号不存在!”<<endl;
return -1;
}
cout<<“你所查询的员工编号为:”<first<<endl;
cout<<“该员工姓名:”<second->name<<endl;
cout<<“该员工电话:”<<it->second->phoneNumber<<endl;
cout<<“该员工地址:”<second->address<<endl;
return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wings(hha)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值