| 5 | 1/1 | 返回列表 |
| 查看: 2083 | 回復(fù): 8 | |||
| 當(dāng)前只顯示滿足指定條件的回帖,點(diǎn)擊這里查看本話題的所有回帖 | |||
lhy317409772銀蟲 (初入文壇)
|
[求助]
求大神幫忙解答! 已有1人參與
|
||
|
困擾了蠻久了....求大神幫忙解答程序錯(cuò)在哪里....疑問都在程序旁邊注釋出來了。 新手貼,沒什么金幣,抱歉....今后努力賺金幣。 非常感謝..... ![]() 源程序如下: //頭文件 #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);}; //實(shí)現(xiàn)文件 #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++) //這個(gè)不是主程序的部分,我是想用來測(cè)試,我用基類指針調(diào)用Show()函數(shù)的結(jié)果, //結(jié)果與我預(yù)想的不一樣,我的Show()函數(shù)是虛擬的,我用基類指針(Port*)指向派類對(duì)象 { pointer[k]->Show (); //用pointer[0]->Show()的時(shí)候,應(yīng)該是調(diào)用Port::Show(),這個(gè)調(diào)用是正確的,但是我的brand成員顯示卻出現(xiàn)了亂碼,這個(gè)不明白是怎么回事。 cout << endl; //用pointer[2]->Show()的時(shí)候,應(yīng)該是調(diào)用VintagePort::Show(),這個(gè)調(diào)用是錯(cuò)的,結(jié)果調(diào)用的是Port::Show(),而且與上面一樣brand成員顯示卻出現(xiàn)了亂碼。 } Port& b=Port("Lafite", "tawny", 27); //我這里用基類引用指向基類對(duì)象,用引用調(diào)用的時(shí)候,就不出現(xiàn)亂碼,所以我覺得應(yīng)該問題出現(xiàn)在指針上 b.Show (); //但是,實(shí)在想不到是為什么? 求解答。 cout << b <<endl; Port& a=VintagePort("Margaux", 56, "Old Velvet", 1898); a.Show(); //還有這里,我用基類引用指向派生對(duì)象,在調(diào)用Show()的時(shí)候,就調(diào)用的是VintagePort::Show(),是正確的。 cout << a << endl; //但是這里又出現(xiàn)了問題,我重載的cout << a,應(yīng)該需要顯示nickname和year的,但是卻與cout << b是一樣的,是我的重載出現(xiàn)問題了么?? 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;因?yàn)?=的函參為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:"; //這里我無法進(jìn)行輸入。 char temp[50]; cin.getline(temp, 50); cin.get(); if (temp=="Lafite" //我這里的字符串的比較應(yīng)該是有問題的,不能用==判斷。{ 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) "; //最后還有這里,無法進(jì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 ] |

銀蟲 (初入文壇)
|
嗯....我自己用程序測(cè)試了一下,這個(gè)是個(gè)臨時(shí)變量,創(chuàng)建后馬上調(diào)用了析構(gòu)函數(shù)...... 呃,我這里用cin >> choice是想把cin的返回值當(dāng)作判斷條件,因?yàn)楫?dāng)我輸入數(shù)字的時(shí)候cin返回1進(jìn)入循環(huán),輸入非數(shù)字的時(shí)候就讀取失敗cin返回0,退出循環(huán)。 我按著您的提示修改了程序,用break來跳出循環(huán)....程序確實(shí)是可以運(yùn)行了,最后也可以進(jìn)行輸入了...但是我不明白用cin >> choice來進(jìn)行輸入,為什么會(huì)影響到后面一個(gè)字符的輸入,我覺得這里應(yīng)該是最后一個(gè)輸入讀取了前方的q或者\(yùn)n,所以直接判斷為非y所以無法顯示所有信息..... 然后,只有成員函數(shù)才可以寫成虛函數(shù),如果我這里寫成虛函數(shù)了就只能有一個(gè)函參,這么寫:virtual std: stream& operator << (std: stream & os) {os << *this;(ps:原來的<<沒變,這里調(diào)用原來的友元) return os;},所以我在輸出的時(shí)候這么調(diào)用*pointer[c]<< cout << endl;看起來蠻別扭,但是結(jié)果確實(shí)是正確的....請(qǐng)問,這個(gè)方法能不能改進(jìn)使得用 cout << *opinter[c];來調(diào)用?? 還有您說的RTTI自己還沒有涉及到,但是我在想如果檢查出是派生對(duì)象,把基類*pointer[c]進(jìn)行轉(zhuǎn)換了輸出,這個(gè)豈不是向下轉(zhuǎn)換,理論上是不允許的,因?yàn)榭赡軄G失數(shù)據(jù),難道這里的可以么?? 最后,我目前才學(xué)習(xí)C++不久,使用的教材是《C++ primer Plus(第五版)》,覺得還不錯(cuò),即將把這本書看完,這里希望您能給點(diǎn)指導(dǎo),想知道看完這本書后可以繼續(xù)看看別的什么比較好的教材來深入學(xué)習(xí).... 非常感謝您不厭其煩的指導(dǎo).... |

