2954: A改错题--是虫还是草

时间限制: 1 Sec  内存限制: 128 MB

提交: 83  解决: 55

题目描述

冬虫夏草为虫体与菌座相连而成,冬天是虫子,夏天却是草。根据类生物(biological),虫(insect),草(grass),虫草(insectgrass)的继承关系,输出生物的名字。

注:本题只需要提交修改和完善部分的代码,请按照C++方式提交。

#include <iostream>

using namespace std;

class Biological

{

public:

    Biological(string name) { this->name=name; }

    virtual void Say() =0;

    virtual ~Biological() {}

    string GetName() { return name; }

private:

    string name;

};

/*   修改和完善该部分代码

class Insect:virtual public Biological

{

public:

    Insect(string name):Biological(name) {}

    virtual void Say() { cout<<this->GetName()<<"是虫"<<endl; }

    virtual ~Insect() {}

};

class Grass:virtual public Biological

{

public:

    Grass(string name):Biological(name) {}

    virtual ~Grass() {}

};

class InsectGrass:public Insect,public Grass

{

public:

    InsectGrass(string name):Insect(name),Grass(name) {}

    virtual ~InsectGrass() {}

};

*/

int main()

{

    Biological *p[3];

    string name;

    getline(cin,name);

    p[0]=new Insect(name);

    getline(cin,name);

    p[1]=new Grass(name);

    getline(cin,name);

    p[2]=new InsectGrass(name);

    for(int i=0; i<3; i++)

    {

        p[i]->Say();

        delete p[i];

    }

    return 0;

}

输入

第一行,虫的名字

第二行,草的名字

第三行,虫草的名字

输出

每行输出相应的名字及类别。

样例输入

Coccinellidae
Prunella vulgaris
Cordyceps sinensis

样例输出

Coccinellidae是虫
Prunella vulgaris是草
Cordyceps sinensis是虫也是草

你  离  开  了  ,  我  的  世  界  里  只  剩  下  雨  。  。  。

#include <iostream>
using namespace std;
class Biological
{
public:
Biological(string name)
{
this->name=name;
}
virtual void Say() =0;
virtual ~Biological() {}
string GetName()
{
return name;
} private:
string name;
};
class Insect:virtual public Biological
{
public:
Insect(string name):Biological(name) {}
void Say()
{
cout<<this->GetName()<<"是虫"<<endl;
}
virtual ~Insect() {}
};
class Grass:virtual public Biological
{
public:
Grass(string name):Biological(name) {}
void Say()
{
cout<<this->GetName()<<"是草"<<endl;
}
virtual ~Grass() {}
};
class InsectGrass:public Insect,public Grass
{
public:
InsectGrass(string name):Insect(name),Grass(name),Biological(name) {}
void Say()
{
cout<<this->GetName()<<"是虫也是草"<<endl;
}
virtual ~InsectGrass() {}
};
int main()
{
Biological *p[3];
string name;
getline(cin,name);
p[0]=new Insect(name);
getline(cin,name);
p[1]=new Grass(name);
getline(cin,name);
p[2]=new InsectGrass(name);
for(int i=0; i<3; i++)
{
p[i]->Say();
delete p[i];
}
return 0;
}

04-26 10:31