ok, in a previous exercise I created a class called Player
class Player
{
private:
string name;
int score;
public:
void setName(string name);
void setScore(int score);
string getName();
int getScore();
};
In the next question I was asked
Write the implementation (.cpp file) of the Player class from the previous exercise.
I'm having alot of trouble trying to get it to work, I can't seem to Implement it.... please help.
Help - C++ -Write the implementation (.cpp file)?
void Player::setName(string name)
{
this-%26gt;name=name;
}
void Player::setScore(int score)
{
this-%26gt;score=score;
}
int Player::getScore()
{
return score;
}
string Player::getName()
{
return name;
}
Reply:Simple: Each one of those set...() methods should set the respective member; each one of the get...() methods should get it.
Now, to be more complete, the set...() methods might also check their parameters for correctness (positive numbers for instance).
Go code.
Reply:since this is super easy I'll code it for you.
the file you are showing me is going to be named player.h
and the file i am going to code is player.cpp
to compile it just do g++ -c Player.cpp that will give you Player.o (object file),
this will allow you to do an #include "player.h" and compile like a test program such as g++ playertest.cpp Player.o -o player
#include "Player.h"
void Player::setName(string _name){
name = _name;
};
void Player::setScore(int _score){
score = _score
};
std::string Player::getName(){ return name; };
int Player::getScore(){ return score };
note unless you have
using namespace std;
at the top of your code, your going to need to put
std::string because it is part of the STL.
Reply:You implement the set methods by giving them parameters and making the assignments within the methods.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment