私はC++をブラッシュアップしていて、toString関数が定義したフォーマット済みの文字列を出力しない理由を理解しようとしています。
私が参照している関数は次のとおりです。friend std::ostream& operator<<(std::ostream&, const Employee&);
Employee.cpp
#include <iostream>
#include <stdio.h>
using namespace std;
class Employee {
private:
string name;
double rate;
double hours;
double getPay() const;
friend std::ostream& operator<<(std::ostream&, const Employee&);
public:
Employee(string, double);
void setHours(double);
};
Employee::Employee(string name, double rate) {
this->name = name;
this->rate = rate;
this->hours = 0;
}
void Employee::setHours(double hours) {
this->hours = hours;
}
double Employee::getPay() const {
double gross = this->hours * this->rate;
double overtime = this->hours > 40 ?
(this->hours - 40) * (this->rate * 1.5) : 0;
return gross + overtime;
}
// toString
std::ostream& operator<<(std::ostream &strm, const Employee &e) {
char buff[64];
return strm << sprintf(buff, "Name: %s, Salary: $%.2f\n",
e.name.c_str(), e.getPay());
}
int main (int* argc, char** argv) {
Employee emp1("Bob", 28);
Employee emp2("Joe", 32);
emp1.setHours(44);
emp2.setHours(25);
cout << emp1 << endl;
cout << emp2 << endl;
return 0;
}