- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
正在做一项作业,几乎所有的事情都完成了,除了我似乎无法布置类(class)的 operator+ 功能。指导/方向将非常受欢迎,因为我似乎无法确定我做错了什么。
#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;
}
最佳答案
你需要创建一个 temp_obj
并返回它,就像你在后缀 operator++
中所做的那样,但是你将更新 temp_obj
的成员而不是更新 this
中的任何内容。
事实上,您可以将其设为const
成员函数,以便编译器检测您是否意外更新了this
。大多数人甚至对 operator+
使用非成员运算符重载以使其成为对称关系。
不相关,但是:
days = h/8;
为整数除法(舍去余数)天
和小时
,这是脆弱的。似乎只有 hours
应该是一个成员变量,而 getDays
函数(应该是 const
)可以即时计算它。operator++
和 operator--
(没有伪参数)不应该按值返回 - 这些函数应该更新 this
的成员并返回对 *this
的引用。专业提示:为避免代码重复,您可以像这样执行递增运算符(假设您希望它们都成为成员函数):
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
}
关于C++ - 重载 operator+ 接受一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28754450/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎离题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a minim
家庭作业 必须同时重载 operator>(istream &in, Complex &value); public: // constructor Compl
这个问题在这里已经有了答案: What are the basic rules and idioms for operator overloading? (8 个答案) 关闭 5 年前。 有什么区别
这是文档中一个不清楚的示例,使用此运算符:http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#at 最佳答案 请注
我不明白这种行为: > sort([1,2,3,4]) ~~ sort([1,2,3,4]) False 你能给我解释一下吗? 为什么这两个列表(显然是相等的)根据 Perl 6 不相等。 更新 有趣
我正在尝试将 Ø 设为逻辑否定运算符。 ¬ True; multi sub prefix: ($n) { return not $n; } 当我运行上面的程序时,它返回以下错误: $
class Port { private: char * brand; char style[20]; // i.e., tawny, ruby, vintage int bo
早上好。我有一些问题。我有这些字段: name: "Mike", city: "NY", address: "something", pets: ["dog", "cat"] 我创建了索引 db.pe
我有以下代码使用 Javascript Webcrypto-API 解密 AES 加密数据,但它会导致“OperationError”并显示消息“操作因操作特定原因而失败”: function loa
我制作了一个自定义 Airflow 操作符,这个操作符接受一个输入,这个操作符的输出在 XCOM 上。 我想要实现的是使用一些定义的输入调用运算符,将输出解析为可在分支运算符内部调用的 Python,
int a = 1; a += ++a; cout << a << endl; // 4 int a = 1; a += a++; cout << a << endl; // 3 为什么这两个例子有不
我有这个队列的实现: #include using namespace std; template struct elem_q { T inf; elem_q* link; }; template
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我对 C++ 中的运算符重载有疑问。我有代表复数的结构,我正在重载运算符,所以我可以用复数进行计算。Visual Studio 2012 给我这个错误:1 IntelliSense:没有运算符“!=”
我正在尝试创建一个 BoolArray 类,它表示一个 bool 值数组,而不是为每个 bool 变量保存 1 个 bool 值。它使用 1 个字符变量来表示使用位的 8 个 bool 值,并节省内存
我类有 2 个运算符有点问题。 我的类(class)宣布: template class MyMap{ keyType keys[MAX]; valueType values[MAX
我有类的迭代器模板和用于 for 语句的类。 template class Itr2 { public: Itr2() { } ~Itr2() { } typedef t
Section 7.2 Enumeration declarations 没有说明任何关于 operator!=() 和作用域的 operator==()枚举。但是下面的代码可以编译。 #includ
我需要将一个 int 序列化到本地文件并将其读入内存。这是代码 #include "stdafx.h" #include #include using namespace std; int _tm
operator += 这样定义对吗?! void operator +=(const BigNumber& other) { *this=(*this) + other; } 在这样的类中:
我是一名优秀的程序员,十分优秀!