【发布时间】:2018-10-12 02:08:56
【问题描述】:
我无法弄清楚我的“副本”变量出了什么问题。
这是我的家庭作业的演示代码,它向我们展示了基本语法以及如何使用不同类型的 OOP 构造函数和关键字。什么时候运行的问题这一切都运行良好,但问题出在副本变量中,当构造函数被复制但它似乎不起作用时,它的值应该为 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
【问题讨论】:
-
为了更清楚地理解问题。您需要知道何时复制课程?或者您需要知道同一个人(例如 Jane)的课程何时有第二个实例?
标签: oop copy-constructor