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 calculator
s 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:
- Be aware of the issues of using
using namespace std
Be aware of the issues of usingusing namespace std
. - You don't appear to be using
<conio.h>
, so just remove it. - You don't need to use
std::endl
in so many places. A simple"\n"
for outputting a newline should be sufficient. More info about that here here.
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 calculator
s 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:
- Be aware of the issues of using
using namespace std
. - You don't appear to be using
<conio.h>
, so just remove it. - You don't need to use
std::endl
in so many places. A simple"\n"
for outputting a newline should be sufficient. More info about that here.
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 calculator
s 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:
- Be aware of the issues of using
using namespace std
. - You don't appear to be using
<conio.h>
, so just remove it. - You don't need to use
std::endl
in so many places. A simple"\n"
for outputting a newline should be sufficient. More info about that here.
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 calculator
s 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:
- Be aware of the issues of using
using namespace std
. - You don't appear to be using
<conio.h>
, so just remove it. - You don't need to use
std::endl
in so many places. A simple"\n"
for outputting a newline should be sufficient. More info about that here.