Convert php class to c++

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Convert php class to c++

Post 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();
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Convert php class to c++

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Convert php class to c++

Post 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;
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Convert php class to c++

Post by Ollie Saunders »

Ah, okay, that makes sense. Should I have put "private:" above the property declarations and "public:" for the methods?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Convert php class to c++

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Convert php class to c++

Post by Ollie Saunders »

Yeah, okay. That was a good answer.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Convert php class to c++

Post 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. :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Convert php class to c++

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Convert php class to c++

Post by John Cartwright »

Moved to Misc.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Convert php class to c++

Post 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 ===|
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Convert php class to c++

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Convert php class to c++

Post 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();
Post Reply