| 查看: 1908 | 回复: 8 | ||
| 当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖 | ||
lhy317409772银虫 (初入文坛)
|
[求助]
求大神帮忙解答!已有1人参与
|
|
|
困扰了蛮久了....求大神帮忙解答程序错在哪里....疑问都在程序旁边注释出来了。 新手贴,没什么金币,抱歉....今后努力赚金币。 非常感谢..... ![]() 源程序如下: //头文件 #include<iostream> class Port { private: char* brand; char style[20]; int bottles; public: Port(const char* br="none", const char* st="none", int b=0); Port(const Port& p); virtual ~Port() {delete [] brand;} Port& operator =(const Port& p); Port& operator+=(int b); Port& operator-=(int b); int BottleCount() const {return bottles;} virtual void Show() const; friend std: stream& operator<< (std: stream& os, const Port& p);}; class VintagePort: public Port { private: char* nickname; int year; public: VintagePort(); VintagePort(const char* br, int b, const char* nn, int y); VintagePort(const VintagePort& vp); ~VintagePort() {delete [] nickname;} VintagePort& operator= (const VintagePort& vp); virtual void Show() const; friend std: stream& operator<< (std: stream& os, const VintagePort& vp);}; //实现文件 #include<iostream> #include<cstring> #include"Port.h" Port: ort(const char* br, const char* st, int b){ brand=new char[strlen(br)+1]; strcpy(brand, br); strncpy(style, st, 20); bottles=b; } Port: ort(const Port& p){ brand=new char[strlen(p.brand)+1]; strcpy(brand, p.brand); strncpy(style, p.style, 20); bottles=p.bottles; } Port& Port: perator = (const Port& p){ if (this==&p) return *this; delete [] brand; brand=new char[strlen(p.brand)+1]; strcpy(brand, p.brand); strncpy(style, p.style, 20); bottles=p.bottles; return *this; } Port& Port: perator += (int b){ bottles+=b; return *this; } Port& Port: perator -=(int b){ if (b<=bottles) bottles-=b; else std::cout<< "You can't take more bottles than we have!\n" << "Operation cancelled!\n"; return *this; } void Port::Show ()const { std::cout<< "Brand: "<< brand << "\nKind: "<< style << "\nBottles: "<< bottles <<std::endl; } std: stream& operator<< (std: stream& os, const Port& p){ os << p.brand << ", " << p.style << ", " << p.bottles; return os; } VintagePort::VintagePort() ort(){ nickname=new char[5]; strcpy(nickname, "none" ;year=0; } VintagePort::VintagePort(const char* br, int b, const char* nn, int y) ort(br, "vintage", b){ nickname=new char[strlen(nn)+1]; strcpy(nickname, nn) ; year=y; } VintagePort::VintagePort(const VintagePort& vp) ort(vp){ nickname=new char[strlen(vp.nickname)+1]; strcpy(nickname, vp.nickname); year=vp.year; } VintagePort& VintagePort: perator = (const VintagePort& vp){ Port: perator = (vp);delete [] nickname; nickname=new char[strlen(vp.nickname)+1]; strcpy(nickname, vp.nickname); year=vp.year; return *this; } void VintagePort::Show () const { Port::Show (); std::cout<< "Nickname: " << nickname << "\nYear: " << year << std::endl; } std: stream& operator<< (std: stream& os, const VintagePort& vp){ os << (const Port& vp; os << ", " << vp.nickname << ", " << vp.year; return os; } //主程序 #include<iostream> #include"Port.h" int main() { using std::cout; using std::cin; using std::endl; Port* pointer[10]; pointer[0]=&Port("Lafite", "tawny", 27); pointer[1]=&Port("Latour", "ruby", 23); pointer[2]=&VintagePort("Haut-Brion", 35, "The Noble", 1875); pointer[3]=&VintagePort("Margaux", 56, "Old Velvet", 1898); for (int k=0;k<4;k++) //这个不是主程序的部分,我是想用来测试,我用基类指针调用Show()函数的结果, //结果与我预想的不一样,我的Show()函数是虚拟的,我用基类指针(Port*)指向派类对象 { pointer[k]->Show (); //用pointer[0]->Show()的时候,应该是调用Port::Show(),这个调用是正确的,但是我的brand成员显示却出现了乱码,这个不明白是怎么回事。 cout << endl; //用pointer[2]->Show()的时候,应该是调用VintagePort::Show(),这个调用是错的,结果调用的是Port::Show(),而且与上面一样brand成员显示却出现了乱码。 } Port& b=Port("Lafite", "tawny", 27); //我这里用基类引用指向基类对象,用引用调用的时候,就不出现乱码,所以我觉得应该问题出现在指针上 b.Show (); //但是,实在想不到是为什么? 求解答。 cout << b <<endl; Port& a=VintagePort("Margaux", 56, "Old Velvet", 1898); a.Show(); //还有这里,我用基类引用指向派生对象,在调用Show()的时候,就调用的是VintagePort::Show(),是正确的。 cout << a << endl; //但是这里又出现了问题,我重载的cout << a,应该需要显示nickname和year的,但是却与cout << b是一样的,是我的重载出现问题了么?? int choice,i=4; cout << "Do you want to take or save port(s)? (1 to take/2 to save/q to quit)"; while (cin >> choice) { if (choice==1) { int kind=0,num=0; cout << "Which brand do you need?(1:Lafite, 2:Latour, 3:Haut-Brion, 4:Margaux)"; cin >> kind; cin.get(); switch (kind) { case 1: { cout << "How many bottles do you need?\n"; cout << "There're " << pointer[0]->BottleCount() << " saved.\n"; cin >> num; *pointer[0]-=num; pointer[0]->Show(); break; } case 2: { cout << "How many bottles do you need?\n"; cout << "There're " << pointer[1]->BottleCount() << " saved.\n"; cin >> num; *pointer[1]-=num; pointer[1]->Show(); break; } case 3: { cout << "How many bottles do you need?"; cout << "There're " << pointer[2]->BottleCount() << " saved.\n"; cin >> num; (Port& *pointer[2]-=num; //或者直接用*pointer[2]-=num;因为-=的函参为Port&,可以指向VintagePort。pointer[2]->Show(); break; } case 4: { cout << "How many bottles do you need?"; cout << "There're " << pointer[3]->BottleCount() << " saved.\n"; cin >> num; *pointer[3]-=num; pointer[3]->Show(); break; } default: cout << "That's not a choice!\n"; } } else { int num=0; cout << "Please enter the brand:"; //这里我无法进行输入。 char temp[50]; cin.getline(temp, 50); cin.get(); if (temp=="Lafite" //我这里的字符串的比较应该是有问题的,不能用==判断。{ cout << "Save bottles: "; cin >> num; *pointer[0]+=num; pointer[0]->Show(); } else if (temp=="Latour" ![]() { cout << "Save bottles: "; cin >> num; *pointer[1]+=num; pointer[1]->Show(); } else if (temp=="Haut-Brion" ![]() { cout << "Save bottles: "; cin >> num; *pointer[2]+=num; pointer[2]->Show(); } else if (temp=="Margaux" ![]() { cout << "Save bottles: "; cin >> num; *pointer[3]+=num; pointer[3]->Show(); } else { cout << "Enter the style: "; char s_temp[10]; cin.getline(s_temp, 10); if (s_temp=="vintage" //这里的判断也有问题。{ cout << "Enter the nickname: "; char n_temp[20]; cin.getline(n_temp, 20); cout << "Enter the year: "; int y_temp; cin >> y_temp; cout << "Enter the bottles: "; int b_temp; cin >> b_temp; pointer=&VintagePort(temp, b_temp, n_temp, y_temp); } else { cout << "Enter the bottles: "; int b_temp; cin >> b_temp; pointer=&Port(temp, s_temp, b_temp); } } } ++i; cout << "Do you want to take or save port(s)? (1 to take/2 to save/q to quit)"; } char ch; cin.get(); cout << "Do you want to see all the Port's information?(y/n) "; //最后还有这里,无法进行输入.... cin.get(ch); if (ch=='y'||ch=='Y') for (int c=0;c<i;++c) cout << *pointer[c] << endl; cout << "Bye!\n"; return 0; } [ Last edited by lhy317409772 on 2014-1-8 at 22:49 ] |
» 猜你喜欢
康复大学泰山学者周祺惠团队招收博士研究生
已经有6人回复
AI论文写作工具:是科研加速器还是学术作弊器?
已经有3人回复
孩子确诊有中度注意力缺陷
已经有6人回复
2026博士申请-功能高分子,水凝胶方向
已经有6人回复
论文投稿,期刊推荐
已经有4人回复
硕士和导师闹得不愉快
已经有13人回复
请问2026国家基金面上项目会启动申2停1吗
已经有5人回复
同一篇文章,用不同账号投稿对编辑决定是否送审有没有影响?
已经有3人回复
ACS Applied Polymer Materials投稿
已经有10人回复
RSC ADV状态问题
已经有4人回复
» 本主题相关价值贴推荐,对您同样有帮助:
求大神帮忙解出两道沉淀溶解题!
已经有18人回复
负极扣式电池测试过程中第二个循环容量低,之后恢复正常,求大神帮忙解答或分析原因
已经有3人回复
跪求各位大神帮忙
已经有3人回复
修改图标问题,请各位大神帮忙!
已经有5人回复
求大神解答···
已经有8人回复
新手求助高斯输入文件写法,求大神帮忙
已经有8人回复
求各路大神们帮忙鉴定一下下面几种植物,谢谢了
已经有5人回复
求大神给解答一下!
已经有4人回复
请各位大神帮忙分析个红外谱图
已经有8人回复
求大神帮忙设计一个工装夹具
已经有6人回复
大神来帮忙解释一下,上下两个图是什么意思
已经有10人回复
跪求大神帮忙翻译一下简历
已经有5人回复
求大神~~~谁会化工工艺设计的~~~~~来各种帮忙~~
已经有7人回复
几道政治问题。求大神解答。
已经有11人回复
几道政治问题,求大神解答
已经有6人回复


