国家二级C++机试(操作题)模拟试卷486
基本操作题
1.请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,此工程包含一个源程序文件proj1.cpp。文件中将表示数组元素个数的常量Size定义为4,并用int类型对类模板进行了实例化。文件中位于每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
1 2 3 4
注意:模板参数名用T。只修改注释“//ERROR*******found*******”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include
using namespace std;
//将数组元素个数Size定义为4
//ERROR *******found*******
const int Size;
template
class MyClass
{
public:
MyClass(T*P)
{
for(int i=0;i<Size;i++)
array[i]=p[i];
}
void Print();
private:
T array[Size];
};
template
//ERROR *******found*******
void MyClass::Print()
{
for(int i=0;i<Size;i++)
cout<<array[i]<<’\t’;
}
int main()
{
int intArray[Size]={1,2,3,4};
//ERROR*******found*******
MyClass obj (intArray);
obj.Print();
cout<<endl;
return 0;
}
(1)const int Size=4;
(2)void MyClass::Print()
(3)MyClassobj(intArray);
解析:(1)主要考查考生对const变量的掌握,因为const变量不能修改,所以在定义的同时必须初始化。
(2)主要考查考生对模板类的成员函数定义的掌握,因为MyClass类是模板类,所以在定义该函数时要加上模板标识符“”,即语句void MyClass::Print()。
(3)主要考查考生对模板类构造函数的调用的理解,从上一条语句int intArray[Size]={1,2,3,4};中可以知道intArray为int型,因此定义obj时要使用,即MyClassobj(intArray);。
简单应用题
2.请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中含有一个源程序文件proj2.cpp,其中定义了CharShape类、Triangle类和Rectangle类。
CharShape是一个抽象基类,它表示由字符组成的图形(简称字符图形),纯虚函数Show用于显示不同字符图形的相同操作接口。Triangle和Rectangle是CharShape的派生类,它们分别用于表示字符三角形和字符矩形,并且都定义了成员函数Show,用于实现各自的显示操作。程序的正确输出结果应为:
*
***
*****
*******
########
########
########
请阅读程序,分析输出结果,然后根据以下要求在横线处填写适当的代码并删除横线。
(1)将Triangle类的成员函数Show补充完整,使字符三角形的显示符合输出结果。
(2)将Rectangle类的成员函数Show补充完整,使字符矩形的显示符合输出结果。
(3)为类外函数fun添加合适的形参。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
//proj2.cpp
#include
using namespace std;
class CharShape{
public:
CharShape(char ch):ch(ch){);
virtual void Show()=0;
protected:
char ch;//组成图形的字符
};
class Triangle:public CharShape{
public:
Triangle(char ch,int r):
CharShape(ch), rows(r){}
void Show();
private:
int rows;//行数
};
class Rectangle:public Char-Shape{
public:
Rectangle(char ch,int r,
int c):CharShape(ch),_rows
(r), cols(c){)
void Show();
private:
int rows t cols;//行数和列数
};
void Triangle::Show()
//输出字符组成的三角形
{
for(int i=1;i<=rows;i++) {
//**********found**********
for(int j=1;j<=__________;j++)
cout<< _ch;
cout<<endl;
}
}
void Rectangle::Show()
//输出字符组成的矩形
{
//**********found**********
for(int i=1;i<=_________;i++){
//**********found**********
for(int j =1;j <=_________;j++)
cout<< ch;
cout<<endl;
}
}
//**********found**********为fun函数添加形参
void fun(_________){cs.Show
(); }
int main()
{
Triangle tri(’*’,4);
Rectangle rect(’#’,3,8);
本文档预览:3600字符,共6588字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载