Hi guys,

I'm really sorry to bother you with a simple question. I'm doing an assignment with operator overloading at the moment & seem to not understand how operator+ should be used in regards to my program. I understand the point of it & how to get it when using ints/long/double/float...etc, but not when the only two available variables are a bool & a char array.

BigInt.h

private:
          char digits[100];
          int number;
          bool plusSign;
public:
          BigInt operator+(BigInt bi);

main.cpp

BigInt number1;
          BigInt number2(3484858);
          BigInt number3("89347893478349234045");

          cout << number1 + number2 + number3 << endl;

Is it possible?

First off, thank you very much for whoever bothers even reading this & secondly "sorry for being such a noob".

wait, can you repeat your question 1 more time, in a more clear and less
anxious manner.Thanks and btw, this

BigInt number1;
          BigInt number2(3484858);
          BigInt number3("89347893478349234045");

          cout << number1 + number2 + number3 << endl;

is possible.

To be able to assign your object to various things you need to make some constructors, it will then be simple to implement your addition operator.
Constructor being:

public:
    BigInt();
    BigInt(const int);
    BigInt(const char*);
    BigInt(const BigInt&);

Of course you can add more..
Then your operator+ is simply:

BigInt& operator+(const BigInt&);

If this is for an assignment it may have to be gone through another way but this is how I would do it.

Edited 6 Years Ago by u8sand: n/a

Then your operator+ is simply:

BigInt& operator+(const BigInt&);

To preserve the correct addition (+) semantics and handle memory correctly operator+ has to return a object not a reference as do all binary operators also since it should not change the actual object it is called upon it can be const so it should be

BigInt operator+(const BigInt&) const;

Edited 6 Years Ago by Banfa: n/a

Hi guys,

I'm really sorry to bother you with a simple question. I'm doing an assignment with operator overloading at the moment & seem to not understand how operator+ should be used in regards to my program. I understand the point of it & how to get it when using ints/long/double/float...etc, but not when the only two available variables are a bool & a char array.

Finance.h

#ifndef _Finance_H_
#define _Finance_H_

#include <iostream>
#include <string>

using namespace std;

class Finance {
 private:
          char digits[100];
          int number;
          bool plusSign;
 public:
          // constructors
          Finance();
          Finance(const int);
          Finance(const char*){}
          Finance(const Finance&);

          // friends (for I/O)
          friend ostream& operator<<(ostream& out, Finance bi);
          friend istream& operator>>(istream& in, Finance bi);

          // accessor & mutator
          bool isPositive() {return plusSign;}
          bool isNegative() {return !plusSign;}
          void setPositive() {plusSign = true;}
          void setNegative() {plusSign = false;}

          // overloaded math operators
          Finance operator+(Finance bi);

          // Overloaded assignment operator
          string operator=(string s);

          // overloaded rational operators
          bool operator>(Finance bi);
          bool operator==(Finance bi);
          bool operator<(Finance bi)  {return !(operator>(bi) || operator==(bi));};
          bool operator>=(Finance bi) {return !operator<(bi);};
          bool operator<=(Finance bi) {return !operator>(bi);};
          bool operator!=(Finance bi) {return !operator==(bi);};
};

#endif
#include <iostream>
#include <string>
#include "Finance.h"

using namespace std;

// constructors
Finance::Finance() {
  int i;
  // start at zero;
  for (i=99;i>=0;i--) {
    digits[i] = 0;
  }
}

Finance::Finance(int number){
  int i;
  setPositive();
  if (number < 0) {
    setNegative();
    number = -number;
  }

  for (i=99;i>=0;i--) {
    digits[i] = number % 10;
    number /= 10;
  }

}

Finance::Finance(string s){
  *this = s;
}

// friends (for I/O)
ostream& operator<<(ostream& out, Finance bi){
  int i;
  bool leading0 = true;
  cout << (bi.isPositive()?'+':'-');

  for (i = 0;i < 99;i++) {
    int outi = static_cast<int>(bi.digits[i]);
    if (outi != 0 || !leading0 ) {
      out << outi;
      leading0 = false;
    }
  }
  // last digit always printed
  out << static_cast<int>(bi.digits[99]);
  return out;
}

istream& operator>>(istream& in, Finance& bi){
  string s;
  in >> s;
  bi = s;
  return in;
}

string Finance::operator=(string s) {
  // should remove all spaces from s
  setPositive();
  int pos = 0;
  if (s[0] == '-') {
    setNegative();
    pos = 1;
  }
  int startingPoint = 100 - s.length();
  if (isNegative()) {
    startingPoint ++;
  }
  for (int j = 0;j<startingPoint;j++) {
    digits[j] = 0;
  }
  while (pos < s.length()) {
    digits[startingPoint] = static_cast<int>(s[pos] - '0');
    startingPoint ++;
    pos ++;
  }
  return s;
}

Finance Finance::operator+(Finance bi){
    return bi.number + bi.x;
}

