trying to make a file to hold a class
Mar 22, 2014 at 5:23pm UTC
ok so I have been trying to split into a separate file one of my classes but every time I try to use a string [some name] it errors the string and is so confusing. this code shows me what I want but I can not get it split into separate files and work. Anyone able to help with this? if someone could show me the critter.h file, critter.cpp and main.cpp.
thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include<iostream>
#include<string>
using namespace std;
class critter
{
protected :
int HP;
string name;
public :
void setname(string n);
string getname();
void sethp(int h);
int gethp();
critter();
~critter();
};
critter::critter(): HP(6), name("rat" )
{}
critter::~critter()
{}
void critter::setname(string n)
{
name = n;
}
string critter::getname()
{
return name;
}
int critter::gethp()
{
return HP;
}
void critter::sethp(int h)
{
HP=h;
}
class goblin: public critter
{
public :
goblin();
};
goblin::goblin() // i dont know how to get this goblin to load inital values
{
goblin::sethp(10), goblin::setname("goblin" );
}
int main()
{
critter* mob;
mob=new critter[3];
for (int i = 0; i < 3; ++i)
{mob[i].~critter();}
mob[0]=goblin();
mob[1]=critter();
cout << mob[0].gethp() << " hp's" << endl;
cout << mob[0].getname() << " name\n" ;
cout << mob[1].gethp() << " hp's" << endl;
cout << mob[1].getname() << " name\n" ;
system("pause" );
return 0;
}
Last edited on Mar 22, 2014 at 6:12pm UTC
Topic archived. No new replies allowed.