题目内容 (请给出正确答案)
[主观题]

●试题五 阅读下列程序说明和C++代码,将应填入(n)处的字句写在答卷的对应栏内。 【说明】 ①在类体

●试题五

阅读下列程序说明和C++代码,将应填入(n)处的字句写在答卷的对应栏内。

【说明】

①在类体中添加函数move(double ax, double ay)的定义,使得点的坐标x和y分别移动ax和ay个单位。

②在类定义外完成重载的两个构造函数CPosition()和CPosition(double dx, double dy),其中前者为不带参数的构造函数,使CPosition对象的默认值为x=0,y=0,后者为带参数的构造函数,把数据成员x和y分别初始化为参数dx和dy的值。

③完成函数double distance(double bx, double by)的定义,该函数返回*this和点(bx,by)的距离。

注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。

源程序文件test5.cpp清单如下:

#include<iostream.h>

#include <math.h>

class CPosition

{

public:

CPosition();

CPosition(double dx, double dy);

double getx();

double gety();

(1)

double distance(double bx, double by);

private:

double x;

double y;

};

(2)

{

x=0; y=0;

}

CPosition::CPosition(double dx, double dy)

{

x=dx; y=dy;

}

double CPosition::getx()

{

return x;

}

double CPosition::gety()

{

return y;

}

double CPosition::distance(double bx, double by)

{

(3)

}

void main()

{

double a,b;

cout << "Input x, y position of a point: ";

cin >> a >> b;

CPosition psA(a, b);

cout << "Input x, y position of another point: ";

cin >> a >> b;

cout << "The distance is " << psA.distance(a,b) <<endl;

}

查看答案
如搜索结果不匹配,请 联系老师 获取答案
您可能会需要:
您的账号:,可能会需要:
您的账号:
发送账号密码至手机
发送
更多“●试题五 阅读下列程序说明和C++代码,将应填入(n)处的字…”相关的问题

第1题

●试题五 阅读下列程序说明和C++代码,将应填入(n)处的字句写在答卷的对应栏内。 【说明】 ①定义类

●试题五

阅读下列程序说明和C++代码,将应填入(n)处的字句写在答卷的对应栏内。

【说明】

①定义类Table的私有数据成员x和y,分别用于表示九九表中的两个乘数(x*y),它们都是int型的数据。

②完成类Table的成员函数print()的定义,该函数以"x*y=z"的格式打印出九九表中的一个乘法算式,请使用格式化输出函数printf实现。

③完成类Table9的成员函数print()的定义,该函数调用基类Table的print()函数,将九九表输出到屏幕。

④补充主函数,在屏幕上输出九九表,以便测试所定义的类的正确性。

源程序文件test8_3.cpp清单如下:

#include<iostream.h>

#include<stdio.h>

class Table

{

(1)

int z;

public:

void print(int x,int y,int z);

};

void Table::print (int x,int y,int z)

{

(2)

}

class Table9:public Table

{

public:

void print();

};

void Table9::print()

{

(3)

int x,y,z;

for (i=1;i<10;i++)

{

for(j=1;j<i+1;j++)

{

x=i;

y=j;

z=i*j;

Table::print(y,x,z);

}

printf("\n");

}

}

main()

{

//**4**

return 0;

}

点击查看答案

第2题

试题五(共15分)阅读下列说明和C++-代码,将应填入 (n) 处的字句写在答题纸的对应栏内。【说明】某发

试题五(共15分)

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

【说明】

某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图5-1所示的类图。

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

【C++代码】

include <iostream>

using namespace std;

class invoice{

public:

(1) {

cout《 "This is the content of the invoice!"《 endl;

}

};

class Decorator : public invoice {

Invoice *ticket;

public:

Decorator(lnvoice *t) { ticket = t; }

void printinvoice(){

if(ticket != NULL)

(2);

}

};

class HeadDecorator : public Decorator{

public:

HeadDecorator(lnvoice*t): Decorator(t) { }

void printinvoice0 {

cout《 "This is the header of the invoice! "<< endl;

(3) ;

}

};

