Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

You can also overload operator>> instead of using getdata():

std::istream& operator>>(std::istream& in, calculator& obj)
{
 return in >> obj.val;
}

Similar approach with showdata(), using operator<<:

std::ostream& operator<<(std::ostream& out, calculator const& obj)
{
 return out << "value: " << obj.val;
}

As for your existing overloads, you shouldn't need to return a calculator. You appear to treat each calculator object as a single value. This makes each one to appear to be single-use, and the more values you wish to enter, the more objects thus the more code you'll need to have.

What you should do is maintain one object and have val maintained as you enter data. However, this will also mean that your arithmetic operators aren't needed. If you wish to keep them, then you'll have to approach this differently. For instance, you can calculate (not merely input) a final value for different calculators and use those operators to get a new value. This may look like a needless approach for a simple calculator, but at least you're still able to utilize these arithmetic operators.

Some miscellaneous notes:

You can also overload operator>> instead of using getdata():

std::istream& operator>>(std::istream& in, calculator& obj)
{
 return in >> obj.val;
}

Similar approach with showdata(), using operator<<:

std::ostream& operator<<(std::ostream& out, calculator const& obj)
{
 return out << "value: " << obj.val;
}

As for your existing overloads, you shouldn't need to return a calculator. You appear to treat each calculator object as a single value. This makes each one to appear to be single-use, and the more values you wish to enter, the more objects thus the more code you'll need to have.

What you should do is maintain one object and have val maintained as you enter data. However, this will also mean that your arithmetic operators aren't needed. If you wish to keep them, then you'll have to approach this differently. For instance, you can calculate (not merely input) a final value for different calculators and use those operators to get a new value. This may look like a needless approach for a simple calculator, but at least you're still able to utilize these arithmetic operators.

Some miscellaneous notes:

You can also overload operator>> instead of using getdata():

std::istream& operator>>(std::istream& in, calculator& obj)
{
 return in >> obj.val;
}

Similar approach with showdata(), using operator<<:

std::ostream& operator<<(std::ostream& out, calculator const& obj)
{
 return out << "value: " << obj.val;
}

As for your existing overloads, you shouldn't need to return a calculator. You appear to treat each calculator object as a single value. This makes each one to appear to be single-use, and the more values you wish to enter, the more objects thus the more code you'll need to have.

What you should do is maintain one object and have val maintained as you enter data. However, this will also mean that your arithmetic operators aren't needed. If you wish to keep them, then you'll have to approach this differently. For instance, you can calculate (not merely input) a final value for different calculators and use those operators to get a new value. This may look like a needless approach for a simple calculator, but at least you're still able to utilize these arithmetic operators.

Some miscellaneous notes:

Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

You can also overload operator>> instead of using getdata():

std::istream& operator>>(std::istream& in, calculator& obj)
{
 return in >> obj.val;
}

Similar approach with showdata(), using operator<<:

std::ostream& operator<<(std::ostream& out, calculator const& obj)
{
 return out << "value: " << obj.val;
}

As for your existing overloads, you shouldn't need to return a calculator. You appear to treat each calculator object as a single value. This makes each one to appear to be single-use, and the more values you wish to enter, the more objects thus the more code you'll need to have.

What you should do is maintain one object and have val maintained as you enter data. However, this will also mean that your arithmetic operators aren't needed. If you wish to keep them, then you'll have to approach this differently. For instance, you can calculate (not merely input) a final value for different calculators and use those operators to get a new value. This may look like a needless approach for a simple calculator, but at least you're still able to utilize these arithmetic operators.

Some miscellaneous notes:

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /