Gun program

trouble implementing functions

Page 1 of 1

7 Replies - 3222 Views - Last Post: 13 October 2009 - 11:49 AM Rate Topic: -----

#1 daddymac1213  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Gun program

Post icon  Posted 11 October 2009 - 11:18 AM

I have to develop a program of my own choice, which includes the following:
*a composite object
*an example of inheritance
*at least one virtual and at least one pure virtual function
*at least one overloaded function
*at least one example of pointers
*at least one example of dynamic memory allocation

I'm pretty sure that I've got this covered, but I'm having trouble implementing my shoot function and damage function. I'm just not really sure where to go from here. How would I implement a fire command? and then apply damage from the gun?

Here is the code I have so far:
#include "stdafx.h"
#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 damage(); //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. 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. They also start
// with the class name.
Pistol::Pistol()
{
	name = "Pistol";
}

void Pistol::print() 
{
	cout << "Your pistol has " << ammo << " shots left." << endl << 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);

	system("PAUSE");
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Gun program

#2 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: Gun program

Posted 11 October 2009 - 11:20 AM

Please post your code inside [ code ] tags and include what kind of errors you recieve.
Was This Post Helpful? 1
  • +
  • -

#3 daddymac1213  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Re: Gun program

Posted 11 October 2009 - 11:36 AM

I don't have any errors, I'm just not sure where to go from here. I wanted to have an output like so:

You fired 30 shots from the machine gun.
20 hit and 10 missed.
The hit% is 2.

Also, I have to construct the main method so that it will test each and every member function of the parent and the child classes.


#include "stdafx.h"
#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 damage(); //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. 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. They also start
// with the class name.
Pistol::Pistol()
{
	name = "Pistol";
}

void Pistol::print() 
{
	cout << "Your pistol has " << ammo << " shots left." << endl << 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);

	system("PAUSE");
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: Gun program

Posted 11 October 2009 - 01:03 PM

Okey, a few points:

In your shoot-function, I would now pass all those variables. Instead I would set hitPercent in the constructor of each gun, for instance could the pistol be slightly more accurate than the machine gun. I would then only pass one int variable to the shoot function. This should tell how many bullets you tried to fire. You can then calculate the amount of bullets that hit/missed and how many bullets are left.

Your damage I am somewhat unsure about. What kind of damage should it calculate?
My suggestion, is to make another variable named damage, in which every gun get a value say 5 for the weak pistol and 20 for the slightly stronger machine gun. Then, at the end of the shoot-function, it would multiply the number of bullets that hit with the damage-multiplyer and return this number.

To fit with these changes, the non-default constructor should now contain three values; ammo, hitPercent and damage.


If my above ramblings makes no sense at all to you, I will be happy to write up some example-code. Just ask.

This post has been edited by Lillefix: 11 October 2009 - 01:03 PM

Was This Post Helpful? 1
  • +
  • -

#5 daddymac1213  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Re: Gun program

Posted 11 October 2009 - 01:19 PM

You make some sense, but I wouldn't mind if you could write something up to show me exactly what you are talking about, I would really appreciate it. The program should be as simple as possible. I'm not trying to make this some big, long code, I just need to get the things that I listed above incorporated into the code. I'm pretty sure that I got most of it, I'm just not sure how to implement the code to get an output similar to this:

You fired 30 shots from the machine gun and 20 hit.
You fired 10 shots from the pistol and 3 hit.
The machine gun dealt 25 points worth of damage.
The pistol dealt 3 points of damage.
Your hit percentage is 36%.

For the machine gun I figured it would deal anywhere between 1 and 4 points worth of damage and the pistol would just be 1. The bullets would hit randomly from both guns.

This post has been edited by daddymac1213: 11 October 2009 - 01:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: Gun program

Posted 11 October 2009 - 02:10 PM

I would have done it like this, but I might have changed the code too much to meet your instructors criterias. Anyway I hope you are able to learn something from this.
#include <iostream>

using namespace std;

