Write the implementation (.cpp file) of the Player class from the previous exerc
ID: 3621062 • Letter: W
Question
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 <string>
8 #include <iostream>
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???
Explanation / Answer
Im not exactly sure what would cause that without seeing all of your files. The way that I was taught was best to write classes was to place the class declaration in a header file and separate the class functions in another cpp file. Also main would go in yet another cpp file. Do you have header guards or put #pragma once at the top of your header file? I wrote this code and it compiles and runs fine for me. It doesnt look much different than what you posted, but again, I didnt see your class declaration... /* FILE 1*/ // Player.h #pragma once #include #include using namespace std; class Player { public: void SetName ( std::string name ); void SetScore ( int score ); string GetName ( void ); int GetScore ( void ); private: string mName; int mScore; }; /* FILE 2*/ // Player.cpp #include "Player.h" void Player::SetName( string name ) { mName = name; } void Player::SetScore( int score ) { mScore = score; } string Player::GetName( void ) { return mName; } int Player::GetScore( void ) { return mScore; } /* FILE 3*/ //test.cpp #include "Player.h"; int main (int argc, char** argv) { Player a; return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.