Important alert: (current site time 7/15/2013 6:11:40 PM EDT)
 

VB icon

playerClass.cpp

Email
Submitted on: 12/11/2007 11:51:16 AM
By: Garrett Quinn 
Level: Intermediate
User Rating: By 3 Users
Compatibility: C++ (general)
Views: 6829
 
     I made this neat class for a text adventure I was going to create while I'm not programming for homework. Some private data types and member functions that will manipulate these data members make up this fairly simple class. If it does nothing else I hope it can help out some new people or give coders an idea, or place to start. Thanks for checking it out!
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :playerClass.cpp
//**************************************
#include <iostream>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: playerClass.cpp
// Description:I made this neat class for a text adventure I was going to create while I'm not programming for homework. Some private data types and member functions that will manipulate these data members make up this fairly simple class. If it does nothing else I hope it can help out some new people or give coders an idea, or place to start.
Thanks for checking it out!
// By: Garrett Quinn
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=11850&lngWId=3//for details.//**************************************

//Garrett Quinn, December 9th 2007
/*************************|
| An simple example class |
| to show how player |
| information in a game|
| can be put into one |
| convenient class.|
**************************/
class Player
{
private:
string name;
int health;
int numOfPotion;
public:
//Get the players name...
void getName( )
{
cout << "What will your hero's name be?\n\n";
cin >> name;
}//getName
//Set players health to 10...
void startHealth( )
{
health = 10;
}//startHealth
//Set number of potions player has at start...
void startNumOfPotion( )
{
numOfPotion = 0;
}//startNumOfPotion
//Print the players name and health...
void printStats( )
{
cout << "Name: " << name << endl;
cout << "Health: " << health << endl;
}//printStats
//Set players health after losing health...
void loseHealth( int i )
{
health -= i;
printStats( );
if( health <= 0 )
{
cout << "You have died...\n";
cout << "GAME OVER\n";
}
}//loseHealth
//Set players health after gaining health...
void gainHealth( int j )
{
health += j;
printStats( );
}//gainHealth
//Player uses potion (k is number of potions they want to use...)
void usePotion( int k )
{
//Potion regains 3 health...
int healthGain = 3;
//Make sure player has potions left...
if( numOfPotion <= 0 )
{
cout << "You don't have any potions left.\n";
}
else if( numOfPotion >=1 )
{
numOfPotion -= k;
k *= healthGain;
health += k;
printStats( );
}
}//usePotion
};//Player


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

12/24/2007 1:23:49 AMBrenton A. Saunders

It's a good start for beginner C++ programmers! [=
(If this comment was disrespectful, please report it.)

 
1/18/2008 5:24:27 PMAric Holland

*sigh* good for beginners but not exactly contest material...
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.