class FootDecorator : public Decorator{

public:

FootDecorator(invoice *t): Decorator(t) { }

void printlnvoice() {

(4) ;

cout《 "This is the footnote of the invoice!"《 endl;

}

};

int main(void) {

Invoice t;

FootDecorator f(&t);

HeadDecorator h(&f);

H.printlnvoice();

cout< < “_____”< < endl;

FootDecorator a(NULL);

HeadDecorator b((5) );

B.printinvoice();

return 0;

}

程序的输出结果为:

This is the header of the invoice!

This is the content of the invoice!

This is the footnote of the invoice!

----------------------------

This is the header of the invoice!

This is the footnote of the invoice!

点击查看答案

第3题

试题五(共15分)阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】现欲开发

试题五(共15分)

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

【说明】

现欲开发一个软件系统,要求能够同时支持多种不同的数据库,为此采用抽象工厂模式设计该系统。以SQL Server和Access两种数据库以及系统中的数据库表Department为例,其类图如图5-1所示。

试题五(共15分)阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】现欲

【C++代码】

include <iostream>

using namespace std;

class Department{/*代码省略*/};

class IDepartment{

public:

(1) =0;

(2) =0;

};

class SqlserverDepartment:(3){

public:

void Insert(Department* department){

cout <<"Insert a record into Department in SQL Server!\n";

∥其余代码省略

}

Department GetDepartment(int id){

/*代码省略*/

}

};

class AccessDepartment: (4) {

public:

void Insert(Department* department){

cout <<"Insert a record into Department in ACCESS!\n";

∥其余代码省略

}

Department GetDepartment(int id){

/*代码省略*/

}

};

(5){

public:

(6)=0;

};

class SqlServerFactory:public IFactory{

public:

IDepartment*CreateDepartment(){ return new SqlserverDepartment(); }

∥其余代码省略

};

class AccessFactory:public IFactory{

public:

IDepartment* CreateDepartment(){ return new AccessDepartment();}

∥其余代码省略

};

点击查看答案

第4题

试题五(共15分)阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】某咖啡店

试题五(共15分)

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

【说明】

某咖啡店当卖咖啡时,可以根据顾客的要求在其中加入各种配料,咖啡店会根据所加入的配料来计算费用。咖啡店所供应的咖啡及配料的种类和价格如下表所示。

试题五(共15分)阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】某咖

【C++代码】

include <iostream>

include <string>

using namespace std;

const int ESPRESSO_PRICE = 25;

const int DRAKROAST_PRICE = 20;

const int MOCHA_PRICE = 10;

const int WHIP_PRICE = 8;

class Beverage { //饮料

(1) :string description;

public:

(2) (){ return description; }

(3) ;

};

class CondimentDecorator : public Beverage { //配料

protected:

(4) ;

};

class Espresso : public Beverage { //蒸馏咖啡

public:

Espresso () {description="Espresso"; }

int cost(){return ESPRESSO_PRICE; }

};

class DarkRoast : public Beverage { //深度烘焙咖啡

public:

DarkRoast(){ description = "DardRoast"; }

int cost(){ return DRAKROAST_PRICE; }

};

class Mocha : public CondimentDecorator { //摩卡

public:

Mocha(Beverage*beverage){ this->beverage=beverage; }

string getDescription(){ return beverage->getDescription()+",Mocha"; }

int cost(){ return MOCHA_PRICE+beverage->cost(); }

};

class Whip :public CondimentDecorator { //奶泡

public:

Whip(Beverage*beverage) { this->beverage=beverage; }

string getDescription() {return beverage->getDescription()+",Whip"; }

int cost() { return WHIP_PRICE+beverage->cost(); }

};

int main() {

Beverage* beverage = new DarkRoast();

beverage=new Mocha((5) );

beverage=new Whip((6) );

cout<<beverage->getDescription()<<"¥"<<beverage->cost() endl;

return 0;

}

