题目
阅读下列程序说明和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(doub,e dx,doub,e dy)
{
x=dx; y=dy;
}
double CPosition::getx()
{
return x;
}
double CPosition::gety()
{
return y;
}
double CPosition::distance(double bx,double by)
{
(3)
}
vold main()
{
double a,b;
cout<<"|nput 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) <<end1;
}
第1题
阅读下列说明、c++代码和运行结果,填补代码中的空缺(1)~(6),将解答填入
答题纸的对应栏内。
【说明】
很多依托扑克牌进行的游戏都要先洗牌。下面的c++程序运行时先生成一副扑克牌,
洗牌后再按顺序打印每张牌的点数和花色。
【c++代码】
inciude <iostream>
4Finclude <stdlib. h>
include <ctime>
inciude <aigorithm>
include <string>
Using namespace std
Const string Rank[13]={”A”,”2”,”3”,”4“,“5”,”6,”’“7”8“,9”,”10,”J”,
”Q”,”K”}j//扑克牌点数
第2题
() 阅读下列说明和C++代码,将应填入空(n)处的字句写在答题纸的对应栏内。【说明】 某中学开展中外中学生野外生存夏令营活动,由于中外学生的语言障碍,随队为外籍学员配置一名翻译。以下代码采用适配器(Adapter)模式模拟翻译适配器。其类图如下:
第3题
阅读下列说明和c++代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都
有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌
灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。Command模式
的类图如图5-1所示。
【c++代码】
}
第4题
阅读以下说明和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)
第5题
试题六(共 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)
第6题
●试题五
阅读下列程序说明和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;
}
第7题
阅读下列说明和 C++代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 某快餐厅主要制作井出售儿童套餐,一般包括主餐(各类比萨)、饮料和玩具,其餐品种类可能不同,但其制作过程相同。前台服务员(Waiter)调度厨师制作套餐。现采用生成器(Builder) 模式实现制作过程,得到如图 5-1 所示的类图。图5-1 类图 【C++代码】 include<iostream> include <string> using namespace std; class Pizza { private: string parts; public: void setParts(string parts) { this->parts=parts; } string getParts() { return parts; } }; class PizzaBuilder { protected:Pizza* pizza; public: Pizza* getPizza() { retum pizza; } void createNewPizza() { pizza = new Pizza(); } (1 ); } class HawaiianPizzaBuilder :public PizzaBuilder { public: void buildParts() { pizza->setParts("cross +mild + ham&pineapple"); } }; class SpicyPizzaBuider: public PizzaBuilder { public: void buildParts() { pizza->setParts("pan baked +hot + ham&pineapple"); } } Class Waiter{ Private: PizzaBuilder* pizzaBuilder; public: void setPizzaBuilder(PizzaBuilder* pizzaBuilder) { /*设置构建器*/ (2 ) } Pizza* getPizza() { return pizzaBuilder->getPizza(); } void construct() { /*构建*/ pizzaBuilder->createNewPizza(); (3 ) } }; int main(){ Waiter*waiter=new Waiter(); PizzaBuilder*hawaiian pizzabuilder=new HawaiianPizzaBuilder() (4 ); (5 ); cout<< "pizza: "<< waiter->getPizza()->getParts()<< endl; } 程序的输出结果为: pizza: cross + mild + ham&pineapple
第8题
阅读下列程序说明和C++代码,将应填入(n)处的字句写在对应栏内。
[说明]
①为类Circle增加一个构造函数,该函数有一个参数,并在构造时将该参数值赋给成员 radius。将该函数实现为一个非内联函数,并且使用参数列表的方式将类成员赋值。
②为类Circle增加一个成员函数print(),使得可以输出有关圆的信息,比如下列程序
Circle c;
c. SetRadius(5);
c. Print();
将输出:The circle has radius of 5!
③完成友元函数void CompareR(Circle *c1,Circle *c2)的定义,在屏幕中输出c1与c2比较radius大小结果,要求使用if - else结构完成。
输出结果如下:
The circle has radus of 5 !
The circle has radius of 10 !
cl <c2
源程序文件test7_3, cpp 清单如下:
include < iostream, h >
class Circle {
public:
Circle() :radius(5) {}
(1)
void SetRadius(int r) { radius = r; }
int GetRadius() { return radius; }
(2)
friend void CompareR(Circle * c1,Circle * c2);
private:
int radius;
};
void CompareR(Circle * c! ,Circle * c2)
{
(3)
cout << "c1 > c2" << endl;
else
if ((c1 -> GetRadius()) == (c2 -> GetRadius()))
tout < <"c1=c2' < < endl;
else
if ((c1 -> GetRadius()) < (c2 -> GetRadius()))
cout <<"c1<c2" <<endl;
void main()
Circle c1
c1. SetRadius(5)
c1. Print()
Circle c2(10);
c2. Print()
CompareR(&c1 ,&c2);
}
第9题
阅读以下说明和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;
}
第10题
阅读以下说明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()< <" ";
}
第11题
阅读下列说明和C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 ‘ 以下C++代码实现一个简单的聊天室系统(ChatRoomSystem),多个用户(User)可以向聊天室(ChatRoom)发送消息,聊天室将消息展示给所有用户。类图如图6-1所表示。图6-1 类图
【C++代码】 include<iostream> include <string> using namespace std; class User { private: string name; public: User(string name){ (1) =name; } ~User(){} void setName(string name) { this->name=name; } string getName(){ return name; } void sendMessage(string message); }; class ChatRoom { . public: static void showMessage(User* user, string message) { cout<<"["<<user->getName()<<"] : "<<message<<endl; } }; void User::sendMessage(string message) { (2) (this,message); } class ChatRoomSystem{ public: . . void startup() { User* zhang = new User(“John"); User* li = new User("Leo"); zhang->sendMessage("Hi! Leo!"); li_>sendMessage("Hi! John!"); } void join(User* user) { (3) ("HeIIo Everyone! l am"+user->getName()); } . }; int main(){ ChatRoomSystem*crs= (4) ; crs->startup(); crs->join((5) ("Wayne")); delete crs; } /* 程序运行结果: [John]:Hi! Leo! [Leo]:Hi! John! [Wayne]:Hello Everyone! I am Wayne /*
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!