I have a parent class called Athlete and there is a child class called TennisPlayer. There are two attributes in my parent class which are name and annual salary. There is also a read method which take in user input for both classes. Here is how my parent looks like:
class Athlete
{
public:
Athlete();
Athlete(string name, float annual_salary);
virtual void read();
virtual void display();
string get_name() const;
float get_annual_salary();
void set_name(string name);
void set_annual_salary(float annual_salary);
private:
string name;
float annual_salary;
};
class TennisPlayer : public Athlete
{
public:
TennisPlayer();
TennisPlayer(string name, float annual_salary, int current_world_ranking);
int get_current_world_ranking();
void set_current_world_ranking(int current_world_ranking);
virtual void read();
virtual void display();
private:
int current_world_ranking;
};
The read() method for athlete class works perfectly. But when it comes to tennisPlayer which is the child class, something went wrong, it won't take in the user input, instead it straight away ask me for the input of current world ranking. Here is my .cpp for TennisPlayer class:
void TennisPlayer::read()
{
cout << "Enter name for tennis player: " ;
getline(cin, get_name());
cout << "Enter annual salary for tennis player: ";
cin >> get_annual_salary();
cout << "Enter current world ranking: ";
cin >> current_world_ranking;
}
Conctructor for tennisPlayer class:
TennisPlayer::TennisPlayer(string name, float annual_salary ,int current_world_ranking)
: Athlete(name , annual_salary)
{
this->current_world_ranking = current_world_ranking;
}
Also there's a problem when reading the annual salary for tennisPlayer. I am not quite familiar with inheritance in C++. Thanks in advance.
>>extracting operator andgetline().cin >> get_annual_salary();Aside from any other problems, this is gong to read into a temporaryfloatthat immediately gets discarded. So all this really does is ignore something on the stream (and/or possibly set an error flag on the stream).getline(cin, get_name());actually compile?get_name()is returning a string by value which is an rvalue.