How to access the virtual base class?

closed account (ETAkoG1T)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef PERSON_H_
#define PERSON_H_

#include <string>
#include <iostream>
#include <cstdlib>
using std::string;

class person
{
private: 
	string fullname;
public:
	person(string name = "No Name") { fullname = name; }
	virtual void Show() { std::cout << "name, " << fullname << std::endl; }
};

class gunslinger : virtual person
{
private:
	double draw_time;
	int notches;
public:
	gunslinger(string name = "No Name", double draw = 10, int notch = 0) 
		: person(name), draw_time(draw), notches(notch) { }
	double Draw() { return draw_time; }
	virtual void Show();
	virtual void Data();
};

class pokerplayer : virtual person
{
public:
	pokerplayer(string name = "No Name") : person(name) { }
	int Draw() { return rand() % 52 + 1; }
	void Show() { person::Show(); }
	virtual void Data() { }
};

class badguy : public gunslinger, public pokerplayer
{
public:
	badguy(string name = "No Name", double draw = 10, int notch = 0)
		: gunslinger(name, draw, notch) { }
	int Cdraw() { return pokerplayer::Draw(); }
	double Gdraw() { return gunslinger::Draw(); }
	void Show() 
	{
		pokerplayer::Show();
		gunslinger::Data();
	}
};

#endif 
Last edited on
¿client code?
closed account (ETAkoG1T)
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 gunslinger : public virtual person
1
2
3
4
5
6
7
8
9
10
class badguy : public virtual 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) {}
       
        // ...
};
closed account (ETAkoG1T)
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.
1
2
3
4
5
6
7
8
9
10
11
12
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) {}
       
        // ...
};
closed account (ETAkoG1T)
Yea that works too :)
Topic archived. No new replies allowed.