C++ C++;-重载运算符+;拿一个论点
正在做作业,几乎完成了所有工作,但我似乎无法布置类的运算符+函数。由于我似乎无法确定自己做错了什么,所以非常欢迎提供指导/指导C++ C++;-重载运算符+;拿一个论点,c++,C++,正在做作业,几乎完成了所有工作,但我似乎无法布置类的运算符+函数。由于我似乎无法确定自己做错了什么,所以非常欢迎提供指导/指导 #include <iostream> using namespace std; class numDays { private: // member variables int hours; double days; public: // member functions numDays(int h = 0) {
#include <iostream>
using namespace std;
class numDays {
private: // member variables
int hours;
double days;
public: // member functions
numDays(int h = 0) {
hours = h;
days = h / 8;
}
void setHours(int s) {
hours = s;
days = s / 8;
}
double getDays() {
return days;
}
numDays operator+(numDays& obj) {
// what to put here?
}
numDays operator- (numDays& obj) { // Overloaded subtraction
// and here?
}
numDays operator++ () { // Overloaded postfix increment
hours++;
days = hours / 8.0;
numDays temp_obj(hours);
return temp_obj;
}
numDays operator++ (int) { // Overloaded prefix increment
numDays temp_obj(hours);
hours++;
days = hours / 8.0;
return temp_obj;
}
numDays operator-- () { // Overloaded postfix decrement
hours--;
days = hours / 8.0;
numDays temp_obj(hours);
return temp_obj;
}
numDays operator-- (int) { // Overloaded prefix decrement
numDays temp_obj(hours);
hours--;
days = hours / 8.0;
return temp_obj;
}
};
int main() {
// insert code here...
numDays one(25), two(15), three, four;
// Display one and two.
cout << "One: " << one.getDays() << endl;
cout << "Two: " << two.getDays() << endl;
// Add one and two, assign result to three.
three = one + two;
// Display three.
cout << "Three: " << three.getDays() << endl;
// Postfix increment...
four = three++;
cout << "Four = Three++: " << four.getDays() << endl;
// Prefix increment...
four = ++three;
cout << "Four = ++Three: " << four.getDays() << endl;
// Postfix decrement...
four = three--;
cout << "Four = Three--: " << four.getDays() << endl;
// Prefix increment...
four = --three;
cout << "Four = --Three: " << four.getDays() << endl;
return 0;
}
#包括
使用名称空间std;
班级天数{
私有成员变量
整小时;
双日;
公共成员职能
numDays(int h=0){
小时=小时;
天数=h/8;
}
无效设置小时数(整数秒){
小时=秒;
天数=s/8;
}
双倍天数(){
返程天数;
}
numDays运算符+(numDays和obj){
//在这里放什么?
}
numDays运算符-(numDays&obj){//重载减法
//这里呢?
}
numDays运算符++(){//重载的后缀增量
小时++;
天=小时/8.0;
工作日(小时);
返回温度对象;
}
numDays运算符++(int){//重载前缀增量
工作日(小时);
小时++;
天=小时/8.0;
返回温度对象;
}
numDays运算符--(){//重载的后缀减量
小时--;
天=小时/8.0;
工作日(小时);
返回温度对象;
}
numDays运算符--(int){//重载前缀减量
工作日(小时);
小时--;
天=小时/8.0;
返回温度对象;
}
};
int main(){
//在这里插入代码。。。
星期一(25)、二(15)、三、四;
//显示一和二。
您需要创建一个temp_obj
并返回它,就像您在postfixoperator++
中所做的那样,但是您将更新temp_obj
的成员,而不是更新中的任何内容
事实上,您可以将其设置为常量
成员函数,以便编译器检测您是否意外更新了此
。大多数人甚至对运算符+
使用非成员运算符重载,使其成为对称关系
无关,但:
days=h/8;
是整数除法(余数被丢弃)
- 您的代码似乎在并行维护
天
和小时
,这很脆弱。似乎只有小时
应该是成员变量,而getDays
函数(应该是常量
)可以动态计算它
- 正如Dan Allen所指出的,前缀
运算符+++
和运算符--
(没有伪参数的)不应该按值返回-这些函数应该更新this
的成员,并返回对*this
的引用
专业提示:为了避免代码重复,您可以这样做增量运算符(假设您希望它们都是成员函数):
+
或-
运算符的预期结果到底是什么?补充一下Matt的观点。有些地方使用小时/8,有些地方使用小时/8.0,你会遇到奇怪的不一致。而且你的注释是错误的。numDays运算符++(int)
是post-fix运算符。代码看起来是对的。注释是错误的。当您说“非成员运算符重载为运算符+
”时,您指的是朋友numDays运算符+(const numDays&)
?@JonnyHenly否;在类定义之外numDays运算符+(numDays arg1,numDays arg2)
friend
函数是非成员函数,但是不使用friend
更简单(我认为friend函数在学习材料中使用过度了)这里有一些例子。@ MattMcNabb OH GOTCHA。谢谢你的说明和示例链接。我会给你2个答案。你的意思是“代码> TimeObj.Hyth+Obj.Th;在中间,但是这不会更新<代码>天/代码>。事实上,你可以一劳永逸地做到这一点:<代码>返回NUMDAY(小时+Obj.h)。因为您有一个接受小时数的构造函数。
numDays & operator++() // prefix
{
// your logic here, e.g. hours++;
return *this;
}
numDays operator++(int dummy) // postfix
{
return ++numDays(*this); // invokes prefix operator++ we already wrote
}