重载算术运算符 C++
overloading arithmetic operators c++
我刚开始学习 类 C++,我在处理重载算术运算符时遇到了很多问题。首先,在我的头文件中我有:
#ifndef MONEY_H
#define MONEY_H
#include <iostream>
using namespace std;
class Money{
public:
Money(int dollars, int cents);
Money(int dollars);
Money();
int getDollars() const {return dollars;};
int getCents() const {return cents;};
void setDollarsAndCents(int dollars, int cents);
double getAmount() const {return amount ;};
void setAmount(double amount);
// Define operator functions for comparison operators
friend bool operator==(const Money& firstAmount, const Money& secondAmount);
friend bool operator<(const Money& firstAmount, const Money& secondAmount);
friend bool operator>(const Money& firstAmount, const Money& secondAmount);
//Define operator functions for arithmetic operators
friend Money operator+(const Money& firstAmount, const Money& secondAmount);
friend Money operator-(const Money& firstAmount, const Money& secondAmount);
friend Money operator*(const Money& money, int n);
friend Money operator/(const Money& money, int n);
//Define the output and input operator
friend ostream& operator<<(ostream& outStream, const Money& money);
private:
int dollars, cents;
double amount;
};
#endif
然后我在实现文件上实现了operator+,Money.cpp
#include "Money.h"
// Construct a money object with dollars and cents
Money::Money(int newDollars, int newCents)
{
dollars = newDollars;
cents = newCents;
}
// Construct a money object with JUST the dollars
Money::Money(int newDollars)
{
dollars = newDollars;
cents = 0;
}
// Construct a money object with no arguments (default amount = 0)
Money::Money()
{
amount = 0.0;
}
// Set dollars and cents
void Money::setDollarsAndCents(int newDollars, int newCents)
{
dollars = newDollars;
cents = newCents;
}
// Set monetary amount
void Money::setAmount(double newAmount)
{
//convert cents automatically if >= 100
newAmount = dollars + cents/100.0;
amount = newAmount;
}
// Test if two Money objects are equal or not
bool operator==(const Money& firstAmount, const Money& secondAmount)
{
return (firstAmount.amount == secondAmount.amount);
}
// Test if the first operand is less than the second operand
bool operator<(const Money& firstAmount, const Money& secondAmount)
{
return (firstAmount.amount < secondAmount.amount);
}
// Test if the first operand is greater than the second operand
bool operator>(const Money& firstAmount, const Money& secondAmount)
{
return (firstAmount.amount > secondAmount.amount);
}
// Add two Money objects
Money operator+(const Money& firstAmount, const Money& secondAmount)
{
//assume cents < 100
int carry = 0;
int finalCents = firstAmount.cents + secondAmount.cents;
if (finalCents >= 100){
carry += 1;
finalCents -= 100;
}
int finalDollars = firstAmount.dollars + secondAmount.dollars + carry;
return Money(finalDollars, finalCents);
}
// Subtract two Money objects
Money operator-(const Money& firstAmount, const Money& secondAmount)
{
int borrow = 0;
int finalCents = firstAmount.cents - secondAmount.cents;
if (finalCents < 0){
finalCents += 100;
borrow = 1;
}
int finalDollars = firstAmount.dollars - secondAmount.dollars - borrow;
return Money(finalDollars, finalCents);
}
// Multiply two Money objects
Money operator*(const Money& money, int n)
{
return money.amount * n;
}
// Divide two Money objects
Money operator/(const Money& money, int n)
{
int quotient = money.amount / n;
// check if there isn't a remainder
if ( quotient * n == 0)
return money.amount / n;
else // there's a remainder
return money.dollars / n + money.cents / (n * 100);
}
// Define the output operator
ostream& operator<<(ostream& outputStream, const Money& money)
{
outputStream << money.amount;
return outputStream;
}
最后,在我的测试 Money.cpp 的主要方法中,我有:
#include "Money.h"
using namespace std;
int main()
{
Money m1(-35),m2(53, 35);
//Test operator == (false)
cout << "m1 == m2 = " << (m1 == m2 ? "true" : "false") << endl;
Money m3(-35),m4(35);
//Test operator < (true)
cout << "m3 < m4 = " << (m3 < m4 ? "true" : "false") << endl;
Money m5(-35),m6(53, 35);
//Test operator > (false)
cout << "m5 > 6 = " << (m5 > m6 ? "true" : "false") << endl;
Money m7(12,50),m8(25,55);
// .50 & .50 = .05
//Test operator +
cout << "m7 + m8 = $" << (m7 + m8) << endl;
//~ Money m9(5,75), m10(100);
//~ // .75 - 0 = $-94.25
//~ //Test operator -
//~ cout << "m9 - m10 = $" << m9 - m10 << endl;
//~ Money m11(25,75);
//~ int n = 5;
//~ // .75 * = 8.75
//~ //Test operator *
//~ cout << "m11 * m12 = $" << m11 * n << endl;
//~ Money m13(115,75);
//~ n = 3;
//~ // 5.75 / = .58333
//~ //Test operator /
//~ cout << "m13 / n = $" << m13 / n << endl;
return 0;
}
显然,我得到了答案:m7 + m8 = .94066e-324
。而答案应该是 38.05 美元。
我在这里卡了好久了。如果有人能耐心地解释我搞砸的地方,那就太好了。感谢您的帮助。
问题是您的 operator <<
输出了 amount
成员。然而它是未初始化的:
// Define the output operator
ostream& operator<<(ostream& outputStream, const Money& money)
{
outputStream << money.amount; // not initialized
return outputStream;
}
如果您查看 main
中的 cout
,您会发现:
(m7 + m8)
这个returns一个临时的Money
对象,它将被复制构造(不是默认构造)。复制构造函数是编译器生成的(没关系,但请参阅下面我的评论)。由于 m7
和 m8
都没有设置 amount
,您将垃圾值复制到临时 Money
对象,因此是垃圾值。
此外,由于 double
变量 amount
未初始化,您的代码调用了未定义的行为,编译器生成的复制构造函数将一个垃圾双精度值复制到另一个双精度精度。您永远不想尝试操作未初始化的浮点变量,除非操作涉及初始化变量。
最重要的是,当你构造你的对象时,你应该努力将所有 成员初始化为一个已定义的状态。是否使指针为 NULL,是否使双精度等于 0,等等。您的成员变量应设置为有效状态。
您在本例中使用的构造函数重载未设置 'amount'。
Money::Money(int newDollars, int newCents)
{
dollars = newDollars;
cents = newCents;
}
你的operator+
也没有
Money operator+(const Money& firstAmount, const Money& secondAmount)
{
//assume cents < 100
int carry = 0;
int finalCents = firstAmount.cents + secondAmount.cents;
if (finalCents >= 100){
carry += 1;
finalCents -= 100;
}
int finalDollars = firstAmount.dollars + secondAmount.dollars + carry;
return Money(finalDollars, finalCents);
}
您的 operator<<
显示 amount
并且未初始化。
您应该在所有构造函数重载中设置 amount
。这可能最好通过让它们都调用一个在一个地方执行初始化的私有函数来实现。
更好地摆脱这种以 2 种表示形式(dollars/cents 和数量)存储值的重复性。仅将其存储为其中之一,维护起来会简单得多。
另请注意,如果您调用无参数构造函数,您的 dollars 和 cents 成员变量同样未初始化。
我刚开始学习 类 C++,我在处理重载算术运算符时遇到了很多问题。首先,在我的头文件中我有:
#ifndef MONEY_H
#define MONEY_H
#include <iostream>
using namespace std;
class Money{
public:
Money(int dollars, int cents);
Money(int dollars);
Money();
int getDollars() const {return dollars;};
int getCents() const {return cents;};
void setDollarsAndCents(int dollars, int cents);
double getAmount() const {return amount ;};
void setAmount(double amount);
// Define operator functions for comparison operators
friend bool operator==(const Money& firstAmount, const Money& secondAmount);
friend bool operator<(const Money& firstAmount, const Money& secondAmount);
friend bool operator>(const Money& firstAmount, const Money& secondAmount);
//Define operator functions for arithmetic operators
friend Money operator+(const Money& firstAmount, const Money& secondAmount);
friend Money operator-(const Money& firstAmount, const Money& secondAmount);
friend Money operator*(const Money& money, int n);
friend Money operator/(const Money& money, int n);
//Define the output and input operator
friend ostream& operator<<(ostream& outStream, const Money& money);
private:
int dollars, cents;
double amount;
};
#endif
然后我在实现文件上实现了operator+,Money.cpp
#include "Money.h"
// Construct a money object with dollars and cents
Money::Money(int newDollars, int newCents)
{
dollars = newDollars;
cents = newCents;
}
// Construct a money object with JUST the dollars
Money::Money(int newDollars)
{
dollars = newDollars;
cents = 0;
}
// Construct a money object with no arguments (default amount = 0)
Money::Money()
{
amount = 0.0;
}
// Set dollars and cents
void Money::setDollarsAndCents(int newDollars, int newCents)
{
dollars = newDollars;
cents = newCents;
}
// Set monetary amount
void Money::setAmount(double newAmount)
{
//convert cents automatically if >= 100
newAmount = dollars + cents/100.0;
amount = newAmount;
}
// Test if two Money objects are equal or not
bool operator==(const Money& firstAmount, const Money& secondAmount)
{
return (firstAmount.amount == secondAmount.amount);
}
// Test if the first operand is less than the second operand
bool operator<(const Money& firstAmount, const Money& secondAmount)
{
return (firstAmount.amount < secondAmount.amount);
}
// Test if the first operand is greater than the second operand
bool operator>(const Money& firstAmount, const Money& secondAmount)
{
return (firstAmount.amount > secondAmount.amount);
}
// Add two Money objects
Money operator+(const Money& firstAmount, const Money& secondAmount)
{
//assume cents < 100
int carry = 0;
int finalCents = firstAmount.cents + secondAmount.cents;
if (finalCents >= 100){
carry += 1;
finalCents -= 100;
}
int finalDollars = firstAmount.dollars + secondAmount.dollars + carry;
return Money(finalDollars, finalCents);
}
// Subtract two Money objects
Money operator-(const Money& firstAmount, const Money& secondAmount)
{
int borrow = 0;
int finalCents = firstAmount.cents - secondAmount.cents;
if (finalCents < 0){
finalCents += 100;
borrow = 1;
}
int finalDollars = firstAmount.dollars - secondAmount.dollars - borrow;
return Money(finalDollars, finalCents);
}
// Multiply two Money objects
Money operator*(const Money& money, int n)
{
return money.amount * n;
}
// Divide two Money objects
Money operator/(const Money& money, int n)
{
int quotient = money.amount / n;
// check if there isn't a remainder
if ( quotient * n == 0)
return money.amount / n;
else // there's a remainder
return money.dollars / n + money.cents / (n * 100);
}
// Define the output operator
ostream& operator<<(ostream& outputStream, const Money& money)
{
outputStream << money.amount;
return outputStream;
}
最后,在我的测试 Money.cpp 的主要方法中,我有:
#include "Money.h"
using namespace std;
int main()
{
Money m1(-35),m2(53, 35);
//Test operator == (false)
cout << "m1 == m2 = " << (m1 == m2 ? "true" : "false") << endl;
Money m3(-35),m4(35);
//Test operator < (true)
cout << "m3 < m4 = " << (m3 < m4 ? "true" : "false") << endl;
Money m5(-35),m6(53, 35);
//Test operator > (false)
cout << "m5 > 6 = " << (m5 > m6 ? "true" : "false") << endl;
Money m7(12,50),m8(25,55);
// .50 & .50 = .05
//Test operator +
cout << "m7 + m8 = $" << (m7 + m8) << endl;
//~ Money m9(5,75), m10(100);
//~ // .75 - 0 = $-94.25
//~ //Test operator -
//~ cout << "m9 - m10 = $" << m9 - m10 << endl;
//~ Money m11(25,75);
//~ int n = 5;
//~ // .75 * = 8.75
//~ //Test operator *
//~ cout << "m11 * m12 = $" << m11 * n << endl;
//~ Money m13(115,75);
//~ n = 3;
//~ // 5.75 / = .58333
//~ //Test operator /
//~ cout << "m13 / n = $" << m13 / n << endl;
return 0;
}
显然,我得到了答案:m7 + m8 = .94066e-324
。而答案应该是 38.05 美元。
我在这里卡了好久了。如果有人能耐心地解释我搞砸的地方,那就太好了。感谢您的帮助。
问题是您的 operator <<
输出了 amount
成员。然而它是未初始化的:
// Define the output operator
ostream& operator<<(ostream& outputStream, const Money& money)
{
outputStream << money.amount; // not initialized
return outputStream;
}
如果您查看 main
中的 cout
,您会发现:
(m7 + m8)
这个returns一个临时的Money
对象,它将被复制构造(不是默认构造)。复制构造函数是编译器生成的(没关系,但请参阅下面我的评论)。由于 m7
和 m8
都没有设置 amount
,您将垃圾值复制到临时 Money
对象,因此是垃圾值。
此外,由于 double
变量 amount
未初始化,您的代码调用了未定义的行为,编译器生成的复制构造函数将一个垃圾双精度值复制到另一个双精度精度。您永远不想尝试操作未初始化的浮点变量,除非操作涉及初始化变量。
最重要的是,当你构造你的对象时,你应该努力将所有 成员初始化为一个已定义的状态。是否使指针为 NULL,是否使双精度等于 0,等等。您的成员变量应设置为有效状态。
您在本例中使用的构造函数重载未设置 'amount'。
Money::Money(int newDollars, int newCents)
{
dollars = newDollars;
cents = newCents;
}
你的operator+
Money operator+(const Money& firstAmount, const Money& secondAmount)
{
//assume cents < 100
int carry = 0;
int finalCents = firstAmount.cents + secondAmount.cents;
if (finalCents >= 100){
carry += 1;
finalCents -= 100;
}
int finalDollars = firstAmount.dollars + secondAmount.dollars + carry;
return Money(finalDollars, finalCents);
}
您的 operator<<
显示 amount
并且未初始化。
您应该在所有构造函数重载中设置 amount
。这可能最好通过让它们都调用一个在一个地方执行初始化的私有函数来实现。
更好地摆脱这种以 2 种表示形式(dollars/cents 和数量)存储值的重复性。仅将其存储为其中之一,维护起来会简单得多。
另请注意,如果您调用无参数构造函数,您的 dollars 和 cents 成员变量同样未初始化。