问答题
简单应用 请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程包含程序文件main.cpp,其中有类Animal("动物")、类Dog("狗")、类Cat("猫")的定义和主函数main的定义。请在程序中"//****found****"下的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为: Dog named Lucky speaks Woof Cat 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<
【参考答案】
(1)char[strlen(str)+1](2)return "Woof";(3)Animal(str)(4)Anim......
(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
点击查看答案&解析
<上一题
目录
下一题>
热门
试题
问答题
基本操作请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,程序中位于每个 ERROR ****found**** 之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:fruit1 是苹果吗? truefruit1 是梨吗? falsefruit2 是苹果吗? truefruit2 是梨吗? 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;}
点击查看答案&解析
相关试题
综合应用请使用VC6或使用【答题】菜单打开...