编译运行上述程序,其输出结果为:

DarkRoast, Mocha, Whip ¥38

点击查看答案

第5题

请教:2016年计算机二级C++基础练习三简答题9如何解答?

使用VC6打开考生文件夹下的源程序文件2.cpp。阅读下列函数说明和代码,补充空出的代码。函数sum(intn)返回1,2,3,…,n的和。其中n大于0。

程序要求使用递归实现上述功能。

注意:不能修改程序的其他部分,只能补充sum函数。

试题程序:

#include

#include

intsum(intn)

{

}

voidmain()

{

cout<<"1+2+3+…+100="<

endl;

return;

}

点击查看答案

第6题

试题五(共15分) 阅读以下说明和 C++代码,将应填入 (n) 处的语句或语句成分写在答题纸的对应栏内。

试题五(共15分)

阅读以下说明和 C++代码,将应填入 (n) 处的语句或语句成分写在答题纸的对应栏内。

【说明】

某数据文件students.txt的内容为100名学生的学号和成绩,下面的程序将文件中的数据全部读入对象数组,按分数从高到低进行排序后选出排名前 30%的学生。

【C++代码】

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

class Student {

private:

string sNO; //学号

int credit; //分数

public:

Student(string a,int b) { sNO = a; credit = b;}

Student(){}

int getCredit();

void out();

};

(1) ::getCredit() {

return credit;

}

(2) ::out() {

cout << "SNO: " << sNO << ", Credit=" << credit << endl;

}

class SortStudent {

public:

void sort(Student *s, int n);

SortStudent(){}

};

void SortStudent::sort(Student *s,int n) {

for(int i = 0; i < n-1; i++) {

for(int j = i+1; j < n; j++) {

if(s[i]. (3) < s[j]. (4) ) {

Student temp = s[i]; s[i] = s[j]; s[j] = temp;

}

}

}

}

int main(int argc, char* argv[])

{

const int number = 100; //学生总数

ifstream students;

students.open("students.txt");

if(!students.is_open()) {

throw 0;

}

Student *testStudent = (5) [number];

int k = 0;

string s;

while (getline(students,s,'\n')) { //每次读取一个学生的学号和成绩

Student student(s.substr(0,s.find(',')), atoi(s.substr(s.find(',')+1).c_str()));

testStudent[k++] = student;

}

students.close();

(6) ;

ss.sort(testStudent,k);

cout <<"top 30%: "<<endl;

for(k = 0; k < number * 0.3; k++) {

testStudent[k].out();

}

delete []testStudent;

return 0;

}

点击查看答案

第7题

试题五(共15分)阅读以下说明和C++代码,填充代码中的空缺,将解答填入答题纸的对应栏内。【说明】下面

试题五(共15分)

阅读以下说明和C++代码,填充代码中的空缺,将解答填入答题纸的对应栏内。

【说明】

下面的程序用来计算并寻找平面坐标系中给定点中最近的点对(若存在多对,则输出其中的一对即可)。程序运行时,先输入点的个数和一组互异的点的坐标,通过计算每对点之间的距离,从而确定出距离最近的点对。例如,在图5-1所示的8个点中,点(1,1)与(2,0.5)是间距最近的点对。

试题五(共15分)阅读以下说明和C++代码,填充代码中的空缺,将解答填入答题纸的对应栏内。【说明】下

【C++代码】

include <iostream>

include <cmath>

using namespace std;

class GPoint {

private:

double x, y;

public:

void setX(double x) { this->x = x; }

void setY(double y) { this->y = y; }

double getX() { return this->x; }

double getY() { return this->y; }

};

class ComputeDistance {

public:

double distance(GPoint a,GPoint b) {

return sqrt《a.getX() - b.getX())*(a.getX() - b.getX())

+ (a.getY() - b.getY())*(a.getY() - b.getY()));

}

};

int main()

