C++ 有人能帮我解决这个问题吗? //program8.cpp #包括 #包括“物种h” #包括“爬行动物.h” #包括“哺乳动物h” #包括“昆虫.h” 使用名称空间std; void VirtualPrint(物种和物种类型){ 种类的类型。printme(); } 无效虚拟愤怒(物种和物种类型){ 物种类型。显示危险(); } int main(int argv,char**args){ 爬行动物代表; 昆虫; 虚拟打印(rep); 虚拟打印(ins); 虚拟愤怒; 虚拟愤怒(ins); 返回1; } //物种 #ifndef物种 #定义物种 #包括 #包括 使用名称空间std; 类种{ 公众: 物种(); 虚拟void printme(); 虚拟void showDanger()=0; 受保护的: 字符串名; 字符串颜色; 国际生命体验; bool-thr; }; #恩迪夫 //种类.cpp #包括 #包括 #包括“物种h” 使用名称空间std; 种类::种类(){ cout
您的问题是使用C++ 有人能帮我解决这个问题吗? //program8.cpp #包括 #包括“物种h” #包括“爬行动物.h” #包括“哺乳动物h” #包括“昆虫.h” 使用名称空间std; void VirtualPrint(物种和物种类型){ 种类的类型。printme(); } 无效虚拟愤怒(物种和物种类型){ 物种类型。显示危险(); } int main(int argv,char**args){ 爬行动物代表; 昆虫; 虚拟打印(rep); 虚拟打印(ins); 虚拟愤怒; 虚拟愤怒(ins); 返回1; } //物种 #ifndef物种 #定义物种 #包括 #包括 使用名称空间std; 类种{ 公众: 物种(); 虚拟void printme(); 虚拟void showDanger()=0; 受保护的: 字符串名; 字符串颜色; 国际生命体验; bool-thr; }; #恩迪夫 //种类.cpp #包括 #包括 #包括“物种h” 使用名称空间std; 种类::种类(){ cout,c++,object,inheritance,C++,Object,Inheritance,您的问题是使用getline与>运算符混合。发件人: 当在以空格分隔的输入之后立即使用时,例如在int n;std::cin>>n;之后,getline将使用运算符>>在输入流上留下的结束行字符,并立即返回。常见的解决方案是使用cin.ignore忽略输入行上的所有剩余字符(std::numeric_limits::max(),“\n”);在切换到线路输入之前 因此,cin>>致命性之后,cin流中会留下一条新行。物种第二个实例中的getline看到这条新行并立即返回 另请参阅: 要解决此问题,
getline
与>
运算符混合。发件人:
当在以空格分隔的输入之后立即使用时,例如在int n;std::cin>>n;之后,getline将使用运算符>>在输入流上留下的结束行字符,并立即返回。常见的解决方案是使用cin.ignore忽略输入行上的所有剩余字符(std::numeric_limits::max(),“\n”);在切换到线路输入之前
因此,cin>>致命性之后,cin
流中会留下一条新行。物种第二个实例中的getline
看到这条新行并立即返回
另请参阅:
要解决此问题,请将getline
调用更改为使用cin>
方法。您能显示输出和错误吗?
//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;
}
}