So I am creating a basic layout for a game and I have very little experience with separating classes from my main file and making two new files out of it (implementation file, .h file). I somehow messed up with some of the separation and I can't figure out where exactly I went wrong. I also am having trouble figuring out how to go about creating an object from these two new files in my main. for example, I will show you the ORIGINAL class, then my .h then my .cpp.
******ORIGINAL CLASS******
class Character{
public:
string name;
int health;
Character(){
setName("Unknown Caster");
setHealth(20);
}
Character(string name, int health){
setName(name);
setHealth(health);
}
void setName(string x){
cout << "What is your name?" << endl;
cin >> x;
name = x;
}
int setHealth (int health){
if(health < 0){
health = 0;
}
this-> health = health;
return health;
}
string getName(){
return name;
}
};
*****END OF ORIGINAL CLASS*****
*****START OF .h FILE*****
#ifndef Character_h
#define Character_h
using namespace std;
class Character{
public:
string name;
int health;
Character();///default constructor
Character(string name, int health);
void setName(string x){
}
int setHealth (int health){
}
string getName(){
return name;
}
};
#endif
*****END OF .h FILE*****
*****START OF .cpp FILE*****
#include <iostream>
#include <string>
#include "Character.h"
Character::Character(){
setName("unknown caster");
setHealth(20);
}
Character::Character(){
setName(name);
setHealth(health);
}
void Character::setName(){
cout << "What is your name?" << endl;
cin >> x;
name = x;
}
int Character::setHealth(){
if(health < 0){
health = 0;
}
this-> health = health;
return health;
}
string Character::getName(){
return name;
}
****END OF .cpp FILE*****
My code was running relatively smoothly before I separated the one class into two different files, so I am inclined to believe that I did it incorrectly. My questions are: What did I do wrong? and how do I go about making an object from this class in my main? Thank you for your time in advance!