6楼2014-01-13 13:41:00
lhy317409772
银虫 (初入文坛)
- 应助: 0 (幼儿园)
- 金币: 709.7
- 散金: 20
- 红花: 1
- 帖子: 39
- 在线: 10.3小时
- 虫号: 2735808
- 注册: 2013-10-19
- 性别: GG
- 专业: 控制理论与方法

2楼2014-01-08 22:50:57

3楼2014-01-09 22:29:39
lhy317409772
银虫 (初入文坛)
- 应助: 0 (幼儿园)
- 金币: 709.7
- 散金: 20
- 红花: 1
- 帖子: 39
- 在线: 10.3小时
- 虫号: 2735808
- 注册: 2013-10-19
- 性别: GG
- 专业: 控制理论与方法
|
非常感谢,问题已经解决了绝大部分.... 这里还有一些疑问,希望能得到解决: 1.如何发代码帖子,我的符号变成表情了,注意到您的代码外面穿了马甲:code,请问怎么做到的....我是新人,嘿嘿。 2.Port &&b = Port("Lafite", "tawny", 27); 这里好像有错误,报错 引用无法赋值给引用..... 您说,只有常量引用和右值引用可以绑定在一个临时变量上,我写的是Port& b = Port("Lafite", "tawny", 27);我把左值引用绑定在了临时变量上(请问,您说是不规范的,这里是为什么不规范?),const Port &b = Port("Lafite", "tawny", 27);这个是把常量引用绑定在临时变量上,但是请问一下把右值引用绑定在临时变量上是什么例子? 3. pointer[5] = new VintagePort(temp, b_temp, n_temp, y_temp); 这里为什么要new一个VintagePort? 还是因为只能对左值进行取地址操作?如果仅仅是 Port("Lafite", "tawny", 27);这里创建的是临时变量么?? 4. cout << "Do you want to see all the Port's information?(y/n) "; //最后还是无法输入。 char infor; cin.get();//我在这里已经用这个把cin >> choice;留下的\n给销毁了,但是下面一个读取还是不能工作。 cin.get(infor); if (infor=='y'||infor=='Y') for (int c=0;c<i;++c) cout << *pointer[c] << endl; else cout << "Bye!\n"; 十分感谢。 |

4楼2014-01-12 23:00:23














stream& operator<< (std:
ort(const char* br, const char* st, int b)
;
回复此楼
