-1

I'm new to programming and I have to Overload the += operator , + Operator and ++ post and prefix operator so the following operations work properly but I'm stuck.

Here is the number class:

#include <iostream>

class Number
{
private:
    int m_value;
public:
    Number(int value = 0);

    std::ostream& print(std::ostream& ostr = std::cout)const;
};
std::ostream& operator<<(std::ostream& ostr, const Number& N);





using namespace  std;
void prn(const Number& a, const Number& b, const Number& c)
{
    cout << "c  a  b " << endl
        << c << " " << a << " " << b << endl;
    cout << "--------------------" << endl;
}
int main()
{
    Number a{ 10 }, b{ 20 }, c;
    c = a + b;
    prn(a, b, c);
    c = ++a;
    prn(a, b, c);
    c = a += b;
    prn(a, b, c);
    c = b++;
    prn(a, b, c);
    return 0;
}

/// output:
/*
c  a  b
30 10 20
--------------------
c  a  b
11 11 20
--------------------
c  a  b
31 31 20
--------------------
c  a  b
20 31 21
----------------
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Starting point: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Oct 30 '21 at 03:43
  • If you have implemented these methods (and the number class) please add them. Right now we don't have enough information to give you more than generalizations. – user4581301 Oct 30 '21 at 03:46
  • @user4581301 done – Sufiyan Mustafa Oct 30 '21 at 03:48
  • Need to see more. Read through the link I posted above and take a shot at implementing the operators. If what you make doesn't work, ask targeted questions about the problems you ran into. – user4581301 Oct 30 '21 at 03:57
  • @SufiyanMustafa My answer gives your desired output. Check it out. – Jason Oct 30 '21 at 04:24

1 Answers1

0

The below program shows how to overload different operator correctly.

#include <iostream>
class Number 
{
    //overload operator+ so that we can add two Number objects
    friend Number operator+(const Number &lhs, const Number &rhs);
    
    //overload operator<< so that we can use std::cout<< c 
    friend std::ostream& operator<<(std::ostream &os, const Number& num);
    int m_value = 0;
    
    public:
        Number(int value = 0);
        //overload operator+= . This operator will be used inside operator+ definition
        Number& operator+=(const Number &rhs);
        Number& operator++();
        Number operator++(int);
};

Number::Number(int value): m_value(value)
{
   
}
Number operator+(const Number &lhs, const Number &rhs)
{
    Number sum = lhs;  // copy data members from lhs into sum
    sum += rhs;        // add rhs into sum using compound operator
    return sum;
}
Number& Number::operator+=(const Number &rhs)
{
    m_value += rhs.m_value;
    
    return *this;
}

std::ostream& operator<<(std::ostream &os, const Number& num)
{
    os << num.m_value;
    return os;
}
Number& Number::operator++()
{
   ++m_value;
   
   return *this;
}
Number Number::operator++(int)
{
        Number old = *this; // copy old value
        operator++();  // prefix increment
        return old;    // return old value
}

void prn(const Number& a, const Number& b, const Number& c)
{
    std::cout << "c  a  b " << std::endl
        << c << " " << a << " " << b << std::endl;
    std::cout << "--------------------" << std::endl;
}
int main()
{
     Number a{ 10 }, b{ 20 }, c;
    c = a + b;
    prn(a, b, c);
    c = ++a;
    prn(a, b, c);
    c = a += b;
    prn(a, b, c);
    c = b++;
    prn(a, b, c);
    return 0;
}

Now you'll get your desired output as can be seen here.

c  a  b 
30 10 20
--------------------
c  a  b 
11 11 20
--------------------
c  a  b 
31 31 20
--------------------
c  a  b 
20 31 21
--------------------
Jason
  • 36,170
  • 5
  • 26
  • 60