bemused and confused
bemused and confused

Reputation: 113

Polymorphism constructor function

My code compiles fine but I am having issues with a particular section not displaying the correct output.

Here is my base class

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

and here is my derived class:

 class Bed : public Item
 {
 private:

string frame;
string frameColour;
string mattress;

 public:

 Bed();

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

Function definitions:

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

Main section that is causing the problem:

 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++;

when the programme executes, the cout within the constructor shows the correct output, but when I call getname and getnumber from the main function the programme prints '1' for both , no matter what's stored in there. I thought that derived classes could use base class methods, what have I missed? Any help would be great

thanks Hx

Upvotes: 1

Views: 5171

Answers (4)

besworland
besworland

Reputation: 765

Well, you example is not related to polymorphism. The reason here is that you are not using any virtual functions. Here is the code that you can use.

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

Upvotes: 2

hmjd
hmjd

Reputation: 121981

This looks incorrect (and I am unsure how this even compiled):

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

as getName and getNumber are methods. Change to:

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

Additionally, the initialisation of count is not posted: ensure it is zero.

Upvotes: 1

Amit
Amit

Reputation: 13374

You have not called the base class's constructor from the derived class... Where as it should be the first line... update the code, I am sure it will start working..

EDIT

If it doesn't than you should probably also check the way you are handling count variable... as pointed out by others..

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258618

count appears to be the size of your vector. After you push back the last element, you're not incrementing count, so you're printing an older element.

Why don't you try:

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

Also, you should start using initialization lists in your constructors:

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

Upvotes: 1

Related Questions