问答题

基本操作 请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,程序中位于每个"// ERROR ****found****"之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: fruit1 是苹果吗? true fruit1 是梨吗? false fruit2 是苹果吗? true fruit2 是梨吗? False 注意:只修改"/ ERROR****found****"的下一行语句,不要改动程序中的其他内容。 #include using namespace std; class Fruits{ //水果类 public: Fruits(char *the_name, float the_price); // ERROR **********found********** void ~Fruits() {} bool isFruit(char *name)const; //判断是否是参数所指定的水果 private: char name[50]; //水果名称 float price; //水果价格 }; Fruits::Fruits(char *the_name, float the_price){ // ERROR **********found********** strcpy(the_name,name); price = the_price; } bool Fruits::isFruit(char *name)const{ // ERROR **********found********** return strcmp(name,name)==0; } int main(){ Fruits fruit1("苹果",3.00); Fruits fruit2("梨",2.50); cout << boolalpha; cout << "fruit1 是苹果吗? " << fruit1.isFruit("苹果") << endl; cout << "fruit1 是梨吗? " << fruit1.isFruit("梨") << endl; cout << "fruit2 是苹果吗? " << fruit1.isFruit("苹果") << endl; cout << "fruit2 是梨吗? " << fruit1.isFruit("梨") << endl; return 0; }

【参考答案】

(1)~Fruits() {}(2)strcpy(name,the_name);(3)return strcmp(thi......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
简单应用请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程包含程序文件main.cpp,其中有类Animal( 动物 )、类Dog( 狗 )、类Cat( 猫 )的定义和主函数main的定义。请在程序中 ****found**** 下的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:Dog named Lucky speaks WoofCat named Daisy speaks Miaow注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动 ****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 { return 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& a) {cout<<a.getType()<< named <<a.getName();} **********found**********void speak(___________________){cout<< speaks <<a.getVoice();}int main(){Dog d( Lucky );type(d);speak(d);cout<<endl;Cat c( Daisy );type(c);speak(c);cout<<endl;return 0;}