Page 1 of 1

Noob Question

Posted: Mon Oct 26, 2015 4:10 pm
by ghost_tech81
Hi guys,

Just a quick question concerning C++. I have been writing some code. However I have just realised that as I am writing several classes, it will probably make more sense to have my code organised into Header files.

Can someone please give me some guidance here as I am unsure what I should be including where.

Also do I need to have my class implementations all on the same source file? Or can I code the implementations separately on a different .cpp file for each class?

If it make any difference I am using VC++ 11.

I just feel that my code is a mess and not organised properly at the moment. Plus as far as testing classes goes, I just want to include the specific class I want to test in main.cpp for now.

Cheers in advance.

Code: Select all

#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>

using namespace std;

class textParse
{
public:
	void playerInput(string);
};

class Combat
{
};

class MonsterSpawn
{
};

class PlayScreen
{
};

class PlayWorld
{
public:

	void PrintPage();
	void PrintMonsters();
	void PrintItems();
};

class Player_char
{
	
	int intCombatSkill;
	int intWillPower;
	int intEndurance;
	bool Is_alive;
	string strName;
	
	//Use Boolean Values for classes of magic 

	bool hasSorcery;
	bool hasEnchantment;
	bool hasElementalism;
	bool hasAlchemy;
	bool hasProphecy;
	bool hasPsychomancy;
	bool hasEvocation;

	//Inventory locations

	string Backpack[7];
	string Pouch[4];
	string weapons[2];

	int Nobles;				//Currency

	
	
public:

	void generateChar();                 //In Progress, need to learn liked lists 
	int check_alive();					//complete
	int attack();			
	void takeDamage (int);				//complete
	void print_stats();
};

void Player_char::generateChar()
{
  
  Is_alive = 1;	// Player is alive, always a good start
  Nobles = 0;	//Start off with no money
  cout << endl;
  cout << "Please enter name for Hero:  ";
  getline(cin, strName) ;
  cout << endl << "Greetings " << strName << endl;

  cout << endl << "Generating Statistics" << endl;

  // Generate Combat Skill,, Will Power and Endurance. Use randomm number generator. Then use Lone Wolf Character Generation rules.

  srand (time(NULL));
  intCombatSkill = rand() % 9 + 10;
  intWillPower = rand() % 9 + 20;
  intEndurance = rand() % 9 + 20;

  cout << endl << strName << "  Please see your stats below: " << endl;
  cout << "Combat Skill: " << intCombatSkill << endl;
  cout << "Will Power: " << intWillPower << endl;
  cout << "Endurance: " << intEndurance << endl;






}

int Player_char::check_alive ()
{
	if (intEndurance > 0)
	{
		Is_alive = 1;
		return (1);
	}
	else
	{
		Is_alive = 0;
		return(0);
	}
}

int Player_char::attack()
{
}


void Player_char::takeDamage (int x)
{
	intEndurance = intEndurance - x;
}




int main()
{
}

Re: Noob Question

Posted: Mon Oct 26, 2015 4:58 pm
by dandymcgee
ghost_tech81 wrote:Also do I need to have my class implementations all on the same source file?
No.
ghost_tech81 wrote:Or can I code the implementations separately on a different .cpp file for each class?
Yes.
ghost_tech81 wrote:If it make any difference I am using VC++ 11.
No.

Re: Noob Question

Posted: Tue Oct 27, 2015 7:49 am
by YourNerdyJoe
The most common way to do this would be to put each class declaration in their own separate header files and the definitions in their own separate cpp files.

Code: Select all

//PlayWorld.h

class PlayWorld
{
public:

   void PrintPage();
   void PrintMonsters();
   void PrintItems();
};

//PlayWorld.cpp

void PlayWorld::PrintPage()
{
}
//etc
Repeat this process for each class and include the headers where the class is needed.

In VC++ you can right click on the project in solution explorer and click Add > Class... which will setup the .h and .cpp for you.

Re: Noob Question

Posted: Tue Oct 27, 2015 4:34 pm
by ghost_tech81
YourNerdyJoe wrote:The most common way to do this would be to put each class declaration in their own separate header files and the definitions in their own separate cpp files.

Code: Select all

//PlayWorld.h

class PlayWorld
{
public:

   void PrintPage();
   void PrintMonsters();
   void PrintItems();
};

//PlayWorld.cpp

void PlayWorld::PrintPage()
{
}
//etc
Repeat this process for each class and include the headers where the class is needed.

In VC++ you can right click on the project in solution explorer and click Add > Class... which will setup the .h and .cpp for you.
Cheeers for that, I really should start using the wizards where they are available. Would of saved me a hell of a mess.

Gonna copy the code I have done (more than what is included above), then use the wizard and paste it in.

Cheers again.