多态构造函数

Polymorphism constructor function

本文关键字:构造函数 多态      更新时间:2023-10-16

我的代码编译良好,但我遇到特定部分未显示正确输出的问题。

这是我的基类

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",无论其中存储什么。我以为派生类可以使用基类方法,我错过了什么?任何帮助都会很棒

谢谢Hx

好吧,你的例子与多态性无关。这里的原因是您没有使用任何虚拟函数。以下是您可以使用的代码。

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();
}    

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

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

因为getNamegetNumber是方法。更改为:

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

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

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)
{
}

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

编辑

如果没有,您可能还应该检查处理计数变量的方式......正如其他人指出的那样。