36.1k views
1 vote
Write the implementation (.cpp file) of the Player class from the previous exercise. Again, the class contains:

A data member name of type string .
A data member score of type int .
A member function called setName that accepts a parameter and assigns it to name . The function returns no value.
A member function called setScore that accepts a parameter and assigns it to score . The function returns no value.
A member function called getName that accepts no parameters and returns the value of name .
A member function called getScore that accepts no parameters and returns the value of score .
My code is
#include
8 #include
9 using namespace std;
10 #include "Player.h "
11
12 void Player::setName(string name)
13 {
14 }
15 void Player:: setScore(int score)
16 {
17 }
18 string Player:: getName()
19 {
20 return name;
21 }
22 int Player:: getScore()
23 {
24
25 return score;
26 }
and it keeps saying player.h is being redefined....how do i fix this???

User Tura
by
6.3k points

1 Answer

3 votes

Answer:

//Define the header file

#ifndef PLAYER_H

#define PLAYER_H

//header file.

#include <string>

//Use the standard namespace.

using namespace std;

//Define the class Player.

class Player

{

//Declare the required data members.

string name;

int score;

public:

//Declare the required

//member functions.

void setName(string par_name);

void setScore(int par_score);

string getName();

int getScore();

}

//End the definition

//of the header file.

#endif

Player.cpp:

//Include the "Player.h" header file,

#include "Player.h"

//Define the setName() function.

void Player::setName(string par_name)

{

name = par_name;

}

//Define the setScore() function.

void Player::setScore(int par_score)

{

score = par_score;

}

//Define the getName() function.

string Player::getName()

{

return name;

}

//Define the getScore() function.

int Player::getScore()

{

return score;

}

User Godfatherofpolka
by
6.1k points