class Gun
{

protected:	
	string name;	
	int ammo;
	double damageModifier;
	double hitPercent;
	
public:	
	Gun();	 
	Gun(int, double, double);
	virtual ~Gun();  
	void shoot(int);
	virtual void print();
	virtual void damage(int) = 0;
};

Gun::Gun()
{		   
	name = "Generic Gun";	
	ammo = 0;
	hitPercent = 0.0;  
	damageModifier = 0.0;
}

Gun::Gun(int _ammo, double _hitPercent, double _damageModifier) 
{
	if (_ammo >= 0)
		ammo = _ammo;		
	if (_hitPercent >= 0 && _hitPercent <= 1)
		hitPercent = _hitPercent;
	if (_damageModifier >= 0)
		damageModifier = _damageModifier;
}

void Gun::shoot(int shots)
{   
	if (shots > ammo)
		shots = ammo;
		
	ammo -= shots;
	
	int hit = shots * hitPercent;
	int missed = shots - hit;
	
	cout << endl;
	cout << "You fired " << shots << " bullets with your " << name << endl; 
	cout << hit << " bullets hit their target" << endl; 
	cout << missed << " bullets missed their target" << endl; 
	
	damage(hit);
	
	if (ammo == 0)
		cout << "You are out of bullets" << endl;
	cout << endl;
}
void Gun::print()
{
	cout << "Your " << name << " has " << ammo << " bullets left" << endl;
}

Gun::~Gun()
{
}

class MachineGun : public Gun
{
	public:	  
		MachineGun();	
		void damage(int);
};

MachineGun::MachineGun() : Gun(100, 0.2, 20) 
{ 
	name = "Machine Gun";
}

void MachineGun::damage(int bulletsHit) //bullet-shit :p
{
	double totalDamage = damageModifier * bulletsHit;
	cout << "All the the bullets you sprayed towards your target did a total of " << totalDamage << " damage" << endl;
}

class Pistol : public Gun
{
public:	  
	Pistol();	
	void damage(int);
};

Pistol::Pistol() : Gun(10, 0.50, 5)
{
	name = "Pistol";
}

void Pistol::damage(int bulletsHit) //bullet-shit :p
{
	double totalDamage = damageModifier * bulletsHit;
	cout << "Your weak pistol-bullets did a total of " << totalDamage << " damage" << endl;
}

int main()
{
	MachineGun *mGun = new MachineGun();	// Dynamic allocation of pointer			   
	Pistol *mPistol = new Pistol();			   
	mGun->print();							// Prints number of bullets left 
	mPistol->print();
	mGun->shoot(40);						//Fire some rounds
	mPistol->shoot(15);
	mGun->print();							// Prints number of bullets left 
	mPistol->print();
	delete(mGun);							//Free these pointers memory	
	delete(mPistol);
	
	return 0;
}

Was This Post Helpful? 1
  • +
  • -

#7 daddymac1213  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 16-November 08

Re: Gun program

Posted 11 October 2009 - 02:54 PM

That looks good to me. The only problem that I have is the warning (warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data) on this line:

damage(hit);



Also, how would I go about adding an overloaded function?

Thanks again Lilliefix for all of your help.

This post has been edited by daddymac1213: 11 October 2009 - 03:23 PM

Was This Post Helpful? 0
  • +
  • -

#8 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: Gun program

Posted 13 October 2009 - 11:49 AM

As for the warning, I'm taking responsibility. To remove it, change this line:
virtual void damage(int) = 0; to
virtual void damage(double) = 0;

and this line:
void MachineGun::damage(int bulletsHit) to
void MachineGun::damage(double bulletsHit)

and this line:
void Pistol::damage(int bulletsHit) to
void Pistol::damage(double bulletsHit)

That way, the variable is always a double, and no information is.


As for the overloaded function, one could argue that the constructor is overloaded. If you, however, want to create another overloaded function, you could for example create another shoot-function, where you supply the hitPercent and/or damage. You know, for those time you take your time to aim for the head or the groin :

Something like this:
void Gun::shoot(int shots, double newHitPercent, double newDamage);

This post has been edited by Lillefix: 13 October 2009 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1