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;
}