Create a simple class (DoStuff) containing an int, and overload the operator+ as
ID: 3784427 • Letter: C
Question
Create a simple class (DoStuff) containing an int, and overload the operator+ as a member function. Also, provide a print() member function that takes an ostream& as an argument and prints to that ostream&. Add a binary operator- and operator+ to the class as member functions. You should be able to use the objects in complex expressions such as a + b – c. Now add the operator++ and operator--, both prefix and postfix versions. Overload the << operator to provide the same functionality as the print() member function. Test the class to show that all requirements work correctly. IN C++
I am reposting this question because the solution I got the first time was incorrect and full of errors.
Explanation / Answer
A)
#include <iostream>
class Cents
{
private:
int a cents;
int b cents;
public:
Cents(int cents) { a_cents = cents; }
Cents(int cents) { b cents = cents; }
Cents operator+(int value);
int getCents() { return a_cents; }
};
Cents Cents::operator+(int value)
{
return Cents(a_cents + value);
return Cents(b cents-value);
}
int main()
{
Cents cents1(8);
Cents cents3= cents1 + 3;
std::cout << "I have " << cents2.getCents() << " cents. ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.