bool Finance::operator>(Finance bi){
    if(bi.digits[100] > digits[100])
        return true;
    else
        return false;
}

bool Finance::operator==(Finance bi){
}

main.cpp

#include <iostream>
#include "Finance.cpp"

using namespace std;

int main() {

          // three constructors

          Finance number1;
          Finance number2(3484858);
          Finance number3("89347893478349234045");

          // must be printable

          cout << number1 << endl;
          cout << number2 << endl;
          cout << number3 << endl;

          // must overload istream& operator>>()  [read as a string]


          // operator+ needs to be there too

          cout << number1 + number2 + number3 << endl;

}

Here's the updated, entire code to see what I'm working with. As you can see, the confusion sets in with the lack of understanding what can be added together here...I know you guys stated creating Constructors can help, but I'm not getting how it will help? :( Sorry, I know I'm being a hassle.

Hi guys,

I've tried creating a constructor with which to put all my variables into as well as multiple ones with no success at getting operator+ to work. Is there something I am missing?

operator+ is not in any way dependent on having any sort of constructor unless you have chosen to implement it in this form (for a general type T)

T T::operator+(const T& rhs)
{
    return T(*this) += rhs;
}

Which is dependent on having a copy constructor and an operator+=.

However you do not have to implement your operator+ like this you can just implement it directly without reference to other members of the class.

Hi guys,

I've attempted working on the code since early morning (around 9), and I've had 0 luck. After about a hundred attempts, I'm losing faith. Can anyone tell me what the heck I'm missing? I realize this is the typical 'homework junkie' begging post, but I've got to turn this in a couple hours. I've been doing my other assignments the past couple weeks so I haven't spent any time except today on it. I am very, very sorry for the 'college kid' post here, but I really need the help.

#include <iostream>
using namespace std;

class Finance {
 private:
          char digits[100];
          int number;
          bool plusSign;
 public:
          // constructors
          Finance();
          Finance(string s);
          Finance(int x);

          // friends (for I/O)
          friend ostream& operator<<(ostream& out, Finance bi);
          friend istream& operator>>(istream& in, Finance bi);

          // accessor & mutator
          bool isPositive() {return plusSign;}
          bool isNegative() {return !plusSign;}
          void setPositive() {plusSign = true;}
          void setNegative() {plusSign = false;}

          string operator=(string s);

          // overloaded math operators
          //friend Finance operator+(Finance bi);
};

Finance::Finance(string s){
  *this = s;
}

Finance::Finance(int x)
{
    number = x;
}

// friends (for I/O)
ostream& operator<<(ostream& out, Finance bi){
  int i;
  bool leading0 = true;
  cout << (bi.isPositive()?'+':'-');

  for (i = 0;i < 99;i++) {
    int outi = static_cast<int>(bi.digits[i]);
    if (outi != 0 || !leading0 ) {
      out << outi;
      leading0 = false;
    }
  }
  // last digit always printed
  out << static_cast<int>(bi.digits[99]);
  return out;
}

istream& operator>>(istream& in, Finance& bi){
  string s;
  in >> s;
  bi = s;
  return in;
}

string Finance::operator=(string s) {
  // should remove all spaces from s
  setPositive();
  int pos = 0;
  if (s[0] == '-') {
    setNegative();
    pos = 1;
  }
  int startingPoint = 100 - s.length();
  if (isNegative()) {
    startingPoint ++;
  }
  for (int j = 0;j<startingPoint;j++) {
    digits[j] = 0;
  }
  while (pos < s.length()) {
    digits[startingPoint] = static_cast<int>(s[pos] - '0');
    startingPoint ++;
    pos ++;
  }
  return s;
}

Finance operator+(Finance bi)
{
    Finance result;
    result = bi.number + number;

    return result;
}

int main() {

          // three constructors

          Finance number1;
          Finance number2(3484858);
          Finance number3("89347893478349234045");

          // must be printable

          cout << number1 << endl;
          cout << number2 << endl;
          cout << number3 << endl;

          // must overload istream& operator>>()  [read as a string]


          // operator+ needs to be there too

          //cout << number1 + number2 + number3 << endl;

}

Does anyone know how to solve my problem? :(

Basically, what I'm asking is how do you overload an operator+ using a string & int?

const Test operator+(const Test& lhs, const Test& rhs)
{
      cout << lhs.mX << rhs.mX;
      return Test( lhs.mX + rhs.mX );
}

int main()
{
      const Test t1(3);
      const Test t2("4");
      const Test t3 = t1 + t2;

      return 0;
}

Edited 6 Years Ago by godsgift2dagame: n/a

You have not provided enough information for us to help properly.

In Finance you have not indicated what the member data represents or how you would add 2 together 2 values.

In Test you have not provided the type definition so we don't even know the type of Test::mX

What you have supplied is so sketchy that there are no obvious errors in it.

Edited 6 Years Ago by Banfa: n/a

This article has been dead for over six months. Start a new discussion instead.