Noob Question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Noob Question

Postby ghost_tech81 on Mon Oct 26, 2015 5:10 pm

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()
{
}
ghost_tech81
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 5
Joined: Mon Oct 19, 2015 1:30 pm

Re: Noob Question

Postby dandymcgee on Mon Oct 26, 2015 5:58 pm

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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire

Re: Noob Question

Postby YourNerdyJoe on Tue Oct 27, 2015 8:49 am

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.
See that?.....
Exactly
https://yournerdyjoe.github.io/
User avatar
YourNerdyJoe
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
 
Posts: 85
Joined: Sun Oct 02, 2011 4:28 pm

Re: Noob Question

Postby ghost_tech81 on Tue Oct 27, 2015 5:34 pm

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.
ghost_tech81
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 5
Joined: Mon Oct 19, 2015 1:30 pm


Return to Programming Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron