Hey guys I just started programing about a week ago. I am teaching myself out of a few books from the local Lib.
I am trying to under stand classes. I am trying to make a basic program that uses a class. The book I am using made a class called bow and it is suppose to be able to fire (generates a random number 1-100).
#include <iostream>
#include <ctime>
using namespace std;
// create bow class
class Bow
{
bow(); //constructor
int fire(); // method
~bow();//deconstructor
};
// fire the bow
int Bow::fire()
{
int score; // define variable
score = rand() % ( 100 - 0 + 1 ) + 0; //generates random number
return score; // returns the score
}
//_________________________________________________________________________
// Main Program
//_________________________________________________________________________
int main()
{
cout << " The bow fires......the score is "; cout << bow.fire();
return 0;
}
//_________________________________________________________________________
I am not able to complie this because I am not sure how to implement the class properly. If you could excuse the simple syntax error.
What am I doing wrong?
This is my first posting. Thank you for your time.
You have some problems:
Constructor and destructor are lowercase 'bow' but they must have the same name as the class, which has capital 'B'
Neither the constructor or destructor are defined, they are only declared. If you don't need them, don't declare them and the compiler would provide your class with the defaults
You are calling the method 'fire' without declaring its object.
You should have something like this:
1 2
Bow bow; // 'Bow' is the class and 'bow' is the object, you declare it as with basic types
bow.fire(); // call 'fire' from the object 'bow'