help with virtual print function

Hi, for my assignment I needed to create a virtual print function in my base class.

Eveything seems to work except the printing part.

When it loops through the array and prints, it's outputting like this:

Manufacturer: Winchester
Caliber: .38
Grips: side
Sigts: iron
Laser: true
Single Action: Single Action
Number of Rounds: 500
Semi auto: true


Press any key to continue . . .


It seems to be combining all 3 guns into 1 gun.

Is there a way to keep the guns separate?

Thanks!

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

#pragma warning(disable: 4996)
#include <cstdlib>
#include <iostream>
#include <string>
#include <cctype>
#include <stack>
#include <conio.h>

using namespace std;

// *** Class definitions ***

//base class
class Gun {

protected:
	string g_manufacturer;
	string g_caliber;
	//string gunCabinet[2];
public:
	Gun();
	Gun(const string manufacturer, const string caliber) {
		g_manufacturer = manufacturer;
		g_caliber = caliber;
	}
	
	string getManufactuer() {
		return g_manufacturer;
	}

	string getCaliber() {
		return g_caliber;
	}

	void setManufactuer(string m);
	void setCaliber(string c);
	virtual void print();
};

class Handgun : public Gun {
protected:
	string h_grips;
	string h_sights;
	bool h_laser;

public:
	Handgun();
	Handgun(const string manufacturer, const string caliber, const string grips, const string sights, const bool laser)
	: Gun(manufacturer, caliber) {
		h_grips = grips;
		h_sights = sights;
		h_laser = laser;
	};

	string getGrips() {
		return h_grips;
	}

	string getSights() {
		return h_sights;
	}

	bool getLaser() {
		return h_laser;
	}

	void print();
	void setGrips(string g);
	void setSights(string s);
	void setLaser(bool l);
};

class Revolver : public Handgun {
private:
	bool r_singleAction;
	int r_numberOfRounds;

	/*Handgun(const string manufacturer, const string caliber, const string g, const string s, const bool l)
		: Gun(manufacturer, caliber) {
		grips = g;
		sights = s;
		laser = l;
	};*/

public:
	Revolver();
	Revolver(const string manufacturer, const string caliber, const string grips, const string sights, const bool laser, bool singleAction, int numberOfRounds)
		: Handgun(manufacturer, caliber, grips, sights, laser) {
		r_singleAction = singleAction;
		r_numberOfRounds = numberOfRounds;
	}

	string getSingleAction();

	int getNumberOfRounds() {
		return r_numberOfRounds;
	}

	void print();
	void setSingleAction(bool singleAction);
	void setNumberOfRounds(int numberOfRounds);
};

class Pistol : public Handgun {
private: 
	bool p_semiAuto;

public:
	Pistol();
	Pistol(const string manufacturer, const string caliber, const string grips, const string sights, const bool laser, bool semiAuto)
		: Handgun(manufacturer, caliber, grips, sights, laser) {
		p_semiAuto = semiAuto;
	}

	bool getSemiAuto() {
		return p_semiAuto;
	}

	void print();
	void setSemiAuto(bool semiAuto);
};

//main
int main()
{
	Handgun myHandgun("Winchester", ".38","side","iron",true);

	Revolver myRevolver("HK", ".45", "top", "iron", true, true, 500);
	
	Pistol myPistol("Smith & Wesson", ".50", "top", "scope", true, true);

	Gun *myGunCabinet[3];

	myGunCabinet[0] = &myHandgun;
	myGunCabinet[1] = &myRevolver;
	myGunCabinet[2] = &myPistol;


	for (int i = 0; i < 3; i++)
		myGunCabinet[i] -> print();

	//need this so the DOS window doesn't close
	cout << "\n\n";
	system("pause");
	return 0;
}

//Class Implementations

void Gun::print() {
	cout << "";
}

void Handgun::print() {
	cout << "\nManufacturer: " << getManufactuer() << "\n";
	cout << "Caliber: " << getCaliber() << "\n";
	cout << "Grips: " << getGrips() << "\n";
	cout << "Sigts: " << getSights() << "\n";
	cout << "Laser: " << boolalpha << getLaser() << "\n";
}

void Revolver::print() {
	cout << "Single Action: " << getSingleAction() << "\n";
	cout << "Number of Rounds: " << getNumberOfRounds() << "\n";
}

void Pistol::print() {
	cout << "Semi auto: " << boolalpha << getSemiAuto() << "\n";
}

string Revolver::getSingleAction() {
	if (r_singleAction) {
		return "Single Action";
	}
	else {
		return "Not Single Action";
	}
}
Last edited on
A virtual declared function should remian virtual throughout inheritance. In your derived classes, try declaring print() as virtual just as you did in your base class.
Last edited on
Once declared virtual in a base class, the function is automatically virtual in all derived classes. There is no need to redeclare the function as virtual in the derived class.

(But it doesn't hurt to do so. I tend to do so as it makes it clearer to me that the function is virtual. And sometimes I even comment it in the derived classes, so I know it was declared virtual in a parent class, e.g. /*virtual*/ void print(); )

Your output looks fine to me. As your array contains one of each gun, what else do you expect? What you could do with is a blank line between the print calls...

1
2
3
4
5
    for (int i = 0; i < 3; i++)
    {
        myGunCabinet[i] -> print();
        cout << "\n"; // blank line!
    }


... so your output looks like this?

Manufacturer: Winchester
Caliber: .38
Grips: side
Sigts: iron
Laser: true

Single Action: Single Action
Number of Rounds: 500

Semi auto: true


Andy

PS If you want to be careful with your virtual functions, and you're using a C++11 compliant compiler, you could mark the derived versions of the virtual functions with the "override" (or even "final") specifier.
Last edited on
Topic archived. No new replies allowed.