题目
阅读以下说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。
说明
通常情况下,用户可以对应用系统进行配置,并将配置信息保存在配置文件中。应用系统在启动时首先将配置文件加载到内存中,这些内存配置信息应该有且仅有一份。下面的代码应用了单身模式(Singleton)以保证Configure类只能有一个实例。这样, Configure类的使用者无法定义该类的多个实例,否则会产生编译错误。
C++代码
#include<iostream.h>
class Configure{
(1):
Configure(){} //构造函数
public:
static Configure*Instance();
public:
int GetConf~ureData(){return data;} //获取配置信息
int SetConfigureDate(int m_data)
{data=m_data; return data;} //设置配置信息
private:
static Configure*_instance;
int data; //配置信息
};
(2)=NULL;
Configure*Configure∷Instance(){
if(_instance=NULL){
_instance=(3);
//加载配置文件并设置内存配置信息,此处省略
}
return (4);
}
void main()(
Configure*t=NULL;
t=(5);
int d=t→GetConfigureData();
//获取配置信息后进行其他工作,此处省略
}
第1题
() 阅读下列说明和C++代码,将应填入空(n)处的字句写在答题纸的对应栏内。【说明】 某中学开展中外中学生野外生存夏令营活动,由于中外学生的语言障碍,随队为外籍学员配置一名翻译。以下代码采用适配器(Adapter)模式模拟翻译适配器。其类图如下:
第2题
阅读以下说明和C++代码,将应填入(n)处的字句写在对应栏内。
【说明】
C++标准模板库中提供了vector模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空间为std。vector模板类的部分方法说明如下表所示:
【C++代码】
include <iostream>
include <vector>
using namespace (1);
typedef vector< (2) > INTVECTOR;
const int ARRAY_SIZE = 6;
void ShowVector (INTVECTOR &theVector);
int main() {
INTVECTOR theVector;
// 初始化 theVector, 将theVector的元素依次设置为0至5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++}
theVector.push_back((3));
ShowVector(theVector); // 依次输出theVector中的元素
theVector.erase (theVector.begin () + 3};
ShowVector(theVector);
}
void ShowVector (INTVECTOR &theVector) {
if (theVector.empty ()) {
cout << "theVector is empty." << endl; return;
}
INTVECTOR::iterator (4);
for (theIterator=theVector.begin(); theIterator !=theVector.end(); theIterator++) {
cout << *theIterator;
if (theIterator != theVector.end()-1) cout << ", ";
}
cout << end1;
}
该程序运行后的输出结果为:
0,1,2,3,4,5
(5)
第3题
阅读以下说明,以及用C++在开发过程中所编写的程序代码,将应填入(n)处的字句写在对应栏内。
【说明】
在下面函数横线处填上适当的字句,使其输出结果为:
构造函数.
构造函数.
1,2
5,6
析构函数
析构函数.
【C++代码】
include "iostream.h"
class AA
{ public;
AA(int i,int j)
{A=i; B=j;
cout<<"构造函数.\n";
}
~AA(){(1);}
void print();
private:
int A, B;
};
void AA∷print()
{cout<<A<<","<<B<<endl;}
void main()
{
AA *a1, *a2;
(2)=new AA(1, 2);
a2=new AA(5, 6);
(3);
a2->print();
(4) a1;
(5) a2;
}
第4题
试题六(共 15 分)
阅读以下说明和 C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
C++标准模板库中提供了 vector 模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空间为 std。vector模板类的部分方法说明如下表所示:
[C++代码]
include <iostream>
include <vector>
using namespace (1) ;
typedef vector< (2) > INTVECTOR;
const int ARRAY_SIZE = 6;
void ShowVector(INTVECTOR &theVector);
int main(){
INTVECTOR theVector;
// 初始化 theVector,将 theVector的元素依次设置为 0 至 5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)
theVector.push_back((3) );
ShowVector(theVector); // 依次输出 theVector中的元素
theVector.erase(theVector.begin() + 3);
ShowVector(theVector);
}
void ShowVector(INTVECTOR &theVector) {
if (theVector.empty()) {
cout << "theVector is empty." << endl; return;
}
INTVECTOR::iterator (4) ;
for (theIterator = theVector.begin(); theIterator != theVector.end(); theIterator++){
cout << *theIterator;
if (theIterator != theVector.end()-1) cout << ", ";
}
cout << endl;
}
该程序运行后的输出结果为:
0, 1, 2, 3, 4, 5
(5)
第5题
阅读下列说明和c++代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都
有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌
灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。Command模式
的类图如图5-1所示。
【c++代码】
}
第6题
阅读以下说明和C++代码,将应填入(n)处的字句写在对应栏内。
【说明】
以下C++程序的功能是计算三角形、矩形和正方形的面积并输出。程序由4个类组成:类 Triangle、Rectangle和Square分别表示三角形、矩形和正方形:抽象类Figure提供了一个纯虚函数getAxea(),作为计算上述3种图形面积的通用接口。
【C++代码】
include<iostream>
include<cmath>
using namespace std;
class Figure{
public:
virtual double getArea()=0;//纯虚函数
};
class Rectangle : (1) {
protected:
double height;
double width;
public:
Rectangle(){}
Rectangle(double height, double width){
this->height=height;
this->width=width;
}
double getArea(){
return (2);
}
};
class Square: (3) {
public:
Square(double width){
(4);
}
};
class Triangle: (5) {
private:
double la,lb,lc;
public:
Triangle(double la,double lb,double lc){
this->la=la;this->1b=1b;this->lc=lc;
}
double getArea(){
double s=(la+lb+lc)/2.0;
return sqrt(s*(s-la)*(s-lb)*(s-lc));
}
int main()
{
Figure *figures[3]={new Triangle(2,3,3),new Rectangle(5,8), new Square(5)};
for(int i=0;i<3;i++){
cout<<"figures["<<i<<"]area="<<(figures[i])->getArea()<<endl;
}
return 0;
}
第7题
阅读以下说明C++代码,将应填入(n)处的字句写在对应栏内。
[说明]
以下程序的功能是实现堆栈的一些基本操作。堆栈类stack共有三个成员函数:empty判断堆栈是否为空;push进行人栈操作;pop进行出栈操作。
[C++程序]
include "stdafx. h"
include <iostream, h>
eonst int maxsize = 6;
class stack {
float data[ maxsize];
int top;
public:
stuck(void);
~ stack(void);
bool empty(void);
void push(float a);
float pop(void);
};
stack: :stack(void)
{ top =0;
cout < < "stack initialized." < < endl;
}
stack:: ~stack(void) {
cout < <" stack destoryed." < < endl;
bool stack:: empty (void) {
return (1);
void stack: :push(float a)
if(top= =maxsize) {
cout < < "Stack is full!" < < endl;
return;
data[top] =a;
(2);
}
float stack:: pop (void)
{ if((3)){
cout< < "Stack is undcrflow !" < < endl;
return 0;
(4);
return (5);
}
void main()
{ stack s;
coat < < "now push the data:";
for(inti=l;i< =maxsize;i+ +) {
cout< <i< <" ";
s. push(i);
}
coat < < endl;
cout< < "now pop the data:";
for(i = 1 ;i < = maxsize ;i + + )
cout< <s. pop()< <" ";
}
第8题
阅读以下说明和C++程序代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
在下面的C++代码中,类SalesTicket能够完成打印票据正文的功能,类HeadDec- orator与FootDecorator分别能够完成打印票据的台头和脚注的功能。
已知该程序运行后的输出结果如下所示,请填补该程序代码中的空缺。
这是票据的台头!
这是票据正文!
这是票据的脚注!
---------------
这是票据的台头!
这是票据的脚注!
【C++程序代码】
#include
using namespace std;
class SalesTicket {
public:
(1) printTicket() { cout << " 是票据正文!" << endl;}
class Decorator : public SalesTicket{
SalesTicket *ticket;
public:
Decorator(SalesTicket *t){ ticket = t; }
void printTicket(){
if(ticket != NULL)
ticket->printTicket();
}
};
class HeadDecorator : public Decorator{
public:
HeadDecorator(SalesTicket *t): (2) { }
void printTicket() {
sour << "这是票据的台头!" << endl;
Decorator::printTicket();
}
};
class FootDecorator :public Decorator{
public:
FootDecorator(SalesTicket *t): (3)
void printTicket() {
Decorator::printTicket();
cout << "这是票据的脚注!" << endl;
}
};
void main(void) {
SalesTicket t;
FootDecorator f(&t);
HeadDecorator h((4) );
h.printTicket();
cout << "-------------------------"<< endl;
FootDecorator a(NULL);
HeadDecorator b((5) );
b.printTicket();
}
第9题
阅读以下说明和C++码,将相应的文字填入(n)处,写在对应栏内。
[说明]
以下代码实现了对象引用作为函数参数,仔细阅读以下代码,分析运行结果,填入(n)处。
[代码]
include<iostream.h>
class Sample
{
int x,y;
public:
Sample() {x=y=0; }
Sample (int i, int j ) {x=i; y=j; }
void copy (Sample &s ).,
void setxy (int i, int j ) {x=i; y=j; }
void print ({cout<<"x="<<x<<end1 ; cout <<"y="<<y<<end1;
};
void Sample: copy (Sample &s )
{
X=S.X;
y=s.y;
}
void func (Sample s1, Sample &s2 )
{
s1.setxy (10,20 );
s2.setxy (30,40 );
}
void main ()
{
Sample p (1,2 ) ,q;
q.copy (p );
time (p,q );
p.print ();
q.print ();
}
运行结果
(1)
(2)
(3)
(4)
第10题
阅读下列函数说明和C++代码,将应填入(n)处的字句写在对应栏内。
[说明]
在销售系统中常常需要打印销售票据,有时需要在一般的票据基础上打印脚注。这样就需要动态地添加一些额外的职责。如下展示了Decorator(修饰)模式。SalesOrder对象使用一个SalesTicket对象打印销售票据,先打印销售票据内容,然后再打印脚注。图5-1显示了各个类间的关系。以下是C++语言实现,能够正确编译通过。
[图5-1]
[C++代码]
class Component{
public:
(1) void prtTicket()=0;
};
class SalesTicket:public Component{
public:
void prtTicket(){
cout<<"Sales Ticket!"<<endl;
}
};
class Decorator:public Component{
public:
virtual void prtTicket();
Decorator(Component *myC);
private:
(2) myComp;
};
Decorator::Decorator(Component *myC)
{
myComp=myC;
}
void Decorator::prtTicket()
{
myComp->prtTicket();
}
class Footer:public Decorator{
public:
Footer(Component *myC);
void prtTicket();
void prtFooter();
};
Footer::Footer(Component *myC): (3) {}
void Footer::prtFooter()
{
cout<<"Footer"<<endl;
}
void Footer::prtTicket()
{
(4) ;
prtFooter();
}
class SalesOrder{
public:
void prtTicket();
};
void SalesOrder::prtTicket()
{
Component *myST;
myST=new Footer((5) );
myST->prtTicket();
}
(1)
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!