help with classes, base class and dervied class

hi there, below is project requirement, i have just complete the code, but getting error message that 'damage' : is not a member of 'Gun'. PLS Help!!!


1. Create a new project that consists of at least two classes: a base class and a derived class. It should also contain at least
one more class that will be used to instantiate the composite member of either the base or the derived class.
2. The base class and derived classes should have at least one data member and two member functions each in addition to
the constructor. The derived class should have at least one overloaded function and a data member which is a member of
another class (composition).

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;



class Gun //base class
{

// Protected means that only this class and derived classes
// have access to these variables.
protected:

int shotsFired;

int hit;

int miss;

double hitPercent;

string name;

int ammo; // Not static because ammo is individual for each gun

public:
Gun();

Gun(int); // Overloaded constructor

virtual ~Gun(); //virtual destructor



void shoot(int, double, int, int, int);

void reload();
virtual void print() = 0;//pure virtual function
virtual void damager(); //virtual function(no = 0 part)

};


// Define functions for class, notice they are prefixed with

// the class name. These are for "Gun"

Gun::Gun() {


shotsFired = 0;

hit = 0;

miss = 0;

hitPercent = 0.0;

name = "Generic Gun";

ammo = 0;

}



Gun::Gun(int rounds) {
if (rounds >= 0) {
ammo = rounds;

}
}

void Gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired)

{

ammo--;
shotsFired = hit + miss;
hitPercent = hit / shotsFired;

miss = shotsFired - hit;

cout << "Shots: " << shotsFired << endl;

cout << "Hit %: " << hitPercent << endl;

}



void Gun::damage() {



}



Gun::~Gun() { }







class MachineGun : public Gun //example of inheritance & derived class

{

public:

MachineGun();

void print();

};



// MachineGun class constructor, passes 30 rounds to parents

// overloaded constructor. Notice they start with class name.

MachineGun::MachineGun() :Gun(30) {

name = "Machine Gun";

}



void MachineGun::print() {

cout << "Your machine gun has " << ammo << " shots left." << endl;

}





class Pistol : public Gun

{

public:

Pistol();

void print();

};



// Functions for Pistol class. Again, notice they start

// with the class name.

Pistol::Pistol() {

name = "Pistol";

}



void Pistol::print() {

cout << "Your pistol has " << ammo << " shots left." << endl;

}





int main()

{

MachineGun *mGun = new MachineGun(); // Dynamic allocation of pointer

mGun->print(); // Prints number of bullets left

Pistol *mPistol = new Pistol();

mPistol->print();



// Free these pointers memory

delete(mGun);

delete(mPistol);

}




Made it to more readable BY USING code tags (those <> on right hand side of this writing box)

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
#include <iostream> 
#include <string> 
#include <iomanip> 
#include <fstream> 

using namespace std; 

class Gun //base class 
{ 
// Protected means that only this class and derived classes 
// have access to these variables. 
protected:
	int shotsFired; 
	int hit; 
	int miss; 
	double hitPercent; 
	string name; 
	int ammo; // Not static because ammo is individual for each gun 
public:
	Gun(); 
	Gun(int); // Overloaded constructor 
	virtual ~Gun(); //virtual destructor 
	void shoot(int, double, int, int, int);
	void reload();
	virtual void print() = 0;//pure virtual function
	virtual void damager(); //virtual function(no = 0 part)
};

// Define functions for class, notice they are prefixed with 
// the class name. These are for "Gun" 
Gun::Gun() 
{
	shotsFired = 0; 
	hit = 0; 
	miss = 0; 
	hitPercent = 0.0; 
	name = "Generic Gun"; 
	ammo = 0; 
} 

Gun::Gun(int rounds) 
{ 
	if (rounds >= 0) { 
		ammo = rounds;
	} 
}

void Gun::shoot(int ammo, double hitPercent, int hit, int miss, int shotsFired) 
{ 
	ammo--;
	shotsFired = hit + miss; 
	hitPercent = hit / shotsFired; 
	miss = shotsFired - hit; 
	cout << "Shots: " << shotsFired << endl; 
	cout << "Hit %: " << hitPercent << endl; 
} 

void Gun::damage() { } 

Gun::~Gun() { } 

class MachineGun : public Gun //example of inheritance & derived class 
{ 
public: 
	MachineGun(); 
	void print(); 
}; 

// MachineGun class constructor, passes 30 rounds to parents 
// overloaded constructor. Notice they start with class name. 

MachineGun::MachineGun() :Gun(30) 
{ 
	name = "Machine Gun"; 
} 

void MachineGun::print() 
{ 
	cout << "Your machine gun has " << ammo << " shots left." << endl; 
} 

class Pistol : public Gun 
{ 
public: 
	Pistol(); 
	void print(); 
}; 

// Functions for Pistol class. Again, notice they start 
// with the class name. 

Pistol::Pistol()
{ 
	name = "Pistol"; 
} 

void Pistol::print() 
{ 
	cout << "Your pistol has " << ammo << " shots left." << endl; 
} 

int main() 
{ 
	MachineGun *mGun = new MachineGun(); // Dynamic allocation of pointer 
	mGun->print(); // Prints number of bullets left 
	Pistol *mPistol = new Pistol(); 
	mPistol->print(); 
	
	// Free these pointers memory 
	delete(mGun); 
	delete(mPistol);

	return 0; // ALWAYS RETURN SOMETHING FROM int main() 
} 
oh thanks...i haven't run the program, were you able to see the problem
still having the same error.
closed account (DSLq5Di1)
Where is Gun::Damage() declared in your class definition?

1
2
3
4
5
6
7
8
9
10
11
12
13
class Gun
{ 
    ...

public:
	Gun(); 
	Gun(int);
	virtual ~Gun();
	void shoot(int, double, int, int, int);
	void reload();
	virtual void print() = 0;
	virtual void damager(); // is this a typo?
};
oh thanks..this was a typo....other than sloppy is everything else OK.
Topic archived. No new replies allowed.