公司Company类派生出集团公司Group类,扩展数据成员子公司数量 【问题描述】…
- User
公司Company类派生出集团公司Group类,扩展数据成员子公司数量 【问题描述】 公司Company类包含公司名称name(字符串)、count人数(大于0的整数)两个数据成员和构造函数及成员函数show(显示公司信息);由Company类派生出集团公司Group类,扩展数据成员子公司数量(大于0的整数)和构造函数及成员函数show(显示集团公司信息)。依次输入集团公司名name、子公司数量、总人数count,输出相应的集团公司信息。 参考主函数: int main() { string name; int count, total; cout << "请输入总公司名和子公司数量及总人数:"; cin >> name >> total >> count; Group grp(name, count, total); grp.show(); return 0; } 【输入形式】 【输出形式】 【样例输入】 请输入总公司名和子公司数量及总人数:信科集团 5 500 【样例输出】
信科集团 500 5 【样例输入】 请输入总公司名和子公司数量及总人数:信科集团 0 500 【样例输出】
子公司数量错误! 【样例输入】 请输入总公司名和子公司数量及总人数:信科集团 500 0 【样例输出】
人数错误!
- Assistant
#include using namespace std; class company { public: company(string _name,int _count):name(_name),count(_count){} virtual void show(){ cout<<name<<endl<<count<<endl; } protected: string name; int count; }; class group:public company { public: group(string _name,int _count,int _total):company(_name,_total) { if(_count<=0)cout<<"子公司数量错误!"<<endl,sub_count=0; else sub_count=_count; if(_total<=0)cout<<"人数错误!"<<endl,count=0; } void show(){ company::show(); cout<<sub_count<<endl; } private: int sub_count; }; int main() { string name; int count, total; cout << "请输入总公司名和子公司数量及总人数:"; cin >> name >> count >> total; group grp(name, count, total); grp.show(); return 0; }
- User
编译错误. Main.cpp:19:1: error: stray '\342' in program if(_count<=0)cout<<“子公司数量错误!”<<endl,sub_count=0; ^ Main.cpp:19:1: error: stray '\200' in program Main.cpp:19:1: error: stray '\234' in program Main.cpp:19:1: error: stray '\345' in program Main.cpp:19:1: error: stray '\255' in program Main.cpp:19:1: error: stray '\220' in program Main.cpp:19:1: error: stray '\345' in program Main.cpp:19:1: error: stray '\205' in program Main.cpp:19:1: error: stray '\254' in program Main.cpp:19:1: error: stray '\345' in program Main.cpp:19:1: error: stray '\217' in program Main.cpp:19:1: error: stray '\270' in program Main.cpp:19:1: error: stray '\346' in program Main.cpp:19:1: error: stray '\225' in program Main.cpp:19:1: error: stray '\260' in program Main.cpp:19:1: error: stray '\351' in program Main.cpp:19:1: error: stray '\207' in program Main.cpp:19:1: error: stray '\217' in program Main.cpp:19:1: error: stray '\351' in program Main.cpp:19:1: error: stray '\224' in program Main.cpp:19:1: error: stray '\231' in program Main.cpp:19:1: error: stray '\350' in program Main.cpp:19:1: error: stray '\257' in program Main.cpp:19:1: error: stray '\257' in program Main.cpp:19:1: error: stray '\357' in program Main.cpp:19:1: error: stray '\274' in program Main.cpp:19:1: error: stray '\201' in program Main.cpp:19:1: error: stray '\342' in program Main.cpp:19:1: error: stray '\200' in program Main.cpp:19:1: error: stray '\235' in program Main.cpp:21:1: error: stray '\342' in program if(_total<=0)cout<<“人数错误!”<<endl,count=0; ^ Main.cpp:21
- Assistant
问题分析:编译错误是因为引号使用不正确,是因为拷贝的代码是在有修改单双引号的情况下进行复制的,而复制后的引号变成了中文引号,导致编译器无法识别所出现的问题,需要将中文引号修改成英文引号。
修改方案:将引号替换成英文引号。
具体方法:将“ ”变成" "即可。
完整代码:
- User
- Integer类,包含一个int型成员,一个构造函数Integer,一个成员函数gcd 【问题描述】 设计一个Integer类,包含一个int型成员,一个构造函数Integer(用于初始化参数),一个成员函数gcd(计算最大公约数),输入两个正整数a 和 b,得到其最大公约数。 参考主函数: int main() { int a, b; cin >> a >> b; Integer A(a); Integer B(b); cout << A.gcd(B) << endl; return 0; } 【输入形式】 【输出形式】 【样例输入】 【样例输出】 0 12 【样例输入】 input error 【样例输出】 15 12
- Assistant
#include using namespace std; class Integer{ public: Integer(int n){ number=n; } int gcd(Integer b){ if(number==0||b.number==0)return -1; else{ int aa=number>0?number:-number;//绝对值 int bb=b.number>0?b.number:-b.number; if(aa<bb)swap(aa,bb);//保证aa>bb int cc=aa%bb; if(cc==0)return bb; else{ Integer now(cc); return b.gcd(now); } } } private: int number; }; int main() { int a, b; cin >> a >> b; if(a<=0||b<=0)cout<<"input error"<<endl; else{ Integer A(a); Integer B(b); cout << A.gcd(B) << endl; } return 0; }
- User
编译错误. Main.cpp:28:1: error: stray '\342' in program if(a<=0||b<=0)cout<<“input error”<<endl; ^ Main.cpp:28:1: error: stray '\200' in program Main.cpp:28:1: error: stray '\234' in program Main.cpp:28:1: error: stray '\342' in program Main.cpp:28:1: error: stray '\200' in program Main.cpp:28:1: error: stray '\235' in program Main.cpp: In function 'int main()': Main.cpp:28:24: error: 'input' was not declared in this scope if(a<=0||b<=0)cout<<“input error”<<endl; ^ Main.cpp:28:30: error: expected ';' before 'error' if(a<=0||b<=0)cout<<“input error”<<endl; ^
- Assistant
问题分析:编译错误是因为引号使用不正确,是因为拷贝的代码是在有修改单双引号的情况下进行复制的,而复制后的引号变成了中文引号,导致编译器无法识别所出现的问题,需要将中文引号修改成英文引号。
修改方案:将引号替换成英文引号。
具体方法:将“ ”变成" "即可。
完整代码:
- User
继续
- Assistant
#include using namespace std; class Integer{ public: Integer(int n){ number=n; } int gcd(Integer b){ if(number==0||b.number==0)return -1; else{ int aa=number>0?number:-number;//绝对值 int bb=b.number>0?b.number:-b.number; if(aa<bb)swap(aa,bb);//保证aa>bb int cc=aa%bb; if(cc==0)return bb; else{ Integer now(cc); return b.gcd(now); } } } private: int number; }; int main() { int a, b; cin >> a >> b; if(a<=0||b<=0)cout<<"input error"<<endl; else{ Integer A(a); Integer B(b); cout << A.gcd(B) << endl; } return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.