多态构造函数
Polymorphism constructor function
我的代码编译良好,但我遇到特定部分未显示正确输出的问题。
这是我的基类
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;
因为getName
和getNumber
是方法。更改为:
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)
{
}
您尚未从派生类调用基类的构造函数...它应该是第一行...更新代码,我相信它会开始工作..
编辑
如果没有,您可能还应该检查处理计数变量的方式......正如其他人指出的那样。
相关文章:
- 多态二进制函数
- 对象实例化调用构造函数的次数太多
- Doees the 'this' 指针参与虚函数的多态行为
- 如何在基类指针向量的元素上应用重载的多态函数
- C++:使用运算符 = 调用多参数构造函数
- 为什么删除的复制构造函数不允许使用其他具有多态类型的构造函数?
- C++ 多态构造函数错误;标识符未定义
- 多态性是否适用于值?或者在按(基)值返回时使用派生类的移动构造函数
- C 多态性实例的构造函数和破坏者
- 使用反向引用移动多态成员的构造函数
- 有没有办法破坏然后移动构造一个多态基类
- 具有引用数据类型的函数多态性
- 多态类中没有隐式复制构造函数
- 多态构造函数
- 在 C++11 中,是否可以将 ref'ed 基类传递给线程的构造函数并获得多态行为?
- C++多态性使用子构造函数
- 具有类型转换的多态复制构造函数
- 模板函数多态性
- 基类和派生类构造函数多态性问题
- 模板大小递归--构造函数多个重载