Help on Project

For my class I'm doing a project which is creating a game. We're spliting it into different parts, one of which is a weapons class.
Assignment 10, basic Weapon class
For this assignment you will write a simple Weapon class and a test program.
The Weapon class should have three private data members: two integers (hit_chance and stamina_required) and a string for the weapon type.
There should be two public member functions: a constructor (which takes 3 arguments to initialize the private data) and a display function which prints out the private data members.
Split the Weapon class into Weapon.h and Weapon.cpp.
Put the main() function in a file named: assignment10.cpp
Write a Makefile to build the application.
You can use this sample code for your main() function:

1
2
3
4
5
6
7
8
#include<iostream>
#include"Weapon.h"
int main()
{
  Weapon w1("Lance", 13, 5); //type, hit_chance, stamina_required
  w1.display();
  return 0;
}


This is what I have for my Weapon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  1 #include<iostream>
  2 using namespace std;
  3 
  4 void Weapon::display(void)
  5 {
  6   cout << "hit chance=" << hit_chance << endl;
  7   cout << "stamina required=" << stamina_required << endl;
  8 
  9 }
 10 
 11 Weapon::Weapon (int stamina_required, int hit_chance)
 12 {
 13 
 14 }
 15 


This is what I have for my Weapon.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
 class Weapon {
  2     private: int hit_chance;
  3              int stamina_required;
  4              string weapon_type;
  5 
  6     public: void display(void);
  7             Weapon()
  8 
  9 }
 10 
~                                                                               
~                                                                               
~                  


and this is what I have as my assignment10:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  1 #include<iostream>
  2 #include"Weapon.h"
  3 using namespace std;  
  4      
  5 int main()
  6  {   
  7    Weapon w1("Lance", 13, 5); // type, hit_chance, stamina_required
  8    w1.display();
  9    return 0;
 10  }
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~             



How would I split up my weapon class in weapon.h and weapon.cpp?
Last edited on
well since the constructor takes 3 arguments, you need 3 not two in the definition and declaration.

the declaration would look like ( i also used default values for these):
 
Weapon(string = "", int = 0, int = 0);


And for the constructor just use the passed in values to set the private data members:
1
2
3
4
5
Weapon::Weapon(string weapon, int stamina, int hit) {
       weapon_type = weapon;
       stamina_required = stamina;
       hit_chance = hit;
}


As for display just add a line to cout the weapon_type and that should be it
Last edited on
where would I put the declaration?

After trying to compile the program I get all of these errors

1
2
3
4
5
6
7
8
9
10
11
In file included from Weapon.cpp:2:
Weapon.h:5: error: 'string' does not name a type
Weapon.h:9: error: expected ')' before 'weapon_type'
Weapon.cpp:3: error: expected unqualified-id before 'using'
Weapon.cpp: In member function 'void Weapon::display()':
Weapon.cpp:7: error: 'cout' was not declared in this scope
Weapon.cpp:7: error: 'endl' was not declared in this scope
Weapon.cpp:9: error: 'weapon_type' was not declared in this scope
Weapon.cpp: At global scope:
Weapon.cpp:12: error: expected ')' before 'weapon'
see@jaguar:~/assignments/a10$ 
Topic archived. No new replies allowed.