銀蟲 (初入文壇)

木蟲 (正式寫手)

銀蟲 (初入文壇)
|
非常感謝,問題已經(jīng)解決了絕大部分.... 這里還有一些疑問,希望能得到解決: 1.如何發(fā)代碼帖子,我的符號(hào)變成表情了,注意到您的代碼外面穿了馬甲:code,請(qǐng)問怎么做到的....我是新人,嘿嘿。 2.Port &&b = Port("Lafite", "tawny", 27); 這里好像有錯(cuò)誤,報(bào)錯(cuò) 引用無法賦值給引用..... 您說,只有常量引用和右值引用可以綁定在一個(gè)臨時(shí)變量上,我寫的是Port& b = Port("Lafite", "tawny", 27);我把左值引用綁定在了臨時(shí)變量上(請(qǐng)問,您說是不規(guī)范的,這里是為什么不規(guī)范?),const Port &b = Port("Lafite", "tawny", 27);這個(gè)是把常量引用綁定在臨時(shí)變量上,但是請(qǐng)問一下把右值引用綁定在臨時(shí)變量上是什么例子? 3. pointer[5] = new VintagePort(temp, b_temp, n_temp, y_temp); 這里為什么要new一個(gè)VintagePort? 還是因?yàn)橹荒軐?duì)左值進(jìn)行取地址操作?如果僅僅是 Port("Lafite", "tawny", 27);這里創(chuàng)建的是臨時(shí)變量么?? 4. cout << "Do you want to see all the Port's information?(y/n) "; //最后還是無法輸入。 char infor; cin.get();//我在這里已經(jīng)用這個(gè)把cin >> choice;留下的\n給銷毀了,但是下面一個(gè)讀取還是不能工作。 cin.get(infor); if (infor=='y'||infor=='Y') for (int c=0;c<i;++c) cout << *pointer[c] << endl; else cout << "Bye!\n"; 十分感謝。 |

| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 310求調(diào)劑 +14 | 爭(zhēng)取九點(diǎn)睡 2026-03-30 | 14/700 |
|
|---|---|---|---|---|
|
[考研] 求調(diào)劑求調(diào)劑 +6 | 121. 2026-04-02 | 6/300 |
|
|
[考研] 285求調(diào)劑 +13 | AZMK 2026-04-02 | 13/650 |
|
|
[考研] 265求調(diào)劑11408 +4 | 劉小鹿lu 2026-03-27 | 4/200 |
|
|
[考研] 0805求調(diào)劑 +8 | 是水分 2026-03-31 | 8/400 |
|
|
[考研]
|
廈大化工 2026-04-01 | 5/250 |
|
|
[考研] 070305高分子化學(xué)與物理 304分求調(diào)劑 +14 | c297914 2026-03-28 | 14/700 |
|
|
[考研] 一志愿北京科技,085601總分305求調(diào)劑 +9 | 半生瓜! 2026-04-01 | 11/550 |
|
|
[考研] 生物學(xué)327,求調(diào)劑 +5 | 書上的梅子 2026-04-01 | 6/300 |
|
|
[考研] 08工科275分求調(diào)劑 +13 | AaAa7420 2026-03-31 | 13/650 |
|
|
[考研] 085410 一志愿211 22408分?jǐn)?shù)359求調(diào)劑 +3 | 123456789qw 2026-03-31 | 4/200 |
|
|
[考研] 安全工程 285 求調(diào)劑 +3 | Xinyu56 2026-04-01 | 4/200 |
|
|
[考研] 310分求調(diào)劑 +4 | 成功上岸wang 2026-04-01 | 4/200 |
|
|
[考研] 一志愿中國(guó)科學(xué)院大學(xué)265求調(diào)劑 +8 | 恬淡ye 2026-03-31 | 9/450 |
|
|
[考研] 一志愿北交材料工程總分358 +5 | cs0106 2026-04-01 | 7/350 |
|
|
[考研] 本2一志愿C9-333分,材料科學(xué)與工程,求調(diào)劑 +9 | 升升不降 2026-03-31 | 9/450 |
|
|
[考研] 266分,求材料冶金能源化工等調(diào)劑 +8 | 哇呼哼呼哼 2026-03-27 | 10/500 |
|
|
[考研] 262求調(diào)劑 +7 | ZZ..000 2026-03-30 | 8/400 |
|
|
[考研] 283求調(diào)劑(080500) +14 | A child 2026-03-27 | 14/700 |
|
|
[考研] 藥學(xué)105500求調(diào)劑 +3 | Ssun。。 2026-03-28 | 3/150 |
|