bigint Project

I need help with my project for my c++ class. I need to create a class called "bigint".

Requirements:
-You must use the class construct to implement your ADT.
-The ADT bigint need only work for positive numbers.
-Use a global constant value for the maximum size of the bigint that is, a constant sized array.

Implementation:
-The size of the bigint must be specified by a global constant
-A method (constructor) to initialize a bigint to zero.
-A method (constructor) to initialize a bigint to an int value you provide [0, maxint]. Example: bigint(128).
-A method (constructor) to initialize a bigint to a char[] you provide. You can assume what is provided is a valid bigint. Example: bigint("299793").
-A method to write a bigint that prints at most 60 digits per line.
-A method to compare if two bigints are equal. It should return a bool - true if equal and false otherwise.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #ifndef BIGINT_H
  #define BIGINT_H
  #include <iostream>
  const in BIGINT_SIZE = 128;

  class bigint{
  public:
     bigint();
     bigint(int);
     bigint(const char[]);
     bigint output(std::ostream &out);
  private:
     int number [BIGINT_SIZE];
  };
  #endif 


So that is my code. My current issue is that i need a default constructor to initialize bigint to 0.

The test file is:
1
2
3
4
5
6
7
8
9
  #include bigint.h
  #include <cassert>
  #include <iostream>
  int main()
  {
     bigint bi;
     assert(b == 0);
     std::cout << "0 == ";
  }


I cannot change the test file. Right now I am getting an error of "no match for 'operator==' in 'bi == 0'". I do not understand why. i though i initialized the bigint to 0 with "bigint();". I'm confused and desperately need help here.
you have some typos:
1
2
3
4
5
const in BIGINT_SIZE = 128; // should be const int

 #include bigint.h                   // should enclose bigint.h with quotes

assert ( bi == 0 );                  // can you compare object to an int ? 

Okay, I fixed the the first two typos. However, the "assert (bi == 0);" is provided by the instructor. So apparently I'm supposed to be able to compare an object and an int.
maybe bi must be a pointer to an object ? , and 0 means NULL
Last edited on
I cannot change anything listed in the test file. All I can change is my header file and have a .cpp file. I don't know how to make that bigint bi equal to 0.
> I am getting an error of "no match for 'operator==' in 'bi == 0'".
> I do not understand why
Because the compiler can't read minds. If you want to compare two things, you must tell it how to do it.
By instance
1
2
3
bool operator==(const bigint &a, int b){
   return false;
}
Okay. I understand what you're getting at. But I get this error:

bigint.h:22: error: ‘bool bigint::operator==(const bigint&, int)’ must take exactly one argument

I typed in "bool operator==(const bigint &a, int b);" letter by letter, but I get the above error.
You put it inside the class, Member functions refer to the caller object, so it already knows what the first parameter is
1
2
3
4
5
6
class bigint{
public:
   bool operator==(int b) const{
      return false;
   }
};



The previous example was as a global function
Okay.. Why do you automatically return false?
closed account (D80DSL3A)
You could do as ne555 suggests, but then a 2nd function for comparing bigints would be needed:
1
2
3
4
5
6
class bigint{
public:
   bool operator==(const bigint &a) const{
      // code to make the comparison
   }
};

You actually need only this 2nd one because your constructor bigint(int); tells the compiler how to "cast" an int to a bigint, which it will then substitute as the right operand in bigint == int.

I suggest for your header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef BIGINT_H
  #define BIGINT_H
  #include <iostream>
  const in BIGINT_SIZE = 128;

  class bigint{
  public:
     bigint();//  this will create a "default" bigint
     bigint(int);// this will convert an int to a bigint
     bigint(const char[]);// this will convert a char string to a bigint
     bool operator==( const bigint& );// this will compare a bigint to a bigint or anything which can be converted to a bigint
     bigint output(std::ostream &out);
  private:
     int number [BIGINT_SIZE];
     int numDigits;// something to keep track of how many elements are in actual use
  };
  #endif  
Last edited on
> Why do you automatically return false?
Because I have no idea how your class works and how to make the comparison.
Okay, so here's my header file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef BIGINT_H
#define BIGINT_H
#include <iostream>
const int BIGINT_SIZE = 128;

class bigint{
public:

        bigint();
        bigint(int);
        bigint(const char[]);
        bigint output(std::ostream &out);
        bool operator==(const bigint&);
private:
        int number[BIGINT_SIZE];
};

#endif 


My current problem is in my .cpp file. I am struggling on how to write the bigint output(std::ostream &out). My project calls for a method that needs to "write a bigint that prints at most 60 digits per line." For the life of me I cannot figure out how to write this method in the implementation file.
Topic archived. No new replies allowed.