国家二级C++机试(操作题)模拟试卷481
基本操作题
1.请使用VC6或使用【答题】菜单打开考生文件夹progl下的工程progl。此工程中包含程序文件main.cpp,其中有类Score(“成绩”)和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
学号:12345678课程:英语总评成绩:85
注意:只修改每个“//ERROR****found****”下的一行,不要改动程序中的其他内容。
#include
using namespace std;
class Score{
public:
Score(const char *the_course,const char * the_id,int the_normal,int the_midterm,int the_end of term)
:course(the course),normal(the_normal),midterm(the midterm),end_of_term(the end_of_term){
//ERROR*********found*********
strcpy(the_id,student_id);
}
const char*getCourse()const{return course;)//返回课程名称
//ERROR*********found*********
const char*getTD()const{return&student id;}//返回学号
int getNormal()const{return normal;} //返回平时成绩
int getMidterm()const{ return midterm;}
//返回期中考试成绩
int getEndOfTerm()const{return end of term;} //返回期末考试成绩
int getFinal()const;//返回总评成绩
private:
const char*course; //课程名称
char student id[12];//学号
int normal; //平时成绩
int midterm; //期中考试成绩
int end of term; //期末考试成绩
};
//总评成绩中平时成绩占20%,期中考试占30%,期末考试占50%,最后结果四舍五入为一个整数
//ERROR*********found*********
int getFinal()const{
return noEmal*0.2+midterm*0.3+end_of_term*0.5+0.5;
}
int main(){
char English[]=\\
(1)strcpy(student_id,the_id);
(2)const char*getID()const{return studentjd;}
(3)int Score::getFinal()const{
解析:(1)主要考查考生对strcpy()函数的掌握情况,strcpy(参数一,参数二)函数的功能是将参数二的字符串复制给参数一,因此在这里student_id应该位于参数一的位置,即strcpy(student_id,the_id);。
(2)主要考查考生对函数返回值的掌握情况,根据注释:返回学号可知学号应该由一个字符串组成。再看函数要返回的类型:const char*,可知要返回一个char型指针,也就是一个char型数组,而&student_jd是一个char型数组指针,因此直接写student_jd即可。
(3)主要考查考生对类的成员函数的掌握情况,因为getFinal函数是Score类的成员函数,所以在定义时要加上类名和作用域符,即Score::。
简单应用题
2.请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中在编辑窗口内显示的主程序文件中定义有类Base和Derived,以及主函数main。程序文本中位于每行“//****found****”下面的一行内有一处或多处下画线标记,请在每个下画线标记处填写合适的内容,并删除下画线标记。经修改后运行程序,得到的输出应为:
sum=55。
注意:只在横线处填写适当的代码,不要改动程序中的其他内容。
#include
using namespace std;
class Base
{
public:
Base(int m1,int m2) {
meta1=m1;mem2=m2;
}
int sum(){return mem1+mem2;}
private:
int mem1,mem2;//基类的数据成员
};
//派生类Derived从基类Base公有继承
//*********found*********
class Derired:
{
public:
//构造函数声明
Derived(int m1,int m2,int m3);
//sum函数定义,要求返回mem1、mem2和mem3之和
//*********found*********
int sum(){ return_________+mem3;}
private:
int mere3; //派生类本身的数据成员
};
//构造函数的类外定义,要求由m1和m2分别初始化meta1和mem2。由m3初始化mere3
//*********found*********
________Derived(int m1,int m2,int m3):
//*********found*********
________,mem3(m3){)
int main(){
Base a(4,6);
Derived b(10,15,20);
int sum=a.sum()+b.sum();
cout<<\\
(1)public Base
(2)Base::sum()
(3)Dertved::
(4)Base(m1,m2)
解析:(1)主要考查考生对公有继承的掌握情况,根据题目要求:派生类Derived从基类Base公有继承,因此这里使用public来公有继承。
(2)主要考查考生对成员函数的掌握情况,根据题目对sum函数的要求:sum函数定义,要求返回mem1、mem2和mem3之和,因此这里直接调用基类的sum函数,再加上mem3就满足题目要求。
(3)主要考查考生对构造函数的掌握情况,由于Derived的构造函数在类外定义,因此要加上类名和作用域符,即Derived::。
本文档预览:3600字符,共3682字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载