24小时热门版块排行榜    

CyRhmU.jpeg
查看: 1911  |  回复: 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 ]
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

获得知识的唯一办法,就是靠青春去换取。
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

lhy317409772

银虫 (初入文坛)

引用回帖:
4楼: Originally posted by lhy317409772 at 2014-01-12 23:00:23
非常感谢,问题已经解决了绝大部分....
这里还有一些疑问,希望能得到解决:
1.如何发代码帖子,我的符号变成表情了,注意到您的代码外面穿了马甲:code,请问怎么做到的....我是新人,嘿嘿。
2.Port && ...

呃...另外.....最后一个输出所有信息,我发觉似乎有问题...我用的是
<<*pointer[c] ,这里所有的都应该调用的是<< (Port),所以我的VintagePort的信息估计会显示不完全...这里有没有办法能够解决??
获得知识的唯一办法,就是靠青春去换取。
5楼2014-01-12 23:12:01
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 9 个回答

lhy317409772

银虫 (初入文坛)

里面的符号有的变成了表情....汗....
   但是直接复制的话,应该是可以变回来的吧!?
获得知识的唯一办法,就是靠青春去换取。
2楼2014-01-08 22:50:57
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

木叶清风

木虫 (正式写手)

【答案】应助回帖

★ ★ ★ ★ ★
感谢参与,应助指数 +1
lhy317409772: 金币+5, ★★★★★最佳答案, 已经解决了大部分问题。 2014-01-12 23:03:16
CODE:
#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::ostream& operator<< (std ::ostream& 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::ostream& operator<< (std::ostream& os, const VintagePort& vp);
};

CODE:
//实现文件
#include<iostream>
#include<cstring>
#include"Port.h"

Port::Port(const char* br, const char* st, int b)
{
        brand = new char[strlen(br) + 1];
        strcpy(brand, br);
        strncpy(style, st, 20);
        bottles = b;
}

Port::Port(const Port& p)
{
        brand = new char[strlen(p.brand) + 1];
        strcpy(brand, p.brand);
        strncpy(style, p.style, 20);
        bottles = p.bottles;
}

Port& Port::operator = (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::operator += (int b)
{
        bottles += b;
        return *this;
}

Port& Port::operator -= (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::ostream& operator<< (std::ostream& os, const Port& p)
{
        os << p.brand << ", " << p.style << ", " << p.bottles;
        return os;
}

VintagePort::VintagePort()
:Port()
{
        nickname = new char[5];
        strcpy(nickname, "none");
        year = 0;
}

VintagePort::VintagePort(const char* br, int b, const char* nn, int y)
:Port(br, "vintage", b)
{
        nickname = new char[strlen(nn) + 1];
        strcpy(nickname, nn);
        year = y;
}

VintagePort::VintagePort(const VintagePort& vp)
:Port(vp)
{
        nickname = new char[strlen(vp.nickname) + 1];
        strcpy(nickname, vp.nickname);
        year = vp.year;
}

VintagePort& VintagePort::operator = (const VintagePort& vp)
{
        Port::operator = (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::ostream& operator<< (std::ostream& os, const VintagePort& vp)
{
        os << (const Port&) vp;
        os << ", " << vp.nickname << ", " << vp.year;
        return os;
}

CODE:
//主程序
#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);
        Port pa = Port("Lafite", "tawny", 27);
        Port pb = Port("Latour", "ruby", 23);
        VintagePort va = VintagePort("Haut-Brion", 35, "The Noble", 1875);
        VintagePort vb = VintagePort("Margaux", 56, "Old Velvet", 1898);
        pointer[0] = &pa; // 只能对左值取地址,像你那样取地址是非法的,其实Port的析构函数都已经调用
        pointer[1] = &pb;
        pointer[2] = &va;
        pointer[3] = &vb;

        for (int k = 0; k<4; k++)
        {
                pointer[k]->Show();
                cout << endl;
        }

        //Port& b = Port("Lafite", "tawny", 27);
        Port prefb = Port("Lafite", "tawny", 27);
        Port &b = prefb;
        // 或者你可以直接用Port &&b = Port("Lafite", "tawny", 27);
        // 或者const Port &b = Port("Lafite", "tawny", 27);
        // 只有常量引用和右值引用可以绑定在一个临时变量上,你看到没给你报错不代表你的写法
        // 就真的没有错误,只是微软的编译器没有支持标准而已
        b.Show();
        cout << b << endl;

        Port&& a = VintagePort("Margaux", 56, "Old Velvet", 1898);//同上
        a.Show();
        cout << a << endl;// 没明白你这怎么重载了,operator(ostream, const Port&)直接从非const到const转换的匹配,没有任何问题
       
        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:";// << endl;    //这里我无法进行输入。
                        char temp[50];
                        cin.get();
                        cin.getline(temp, 50);// 你原始的做法是直接获取新的一行,当然得到的就是'\n',没有什么不能输入的
                        //cin.get();
                        if (strcmp(temp,"Lafite") == 0)     //我这里的字符串的比较应该是有问题的,不能用==判断。
                        {
                                cout << "Save bottles: ";
                                cin >> num;
                                *pointer[0] += num;
                                pointer[0]->Show();
                        }
                        else if (strcmp(temp,"Latour") == 0)
                        {
                                cout << "Save bottles: ";
                                cin >> num;
                                *pointer[1] += num;
                                pointer[1]->Show();
                        }
                        else if ( strcmp(temp,"Haut-Brion") == 0)
                        {
                                cout << "Save bottles: ";
                                cin >> num;
                                *pointer[2] += num;
                                pointer[2]->Show();
                        }
                        else if ( strcmp(temp,"Margaux") == 0)
                        {
                                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 ( strcmp(s_temp,"vintage") == 0)       //这里的判断也有问题。
                                {
                                        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[5] =  new VintagePort(temp, b_temp, n_temp, y_temp);//不知道你想用哪一个pointer,暂时用5,自己改
                                }
                                else
                                {
                                        cout << "Enter the bottles: ";
                                        int b_temp;
                                        cin >> b_temp;
                                        pointer[5] = new Port(temp, s_temp, b_temp);//不知道你想用哪一个pointer
                                }
                        }
                }
                ++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;

}

www.cvdelver.com
3楼2014-01-09 22:29:39
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

lhy317409772

银虫 (初入文坛)

引用回帖:
3楼: Originally posted by 木叶清风 at 2014-01-09 22:29:39
#include<iostream>

class Port
{
private:
        char* brand;
        char style;
        int bottles;
public:
        Port(const char* br = "none", const char* st = "none", int b = 0);
        Por ...

非常感谢,问题已经解决了绝大部分....
这里还有一些疑问,希望能得到解决:
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
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
信息提示
请填处理意见