有人可以帮我解决这个问题吗?

时间:2015-11-14 03:20:10

标签: c++ object inheritance

//program8.cpp
#include <iostream>
#include "species.h"
#include "reptilian.h"
#include "mammalian.h"
#include "insects.h"

using namespace std;

void VirtualPrint(species &typeofSpecies){
 typeofSpecies.printme();
}

void VirtualDanger(species &typeofSpecies){
 typeofSpecies.showDanger();
}

int main(int argv, char **args){

 reptilian rep;
 insects ins; 
 VirtualPrint(rep);
 VirtualPrint(ins);
 VirtualDanger(rep);
 VirtualDanger(ins);
 return 1;
}


//species.h
#ifndef SPECIES_H
#define SPECIES_H
#include <iostream>
#include <string>

using namespace std;

class species{
 public:
  species();
  virtual void printme();
  virtual void showDanger() = 0;

 protected:
  string name;
  string color;
  int lifeExp;
  bool thr;
};

#endif

//species.cpp
#include <iostream>
#include <string>
#include "species.h"

using namespace std;

species::species(){
 cout << "Please enter name of species:" << endl;
 getline(cin, name);
 cout << "Please enter color of species:" << endl;
 getline(cin, color);
 cout << "Please enter life expectancy of species in years:"  << endl;
 cin >> lifeExp;
 cout << "Please enter if species is threat:true(1) or false(0)" << endl;
 cin >> thr;
}

void species::printme(){
 cout << "Name: " << name << "  Color: " << color << "  Life Expectancy: " << lifeExp << "  Threat: " << thr << endl;
}

//reptilian.h
#ifndef REPTILIAN_H
#define REPTILIAN_H
#include <iostream>
#include <string>
#include "species.h"

using namespace std;

class reptilian : public species{
 public:
  reptilian();
  virtual void printme();
  virtual void showDanger(); 

 protected:
  int length;
  int lethality;
};

#endif


//reptilian.cpp
#include <iostream>
#include "reptilian.h"

using namespace std;

reptilian::reptilian(){
 cout << "Please enter length(inches): " << endl;
 cin >> length;
 cout << "Please enter lethality(0-100): " << endl;
 cin >> lethality;
}

void reptilian::printme(){
 species::printme();
 cout << "  Length: " << length << "  Lethality: " << lethality << endl;;
}

void reptilian::showDanger(){
 cout << endl;
 if(thr == true){
  cout << "This species is a threat" << endl;
  cout << "The name of the species is " << name << " has a color of " << color << ", has a life expectancy of " << lifeExp << " has a length of " << length << ", and a lethality of " << lethality << endl;
 }
}

这是我的程序代码,如果生成一个爬虫类对象,它运行正常。但是当制作两个时,它将不会采用第二个对象的名称。当制作两个昆虫物体时也会发生同样的情况。我还没有测试过哺乳动物的对象,因为我试图首先解决这个问题

编辑: 输出:

请输入物种名称:

SAM

请输入物种颜色:

请输入物种的预期寿命:

100

如果物种是威胁,请输入:true(1)或false(0)

0

请输入长度(英寸):

60

请输入致死率(0-100):

0

请输入物种名称:

请输入物种颜色:

请输入物种的预期寿命:

20

如果物种是威胁,请输入:true(1)或false(0)

0

请输入长度(英寸):

10

请输入致死率(0-100):

0

请输入物种的毒性(0-100):

0

姓名:山姆颜色:橙色预期寿命:100威胁:0

长度:60致死率:0

名称:颜色:黄色预期寿命:20威胁:0

长度:10致死率:0

有毒:0

1 个答案:

答案 0 :(得分:1)

您的问题是使用getline>>运算符混合。来自http://en.cppreference.com/w/cpp/string/basic_string/getline

  

在以空格分隔的输入后立即使用,例如在int n之后; std :: cin&gt;&gt; n;,getline使用运算符&gt;&gt;消耗输入流上留下的结束字符,并立即返回。一个常见的解决方案是使用cin.ignore忽略输入行上的所有剩余字符(std :: numeric_limits :: max(),'\ n');在切换到面向行的输入之前。

所以cin >> lethalitycin信息流中留下换行符后。 <{1}}的第二个实例中的getline会看到此换行符并立即返回。

另请参阅:std::cin:: and why a newline remains

要解决此问题,请更改species来电以使用getline方法。