Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a simple class (DoStuff) containing an int, and overload the operator+ as

ID: 3780507 • 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++

Explanation / Answer

#include <iostream>

using namespace std;

class Simple

{

int x;

public:

Simple operator +(const Simple &a);

//operator overloading using member function

Simple operator+(int value);

Simple operator-(int value);

Simple operator++(int value);

Simple operator--(int value);

void print(ostream& os);

//overloading << operator

friend ostream &operator<<( ostream &output, const Simple &s )

{

output << this.s<< " ";

return output;           

}

void getvalue()

   {

   cout<<"Enter a value :";

   cin>>x;

   }

};

Simple Simple::operator+(const Simple &a)

{

return (x+a.x);

}

Simple Simple::operator+(int value)

{

    return Simple (x+ value);

}

Simple Simple::operator-(int value)

{

    return Simple (x-value);

}

{

    return Simple (value+1);

}

Simple Simple::operator--(int value)

{

    return Simple (value-1);

}

void Simple::print(ostream& os)

{

os << this.x << " ";

}

int main() {

clrscr();

Simple obj1,obj2,result,result1,result2,result3;

obj1.getvalue();

obj2.getvalue();

   result = obj1+obj2;

   result1=obj1-obj2;

result2 = obj1++;

   result3=obj1--;

   cout<<"Sum of objects:"<< result;

   cout<<"Subtraction of objects:"<< result1;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote