How can I access the virtual base class? This is a practice exercise from c++ primer plus 6. Also if you find any flaws in the code please comment on that.
The problem is that the name becomes No Name instead of the name specified when creating the gunslinger, I don't know how I can call the virtual base class explicitly
Output,
Name, No Name
... (everything works fine from this point on)
The Data() method is just the Show(), except only with the details on that particular class.
You mean the main function? I know what the problem is and it is not in main, I just have to find out a way to solve it. In the main I initialize an object with a name and initialize all its variables. The only thing that doesn't initialize correctly is the name in the virtual base class. I am not sure how to access it if it is through the poker class or through gunslinger since there is only one base class. I should not have to do any modifications to the main function to make this program work.
class badguy : publicvirtual person, // just documenting that person is a virtual base class
public gunslinger, public pokerplayer
{
public:
badguy(string name = "No Name", double draw = 10, int notch = 0)
: person(name), // **** initialize virtual base class - this is required
gunslinger(name, draw, notch) {}
// ...
};
That didn't do anything naraku... But thank you JLBorges , I ended up having to derive a public virtual person class. The exercise didn't tell me I could do that but I guess it is necessary.
class badguy : // public virtual person, // this is not essential;
// person *is* a virtual base class of badguy even without it
public gunslinger, public pokerplayer
{
public:
badguy(string name = "No Name", double draw = 10, int notch = 0)
: person(name), // this is required;
// without it, person would be default-constructed
gunslinger(name, draw, notch) {}
// ...
};