Я не могу понять, что не так с моей переменной "копий".
Это демонстрационный код для моей домашней работы, который показывает нам основной синтаксис и как использовать различные типы конструкторов ООП и ключевых слов.,Вопрос, когда запускается это все работает нормально, но проблема в переменной копий, она должна иметь значение 1, когда конструктор копируется, но, похоже, он не работает.
#include <iostream>
#include <string>
using namespace std;
const int JANE_HOURS = 30, JIM_HOURS = 20, SETTER_HOURS = 40;
const double SETTER_DAYS = 3.0, HOURS_PER_DAY = 24.0;
class WorkerHours {
private:
int hoursWorked;
int copies;
public:
void setData(int);
void setData(double);
int getCpy();
WorkerHours();
WorkerHours(int);
WorkerHours operator + (const WorkerHours &combinedOb) const;
WorkerHours operator += (const WorkerHours &combinedOb);
operator int() const;
operator double() const;
friend void showInternalData(string label, const WorkerHours &worker);
WorkerHours(WorkerHours &janeCpy);
~WorkerHours();
};
WorkerHours::WorkerHours() {
hoursWorked = 0;
copies = 0;
}
WorkerHours::WorkerHours(int hoursWork) {
hoursWorked = hoursWork;
}
WorkerHours::WorkerHours(WorkerHours &janeCpy) {
hoursWorked = janeCpy.hoursWorked;
copies = (janeCpy.copies + 1);
}
int WorkerHours::getCpy() {
return copies;
}
WorkerHours WorkerHours::operator += (const WorkerHours &combinedOb) {
WorkerHours result;
hoursWorked += combinedOb.hoursWorked;
return result;
}
WorkerHours WorkerHours::operator + (const WorkerHours &combinedOb) const {
WorkerHours result;
result.hoursWorked = hoursWorked + combinedOb.hoursWorked;
return result;
}
void WorkerHours::setData(int a) {
hoursWorked = a;
}
void WorkerHours::setData(double a) {
hoursWorked = a * HOURS_PER_DAY;
}
WorkerHours::operator int() const {
return hoursWorked;
}
WorkerHours::operator double() const {
return (hoursWorked / HOURS_PER_DAY);
}
WorkerHours::~WorkerHours() {
cout << "Destroyed" << endl;
}
// This is the prototype of the showInternalData function.
// It must access INTERNAL STRUCTURES in the worker object.
// Do NOT use member functions to get the data for that object.
void showInternalData(string label, WorkerHours &worker);
int main()
{
// Conversion constructor
WorkerHours jane = JANE_HOURS, jim = JIM_HOURS;
// Copy constructor
WorkerHours janeCopy = jane;
// + operator
WorkerHours combined = jane + jim;
// Default constructor
WorkerHours testSetters;
// Variables set aside for calculations
double daysWorked;
int hoursWorked;
// You can use static_cast here, but it shouldn't be required.
//static_cast<double>(combined);
// Type conversion operator - int
daysWorked = combined;
cout << "TEST DAYS WORKED : " << daysWorked << endl;
// You can use static_cast here, but it shouldn't be required.
// static_cast<int>(combined);
// Type conversion operator - double
hoursWorked = combined; //using the = already (over loading)
cout << "TEST HOURS WORKED: " << hoursWorked << endl;
// Now we start using the internal function
showInternalData("Jane", jane);
showInternalData("JaneCopy", janeCopy);
showInternalData("Jim", jim);
showInternalData("Combined", combined);
// += operators
jane += janeCopy;
showInternalData("Jane + JaneCopy", jane);
// Now we test the overloaded setters
testSetters.setData(SETTER_HOURS);
showInternalData("Testing int setter", testSetters);
testSetters.setData(SETTER_DAYS);
showInternalData("Testing double setter", testSetters);
// We’re done
system("pause");
return 0;
}
void showInternalData(string label, WorkerHours &worker) {
string x = label;
cout << "Data: " << x << ", " << "Hours worked:" << worker.operator int() << ", " << "Copy generation: "<< worker.getCpy()<< endl;
}
Вот как должен выглядеть вывод:
TEST DAYS WORKED : 2.08333
TEST HOURS WORKED: 50
Data: Jane, Hours worked: 30, Copy generation: 0
Data: JaneCopy, Hours worked: 30, Copy generation: 1
Data: Jim, Hours worked: 20, Copy generation: 0
Data: Combined, Hours worked: 50, Copy generation: 0
Data: Jane + JaneCopy, Hours worked: 60, Copy generation: 0
Data: Testing int setter, Hours worked: 40, Copy generation: 0
Data: Testing double setter, Hours worked: 72, Copy generation: 0