Page 1 of 1
Convert php class to c++
Posted: Wed Sep 02, 2009 7:37 am
by Sindarin
I made an object in php and now need to do the same in a C++ program.
can someone please help me translate this code into c++?
Code: Select all
//class container
class character
{
//define variables
var $name;
var $hp;
var $mp;
var $attack;
//main function
public function character($name, $hp, $mp, $attack)
{
$this->Name = $name;
$this->HP = $hp;
$this->MP = $mp;
$this->Attack = $attack;
}
public function get_stats()
{
return $this->Name." | ".$this->HP." HP, ".$this->MP." mp, and ".$this->Attack." attack.";
}
}
//objects
//create new object from class by assigning it to a variable
$character1 = new character('Alex', 200, 100, 20);
//use function from object
echo $character1->get_stats();
Re: Convert php class to c++
Posted: Wed Sep 02, 2009 12:04 pm
by Ollie Saunders
Code: Select all
#include <stdio.h>
using namespace std;
class GameChar {
char * name;
int hp, mp, atck;
GameChar(char* _name, int _hp, int _mp, int _atck) {
name = _name;
hp = _hp;
mp = _mp;
atk = _atck;
}
char* stats() {
char * statStr;
sprintf(statStr, "%s | %i HP %i mp, and %i attack.", name, hp, mp, atck);
return statStr;
}
};
int main() {
GameChar alex = new GameChar("Alex", 200, 100, 20);
puts(alex.stats());
return 0;
}
This could have any number of errors in it but I'd be interested to see how correct I am. I haven't used C++ properly in years.
I changed some of the name to things I think are better.
Re: Convert php class to c++
Posted: Wed Sep 02, 2009 12:43 pm
by requinix
Ollie Saunders wrote:This could have any number of errors in it but I'd be interested to see how correct I am. I haven't used C++ properly in years.
The only issue I see is the uninitialized string pointer in stats(). I'd do it like
Code: Select all
int stats(char* buf) {
return sprintf(buf, "%s | %i HP %i mp, and %i attack.", name, hp, mp, atck);
}
and then
Code: Select all
int main() {
char b[200];
GameChar alex = new GameChar("Alex", 200, 100, 20);
alex.stats(b); puts(b);
return 0;
}
Re: Convert php class to c++
Posted: Wed Sep 02, 2009 1:48 pm
by Ollie Saunders
Ah, okay, that makes sense. Should I have put "private:" above the property declarations and "public:" for the methods?
Re: Convert php class to c++
Posted: Wed Sep 02, 2009 2:41 pm
by requinix
Ollie Saunders wrote:Ah, okay, that makes sense. Should I have put "private:" above the property declarations and "public:" for the methods?
Eh, whatever. It's up to coding style, really. Either the variables are public (and easy to access) or you have a bunch of annoying getters and setters.
Re: Convert php class to c++
Posted: Wed Sep 02, 2009 3:28 pm
by Ollie Saunders
Yeah, okay. That was a good answer.
Re: Convert php class to c++
Posted: Thu Sep 03, 2009 5:35 am
by Sindarin
hmm don't know why it doesn't work for me. I don't know where the public/private goes in c++. :/
Oh, and is possible to do this using string.h for text and not char? I don't know much about pointers yet, still confused about them so I don't want to use them yet. Thanks.

