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>
usingnamespace 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();
virtualvoid print() = 0;//pure virtual function
virtualvoid 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()
}