| 5 | 1/1 | 返回列表 |
| 查看: 2084 | 回復(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è)不是主程序的部分,我是想用來測試,我用基類指針調(diào)用Show()函數(shù)的結(jié)果, //結(jié)果與我預(yù)想的不一樣,我的Show()函數(shù)是虛擬的,我用基類指針(Port*)指向派類對象 { 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); //我這里用基類引用指向基類對象,用引用調(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(); //還有這里,我用基類引用指向派生對象,在調(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 ] |

木蟲 (正式寫手)

銀蟲 (初入文壇)

銀蟲 (初入文壇)
|
非常感謝,問題已經(jīng)解決了絕大部分.... 這里還有一些疑問,希望能得到解決: 1.如何發(fā)代碼帖子,我的符號(hào)變成表情了,注意到您的代碼外面穿了馬甲:code,請問怎么做到的....我是新人,嘿嘿。 2.Port &&b = Port("Lafite", "tawny", 27); 這里好像有錯(cuò)誤,報(bào)錯(cuò) 引用無法賦值給引用..... 您說,只有常量引用和右值引用可以綁定在一個(gè)臨時(shí)變量上,我寫的是Port& b = Port("Lafite", "tawny", 27);我把左值引用綁定在了臨時(shí)變量上(請問,您說是不規(guī)范的,這里是為什么不規(guī)范?),const Port &b = Port("Lafite", "tawny", 27);這個(gè)是把常量引用綁定在臨時(shí)變量上,但是請問一下把右值引用綁定在臨時(shí)變量上是什么例子? 3. pointer[5] = new VintagePort(temp, b_temp, n_temp, y_temp); 這里為什么要new一個(gè)VintagePort? 還是因?yàn)橹荒軐ψ笾颠M(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ā)表 | |
|---|---|---|---|---|
|
[考研] 求生物學(xué)調(diào)劑 +9 | 15172915737 2026-04-01 | 9/450 |
|
|---|---|---|---|---|
|
[考研] 282求調(diào)劑 +18 | ycy1201 2026-04-01 | 20/1000 |
|
|
[考研] 318求調(diào)劑 +3 | 篤行致遠(yuǎn). 2026-03-31 | 4/200 |
|
|
[考研] 309求調(diào)劑 +8 | 呆菇不是戴夫 2026-04-02 | 8/400 |
|
|
[考研] 理學(xué)07化學(xué) 303求調(diào)劑 +16 | 睿08 2026-03-27 | 17/850 |
|
|
[考研] 考研調(diào)劑0702 +3 | 梅子味晚霞 2026-03-28 | 3/150 |
|
|
[考研]
|
shzhou_ 2026-04-01 | 6/300 |
|
|
[考研] 332求調(diào)劑 +8 | Lyy930824@ 2026-03-29 | 8/400 |
|
|
[考研] 086000生物與醫(yī)藥 初試274求調(diào)劑 +5 | 小叮當(dāng)來了 2026-03-30 | 6/300 |
|
|
[考研] 生物學(xué)296求調(diào)劑 +10 | 湯圓包 2026-03-29 | 14/700 |
|
|
[考研] 一志愿北京科技大學(xué)085601材料工程英一數(shù)二初試總分335求調(diào)劑 +5 | 雙馬尾痞老板2 2026-03-31 | 5/250 |
|
|
[考研] 289求調(diào)劑 +7 | BrightLL 2026-03-29 | 7/350 |
|
|
[考研] 375求調(diào)劑 +7 | 雨夏整夜 2026-03-29 | 7/350 |
|
|
[考研] 343求調(diào)劑 +8 | 愛羈絆 2026-03-28 | 8/400 |
|
|
[考研] 266分,求材料冶金能源化工等調(diào)劑 +8 | 哇呼哼呼哼 2026-03-27 | 10/500 |
|
|
[考研] 調(diào)劑求院校招收 +7 | 鶴鯨鴿 2026-03-28 | 7/350 |
|
|
[考研] 085600材料與化工調(diào)劑 +16 | kikiki7 2026-03-30 | 16/800 |
|
|
[考研] 0703化學(xué)321分求調(diào)劑 +10 | 三dd. 2026-03-30 | 11/550 |
|
|
[考研] 304求調(diào)劑 +6 | 曼殊2266 2026-03-27 | 6/300 |
|
|
[考研] 292求調(diào)劑 +4 | 求求了收下我吧?/a> 2026-03-26 | 4/200 |
|