1、计算机二级 C+上机考试 6+2015 年及答案解析(总分:30.00,做题时间:90 分钟)1.基本操作 请使用 VC6 或使用【答题】菜单打开考生文件夹 proj1 下的工程 proj1,程序中位于每个“/ ERROR *found*“之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: fruit1 是苹果吗? true fruit1 是梨吗? false fruit2 是苹果吗? true fruit2 是梨吗? False 注意:只修改“/ ERROR*found*“的下一行语句,不要改动程序中的其他内容。 #include using namespace std; clas
2、s Fruits /水果类 public: Fruits(char *the_name, float the_price); / ERROR *found* void Fruits() bool isFruit(char *name)const; /判断是否是参数所指定的水果 private: char name50; /水果名称 float price; /水果价格 ; Fruits:Fruits(char *the_name, float the_price) / ERROR *found* strcpy(the_name,name); price = the_price; bool Fr
3、uits:isFruit(char *name)const / ERROR *found* return strcmp(name,name)=0; int main() Fruits fruit1(“苹果“,3.00); Fruits fruit2(“梨“,2.50); cout name,name)=0;)解析:【解题思路】 (1)主要考查的是 Fruits 类的析构函数,析构函数没有返回值,所以应该为Fruits() (2)主要考查库函数 strcpy 的应用,本题中目的是想将形参 the_name 赋值给当前对象的 name数据成员,所以 strcpy 的第一个参数应该为 name。 (
4、3)主要考查 this 指针,本题中要使用 strcmp,所以第一个参数为 this-name2.简单应用 请使用 VC6 或使用【答题】菜单打开考生文件夹 proj2 下的工程 proj2,此工程包含程序文件 main.cpp,其中有类 Animal(“动物“)、类 Dog(“狗“)、类 Cat(“猫“)的定义和主函数 main 的定义。请在程序中“/*found*“下的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为: Dog named Lucky speaks Woof Cat named Daisy speaks Miaow 注意:只能在横线处填写适当的
5、代码,不要改动程序中的其他内容,也不能删除或移动“/*found*“。 #include using namespace std; class Animal public: Animal(char* str=“Animal“) /*found* name = new _; strcpy(name,str); virtual Animal() delete name; char* getName() const return name; virtual char* getType() const return “Animal“; virtual char* getVoice() const re
6、turn “Voice“; private: char* name; ; class Dog : public Animal public: Dog(char* str) : Animal(str) char* getType() const return “Dog“; /*found* char* getVoice() const _ ; class Cat : public Animal public: /*found* Cat(char* str) : _ char* getType() const return “Cat“; char* getVoice() const return “Miaow“; ; void type(Animal ai = aj; aj = temp; )解析:【解题思路】 题目要求对整数序列按非递减排序要排序就必须要有比较,所以定义两个下标 i 和j,按题目递增排序要求,当*ai比*aj大时就交换其值,利用中间变量 temp 来实现。