Printing a class' info as a string?

Hello there! I feel that my teacher gave me some vague instructions. I have created a class named Player with the data members name and score. I then have the set and return functions, but then I am to

" Apply FormatPlayerInfo - this returns the Player info as a string"
" Apply PrintPlayerInfo - output to the output window"

these are my instructions! Can you guys help me to even understand what my teachers asking me to do here? What "info" is she referring to? Also, why do I need two seperate functions(I'm assuming) to do this? Couldn't I just convert and display the info in one function? Thanks for the help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Player{
public:
	Player(string z){
		setName(z); 
		
		cout << "Well... I have a working constructor." << endl;
	}
	void setName(string x) {
		name = x;
	}
	void setScore(int y){
		score = y;
	}
	string getName(){
		return name;
	}
	int getScore(){
		return score; 
	}

private: 
	string name;
	int score; 
};
Last edited on
.../;,.
Last edited on
The first task is asking to create a string holding all of the player info (in this case, name and score) in a readable way. For example, it might return "name=Person score=30" (Values would obviously differ, and you may set it up differently).
Have you learned about string streams yet? If so, I'd advise using one of those.

The second one is to print that data (name, score) to the window.


And you are correct that there would be 2 functions. Usually it's a good idea to have different functionality (convert to string, print to screen) separately, so that in the case you ever want to use just one part, you aren't stuck with using all of it. For example, if you'd want to get the data string to put it into a file, you may not want to have it also output to the console.
Internally, the "print data" function could just print the return value from the "get data as string" function.
Topic archived. No new replies allowed.