使用VC6打开考生文件夹下的工程test21_1,此工程包含一个源程序文件test21_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
The grade is 3
源程序文件test21_1.cpp清单如下:
#include<iostream.h>
class student
private:
int grade;
public:
/**************** found*******************/
student(int thegra):(thegra)
~student()
int get_grade()return grade;
;
void main()
int thegra=3;
/**************** found*******************/
student point=new student(thegra);
/**************** found*******************/
cout<<"The grade is"<<point.get_grade()<<endl;
delete point;
参考答案:
(1)错误:student(int thegra):(thegra){}
正确:student(int thegra):grade(thegra){}
(2)错误:student point=new student(thegra)
正确:student*point=new student(thegra);
(3)错误:cout<<"The grade is"<<point.get_grade()<<endl;
正确:cout<<"The grade is"<<point->get_grade()<<endl;
解析:
(1)主要考查考生对于构造函数使用参数列表初始化数据成员的掌握,可以使用参数列表的形式,也可以在函数内部实现,不过参数赋值的双方都必须出现,本题的错误就在于并没有把参数赋值给数据成员;
(2)主要考查考生对于使用指针申请动态空间的方法的理解,new运算符申请的空间返回值为指针类型,指针类型的定义是在变量名前加上“*”;
(3)主要考查考生对于对象指针调用成员函数方法的掌握,必须使用符号“->”,而对象本身使用符号“.”。