90.2k views
2 votes
Write the interface (.h file) of a class Player containing: 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 start code:
class Player
{
string name;
int score;
void setScore (int playerScore)
{
score = playerScore;
}
int getScore()
{
return score;
}
}

2 Answers

5 votes

Final answer:

The class Player's interface includes a private string and int for the player's name and score, with public member functions setName, setScore, getName, and getScore to interact with these properties.

Step-by-step explanation:

The interface of a class Player in a header file (.h file) can be written with appropriate member functions and member variables. Here is an example of how it can be structured:

// Player.h
#include <string>
class Player {
private:
std::string name;
int score;
public:
// Sets the player's name
void setName(const std::string& playerName) {
name = playerName;
}

// Sets the player's score
void setScore(int playerScore) {
score = playerScore;
}

// Returns the player's name
std::string getName() const {
return name;
}

// Returns the player's score
int getScore() const {
return score;
}
};

This interface includes a private member variables for the player's name and score, as well as public member functions to set and get these values.

User ShadeMe
by
4.0k points
4 votes

Answer:

Following are the class definition to this question:

class Player//defining a Player class

{

private://use access specifier

string name;//defining string variable

int score;//defining integer variabl

public://use access specifier

void setName(string par_name);//declaring the setName method with one string parameter

void setScore(int par_score);//declaring the setScore method with one integer parameter

string getName();//declaring string method getName

int getScore();//declaring integer method getScore

};

Step-by-step explanation:

In the above-given code, a Player class is defined, that hold a method and the variable which can be defined as follows:

  • It uses two access specifier, that are public and private.
  • In private, a string and an integer variable "name, score" is declared, which holds their respective value.
  • In public, it declares the get and set method, which holds the value from its parameters.
User Richard Logwood
by
4.3k points