Re: Convert php class to c++
Posted: Thu Sep 03, 2009 6:58 am
by Ollie Saunders
hmm don't know why it doesn't work for me.
What errors and output do you see?
I don't know where the public/private goes in c++. :/
I wouldn't worry about that unless you're using a compiler that defaults to private or protected. Do those exist, tasairis?
Oh, and is possible to do this using string.h for text and not char? I don't know much about pointers yet, still confused about them so I don't want to use them yet. Thanks.
string.h just provides more functions that manipulate character arrays via pointers. That is the definition of a string in C — an array of characters with a pointer. Pointers are really important in C and C++. I'd strongly advise that you make every attempt to understand them before you move on to other things. What don't you understand? Perhaps we can help.
There is a string class (CString I think it's called, although they might just be a Microsoft thing), which might be what you're after. I can't remember where it is defined or how to use it though. It is a higher level feature that probably makes use of template classes, virtual function and other advanced stuff. iostream might be another way you can do it, equally advanced if not more so. They aren't actually that difficult to use, I don't think, but their implementations are complex.
Re: Convert php class to c++
Posted: Thu Sep 03, 2009 11:22 am
by John Cartwright
Moved to Misc.
Re: Convert php class to c++
Posted: Fri Sep 04, 2009 2:36 am
by Sindarin
Pointers are really important in C and C++. I'd strongly advise that you make every attempt to understand them before you move on to other things. What don't you understand? Perhaps we can help.
Yeah I figured they'd be as anyone I know that codes in c++ had problems understanding them.
I understand why you would reference a variable, but why you would need its raw address to play with?
I got that
int apple = 25; //is a variable
int * apple_pointer = &apple; //the apple_pointer variable is now a pointer *. The & character converts from int to int * which gets the pointed variable's (apple) memory address.
Did I got that right?
What I don't understand is where this could be useful since I can work with normal variables fine.
and why would I do:
int orange = *apple_pointer;
when I can simply do:
int orange = apple;
string.h just provides more functions that manipulate character arrays via pointers. That is the definition of a string in C — an array of characters with a pointer.
I don't mind using char, but what I am concerned about is their datatype width. 0 to 255 characters max unsigned. It's really not enough if you have long text. I figured string would be like the 'text' datatype in SQL.
Isn't there any data type like this?
Now about the code:
Code: Select all
#include <stdio.h>
using namespace std;
class GameChar {
char * name;
int hp, mp, atck;
public: GameChar(char* _name, int _hp, int _mp, int _atck) {
name = _name;
hp = _hp;
mp = _mp;
atk = _atck;
}
char* stats() {
char * statStr;
sprintf(statStr, "%s | %i HP %i mp, and %i attack.", name, hp, mp, atck);
return statStr;
}
};
int main() {
GameChar alex = new GameChar("Alex", 200, 100, 20);
puts(alex.stats());
return 0;
}
I get:
C:\CPP Projects\CMD Line\main.cpp||In constructor `GameChar::GameChar(char*, int, int, int)':|
C:\CPP Projects\CMD Line\main.cpp|13|error: `atk' was not declared in this scope|
C:\CPP Projects\CMD Line\main.cpp|13|warning: unused variable 'atk'|
C:\CPP Projects\CMD Line\main.cpp||In function `int main()':|
C:\CPP Projects\CMD Line\main.cpp|25|error: conversion from `GameChar*' to non-scalar type `GameChar' requested|
||=== Build finished: 2 errors, 1 warnings ===|
Re: Convert php class to c++
Posted: Mon Oct 12, 2009 4:37 pm
by superdezign
Basically, anything after the private label is private, everything after the public label is public. Also, their scopes do not overlap.
For simplicity, make everything public. By default, everything is private. Making everything public can be dangerous, but in game programming, it's common. Direct access to variables is faster than using functions.
Re: Convert php class to c++
Posted: Tue Oct 13, 2009 5:58 am
by jackpf
Spam -_-
It's just a duplicate of a post above...
Sindarin wrote:I get:
C:\CPP Projects\CMD Line\main.cpp||In constructor `GameChar::GameChar(char*, int, int, int)':|
C:\CPP Projects\CMD Line\main.cpp|13|error: `atk' was not declared in this scope|
C:\CPP Projects\CMD Line\main.cpp|13|warning: unused variable 'atk'|
C:\CPP Projects\CMD Line\main.cpp||In function `int main()':|
C:\CPP Projects\CMD Line\main.cpp|25|error: conversion from `GameChar*' to non-scalar type `GameChar' requested|
||=== Build finished: 2 errors, 1 warnings ===|
Where are you declaring atk? Read your error messages

Don't you mean atck?
Also, try using a pointer.
Code: Select all
GameChar *alex = new GameChar("Alex", 200, 100, 20);
//then access methods like
alex->method();