0

This is my c++ homework and i dont really get what they meant by setting the values in the method read(). Question: Create a base class called Athlete that contains 2 member variables for attributes common to all professional athletes: name and annual salary. It should also contain pure virtual method, read(). The method read() is called to read data from the user for setting the values of the attributes. Here is my header file

#ifndef ATHLETE_H
#define ATHLETE_H
#include <string>

using namespace std;
class Athlete
{
public:
  Athlete();
  ~Athlete();
  void setName(string name);
  string getName() const;
  void setSalary(double salary);
  double getSalary() const;
  virtual void display() const;
  virtual void read(string name, double salary) const;
private:
  string name;
  double salary;
};
#endif

And my cpp

#include "Athlete.h"
#include <iostream>

Athlete::Athlete() {}

Athlete::~Athlete() {}

string Athlete::getName() const { return this->name; }

void Athlete::setName(string name) {
  this->name = name;
}

double Athlete::getSalary() const {
  return this->salary;
}

void Athlete::setSalary(double salary) {
  this->salary = salary;
}

void Athlete::read(string name, double salary) const {
  Athlete* temp = new Athlete();
  temp->setName(name);
  temp->setSalary(salary);
}

void Athlete::display() const {
  cout << "Name: " << this->getName() << endl;
  cout << "Salary: " << this->getSalary() << endl;
}

I tried to use the setter methods in read but theres an error.

stevenTan
  • 3
  • 3
  • The name `read` is very confusing. Why would you set anything in `read` method? Anyway according to your description all you have to do is `setName(name); setSalary(salary);` inside it (do not create new object). – freakish Jun 29 '16 at 06:43
  • 1
    If you write pure virtual function, you don't implement it, just `virtual void read(string name, double salary) = 0`, another class that derive this class should be implement this function. – Halil İbrahim Oymacı Jun 29 '16 at 06:43
  • @freakish but when i put those 2 lines the compiler says that it is not compatible – stevenTan Jun 29 '16 at 06:46
  • oh so i just leave it empty and let a derived class fill it is it? – stevenTan Jun 29 '16 at 06:47
  • 2
    @stevenTan That's because you've marked `read` method as `const`. If it is supposed to modify the object then it cannot be `const`. – freakish Jun 29 '16 at 06:49
  • 1
    @Melebius Where in the standard is it said you **cannot** implement it ? See: [this question](http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation) – fjardon Jun 29 '16 at 07:41
  • @fjardon Thanks for correction, my fault. – Melebius Jun 29 '16 at 07:52

2 Answers2

1

I think you misread the question. It says that the read() method should read the data from the user. Usually it means read from the standard input. Afterwards, the method should set the values of the attributes for this specific athlete. Meaning, that the entered values relate to this specific object. Not for something new and temporary.

Pulling everything together is may look like the following:

 void Athlete::read()
 {
      string name;
      double salary;
      std::cout << "Please enter the athlete name:";
      std::cin >> name;
      std::cout << "Please enter the athlete salary:";
      std::cin >> salary;
      setName(name);
      setSalary(salary);
}
GMichael
  • 2,726
  • 1
  • 20
  • 30
  • Actually, he did not: "The method read() is called to read data from the user for setting the values of the attributes" (quote from the OP). – Fara Importanta Jun 29 '16 at 07:04
  • @FaraImportanta Well, to read the question you have to read the whole question. The sentence before says "It should also contain pure virtual method, read(). " - the above method is not pure virtual. – skyking Jun 29 '16 at 07:22
  • @skyking I completely agree. But it seems nor you, or @GMichael read the whole question. The first issue of the OP was that `read()` was not a virtual pure function (which you correctly addressed) and the second issue of the OP was that data was to be read from the user (which @GMichael correctly addressed). In my opinion, the correct answer to this question is your answer + @GMichael 's answer. – Fara Importanta Jun 29 '16 at 07:28
  • @FaraImportanta As the question (only) asked for a base class containing a pure virtual method `read` you're not supposed to implement it. Anyway as being pure virtual you're definitely not going to implement it as a member of the base class. – skyking Jun 29 '16 at 07:33
  • @skyking You can implement a pure virtual method, nothing prevents it in the standard. The derived class method implementation can then call on the parent implementation (using `Parent::Method()` syntax). Read Scott Meyer for an example. – fjardon Jun 29 '16 at 07:37
  • @fjardon That's correct, but since the question didn't explicitely ask for it to be implemented one could leave it and interpret the statement as only information on how the method in the derived class is supposed to behave. – skyking Jun 29 '16 at 07:44
  • @skyking That is indeed a problem in GMichael 's answer, but it addresses a requirement in the OP. Although fjardon is right (See [http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation]), I doubt it that that was what the homework was all about. Anyway, I think the OP got his answer and some more... :) – Fara Importanta Jun 29 '16 at 07:45
1

The thing you've missed is that read is supposed to be a pure virtual function. This means that you should not actually implement it, instead you should declare it as:

virtual void read(string name, double salary) = 0;

This means that the Athlete class cannot actually be instantiated (it's called an absract class), instead it will be used as a base class and derived classes would be required to override the read method. If they don't override the method they will themselves be abstract and cannot be instantiated.

You are not required to implement the read method as a method of Athlete once you declared it as pure virtual. It only need to be implemented as a method in the derived class(es).

Also as the method in the derived class is supposed to modify the object the method cannot be const declared (as shown above).

skyking
  • 13,817
  • 1
  • 35
  • 57
  • So in my derived class lets say tennis player i implement the read() method there while i just leave the base class =0 am i correct? – stevenTan Jun 29 '16 at 06:58
  • 1
    @stevenTan You do not need make this method pure virtual. The question says the method is applied to any athlete. So, this should be a regular (or virtual) method of the base class. Not pure one. – GMichael Jun 29 '16 at 07:12
  • @GMichael Yes, you do - the task specifically says that the method should be pure virtual. Read the question! – skyking Jun 29 '16 at 07:23
  • 1
    @stevenTan Yes, that's correct. In the derived class declaration you have to include the method as well (but that was not part of the task). – skyking Jun 29 '16 at 07:24