{

int i,j, numberOfPoints=0;

cout<<"输入点的个数:";

cin>>numberOfPoints;

(1) points= neW GPoint[numberOfPoints];//创建保存点坐标的数组

memset(points,0,sizeof(points));

cout <<"输入"<< numberOfPoints<<"个点的坐标:";

for(i=0;i<numberOfPoints; i++){

double tmpx, tmpy;

cin>>tmpx>>tmpy;

points[i].setX(tmpx);

points[i].setY(tmpy);

}

(2) computeDistance= new ComputeDistance();

int p1=0,p2=1;//p1和p2用于表示距离最近的点对在数组中的下标

double shortestDistance= computeDistance->distance(points[p1], points[p2]);

//计算每一对点之间的距离

for(i=0;i<numberOfPoints; i++){

for(j=i+1;j< (3) ;j++){

double tmpDistance=computeDistance-> (4) ;

if ((5) ) {

p1=i; p2 =j;

shortestDistance= tmpDistance;

}

}

}

cout<<"距离最近的点对是:(";

cout"points[p1].getX()<<","<<points[pl].getY()<<")和(";

cout<<points[p2].getX()<<","<<points[p2].getY()<<")"<<endl;

delete computeDistance;

return 0:

}

点击查看答案

第8题

试题五(共 15 分) 阅读下列说明、图和C++代码,回答问题1 至问题3,将解答写在答题纸的对应栏内。 [

试题五(共 15 分)

阅读下列说明、图和C++代码,回答问题1 至问题3,将解答写在答题纸的对应栏内。

[说明]

已知四个类之间的关系如图 5-1 所示,分别对每个类的方法进行编号,例如 Shape的 perimeter()方法为 1 号,表示为“1:perimeter()” ,Rectangle 类的 perimeter()为2号,表示为“2:perimeter()” ,依此类推,其中,每个类的 perimeter方法都为虚函数且方法签名相同。

试题五(共 15 分) 阅读下列说明、图和C++代码,回答问题1 至问题3,将解答写在答题纸的对应栏

[C++代码]

Triangle *tr = new Triangle();

Square *sq = new Square();

Shape *sh = tr;

[问题 1] 关于上述 C++代码中 sh 和 tr 的以下叙述中,哪两个是正确的(写出编号) 。

① sh 和 tr 分别引用同一个对象;

② sh 和 tr 分别引用同一类型的不同的对象;

③ sh 和 tr 分别引用不同类型的不同对象;

④ sh 和 tr 分别引用同一个对象的不同拷贝;

⑤ sh 和 tr 所引用的内存空间是相同的。

[问题 2] 写出下面消息对应的方法编号(如果该消息错误或者没有对应的方法调用,请

填写“无” ) 。

tr->height() (1)

sh->perimeter() (2)

sq->height() (3)

sq->perimeter() (4)

sh->height() (5)

tr->perimeter() (6)

[问题 3] 不考虑内存释放问题,下列赋值语句中哪两个是合法的(写出合法赋值语句的

编号) 。

① sq = sh; ② sh = tr; ③ tr = sq; ④ sq = tr; ⑤ sh = sq;

点击查看答案

第9题

从下列2道试题(试题五至试题六)中任选 1道解答。如果解答的试题数超过1道,则题号小的1道解答有效。

试题五(共15分)

阅读以下说明、图和C++代码,填补C++代码中的空缺(1)~(5),将解答写在答题纸的对应栏内。

【说明】

已知对某几何图形绘制工具进行类建模的结果如图5.1所示,其中Shape为抽象类(应至少包含一个纯虚拟(virtual)函数),表示通用图形,Box表示矩形,Ellipse表示椭圆,Circle表示圆(即特殊的椭圆),Line表示线条。

从下列2道试题(试题五至试题六)中任选 1道解答。如果解答的试题数超过1道,则题号小的1道解答有效。

下面的C++代码用于实现图5-1所给出的设计思路,将其空缺处填充完整并编译运行,输

出结果为:

Ellipse

Circle

Ellipse

C

E

【C++代码】

include <string>

include <iostream>

using namespace std;

class Shape{

public:

Shape(const string& name){

m_name= name;

}

~Shape(){}

(1) void paint() = 0;

stringgetName()const {

retumm name;

}

Private:

string m_name;

};

//Box和 Line类的定义与 Ellipse类似,其代码略

classEllipse (2) {

public:

Ellipse(const string& name) : Shape(name){ cout<<"Ellipse" <<endl; }

Voidpaint() { cout<<getName()<<endl;}

};

classCircle (3) {

public:

Circle(const string& name) : Ellipse(name){ cout<<"Circl"<<endl; }

};

class Diagram {

public:

void drawAShap(Shape* shape){ shape->paint(); }

void drawShapes() {

shapes[0] = new Circle("C");

shapes[l] = new Ellipse("E");

for (int i=O;i<2; ++1) {

drawAShap(shapes[i]);

}

}

void close (){ /*删除形状,代码略 */ }

private:

Shape* shapes[2];

};

int main()

{

Diagram* diagram = (4)

diagram->drawShapes();

diagram->close ();

(5) diagram;

}

点击查看答案

第10题

试题五(共15分) 阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】 某大型

试题五(共15分)

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

【说明】

某大型商场内安装了多个简易的纸巾售卖机,自动出售2元钱一包的纸巾,且每次仅售出一包纸巾。纸巾售卖机的状态图如图5-1所示。

试题五(共15分) 阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】

采用状态(State)模式来实现该纸巾售卖机,得到如图5-2所示的类图。其中类State为抽象类,定义了投币、退币、出纸巾等方法接口。类SoldState、SoldOutState、NoQuarterState和HasQuarterState分别对应图5-1中纸巾售卖机的4种状态:售出纸巾、纸巾售完、没有投币、有2元钱。

试题五(共15分) 阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】

【C++代码】

include<iostream>

using namespace std;

∥以下为类的定义部分

class TissueMachine; //类的提前引用

class State{

public:

virtual void insertQuarter()=0; //投币

virtual void ejectQuarter()=0; //退币

virtual void turnCrank()=0; //按下“出纸巾”按钮

virtual void dispense()=0; //出纸巾

};

/*类SoldOutState. NoQuarterState. HasQuarterState. SoldState的定义省略,每个类中均定义了私有数据成员TissueMachine* tissueMachine;*/

class TissueMachine{

private:

(1) *soldOutState, *noQuarterState, *hasQuarterState,*soldState, *state;

int count, //纸巾数

public:

TissueMachine(int numbers);

void setState(State* state);

State* getHasQuarterState();

State* getNoQuarterState();

State* getSoldState();

State* getSoldOutState();

int getCount();

//其余代码省略

};

//以下为类的实现部分

void NoQuarterState::insertQuarter(){

tissueMachine->setState((2) );

}

void HasQuarterState::ejectQuarter(){

tissueMachine->setState((3) );

}

void SoldState::dispense(){

if(tissueMachine->getCount()>0){

tissueMachine->setState ((4) );

}

else{

tissueMachine->setState((5) );

}

}//其余代码省略

点击查看答案
发送账号至手机
密码将被重置
获取验证码
发送
温馨提示
该问题答案仅针对搜题卡用户开放,请点击购买搜题卡。
马上购买搜题卡
我已购买搜题卡, 登录账号 继续查看答案
重置密码
确认修改
温馨提示
每个试题只能免费做一次,如需多次做题,请购买搜题卡
立即购买
稍后再说
警告:系统检测到您的账号存在安全风险

为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!

微信搜一搜
赏学吧
点击打开微信
警告:系统检测到您的账号存在安全风险
抱歉,您的账号因涉嫌违反赏学吧购买须知被冻结。您可在“赏学吧”微信公众号中的“官网服务”-“账号解封申请”申请解封,或联系客服
微信搜一搜
赏学吧
点击打开微信