1

我的代码编译得很好,但我遇到了一个特定部分没有显示正确输出的问题。

这是我的基类

class Item
 {
 protected:

//int count;
string model_name;
int item_number;

 public:

Item();
Item(string name, int number);
     string getName(){return model_name;}
int getNumber(){return item_number;}

这是我的派生类:

 class Bed : public Item
 {
 private:

string frame;
string frameColour;
string mattress;

 public:

 Bed();

 Bed(int number, string name, string frm, string fclr, string mtres);

函数定义:

 Bed::Bed(int number, string name, string frm, string fclr, string mtres)
{
model_name=name;
item_number=number;
frame=frm;
frameColour=fclr;
mattress=mtres;
cout<<model_name<<item_number<<frame<<frameColour<<mattress<<endl;
}

导致问题的主要部分:

 Item* item= new Bed(number, name, material, colour, mattress);
 cout<<"working, new bed"<<endl;
 v.push_back(item);
 cout<<"working pushback"<<endl;
 cout<<" this is whats been stored:"<<endl;
 cout<<v[count]->getName<<endl;
 cout<<v[count]->getNumber<<endl;
 count++;

当程序执行时,构造函数中的 cout 会显示正确的输出,但是当我从 main 函数调用 getname 和 getnumber 时,无论其中存储了什么,程序都会为两者打印 '1'。我认为派生类可以使用基类方法,我错过了什么?任何帮助都会很棒

谢谢你

4

4 回答 4

2

好吧,您的示例与多态性无关。这里的原因是您没有使用任何虚拟功能。这是您可以使用的代码。

class Item
{
protected:

    std::string model_name;
    int item_number;

public:

    Item();
    Item(std::string& name, int number) : model_name(name), item_number(number) {};
    std::string getName(){return model_name;}
    int getNumber(){return item_number;}
};

class Bed : public Item
{
private:

    std::string frame;
    std::string frameColour;
    std::string mattress;

public:

    Bed();

    Bed(int number, std::string& name, std::string& frm, std::string& fclr, std::string& mtres) : Item(name, number), 
                                                                                                  frame(frm),
                                                                                                  frameColour(fclr), 
                                                                                                  mattress(mtres) {};
};

int main()
{
    int count = 0;
    std::vector<Item*> v;

    Item* item = new Bed(2, std::string("MyBed"), std::string("wood"), std::string("red"), std::string("soft"));
    std::cout << "working, new bed" << std::endl;
    v.push_back(item);

    std::cout << "working pushback" << std::endl;
    std::cout << " this is whats been stored:" << std::endl;
    std::cout << v[count]->getName() << std::endl;
    std::cout << v[count]->getNumber() << std::endl;

    ++count;

    getchar();
}    
于 2012-05-02T12:53:12.703 回答
1

这看起来不正确(我不确定它是如何编译的):

cout<<v[count]->getName<<endl;
cout<<v[count]->getNumber<<endl; 

asgetNamegetNumberare 方法。改成:

cout<<v[count]->getName()<<endl;
cout<<v[count]->getNumber()<<endl;

此外,count未发布的初始化:确保它为零。

于 2012-05-02T12:49:21.010 回答
1

count似乎是您的vector. 推回最后一个元素后,您并没有增加count,因此您正在打印一个较旧的元素。

你为什么不试试:

cout<<v[v.size()-1]->getName<<endl;
cout<<v[v.size()-1]->getNumber<<endl;

此外,您应该开始在构造函数中使用初始化列表:

Bed::Bed(int number, string name, string frm, string fclr, string mtres) :
  Item(name,number),
  frame(frm),
  frameColour(fclr),
  mattress(mtres)
{
}
于 2012-05-02T12:37:09.720 回答
0

你还没有从派生类中调用基类的构造函数......它应该是第一行......更新代码,我相信它会开始工作......

编辑

如果不是这样,您可能还应该检查您处理计数变量的方式......正如其他人所指出的......

于 2012-05-02T12:37:36.833 回答