How do i overload the operators in this code? header file below const int MAX_DI
ID: 3776698 • Letter: H
Question
How do i overload the operators in this code?
header file below
const int MAX_DIGITS = 100;
class bigNum
{
public:
void readBig();//function that reads an integer and turns it into
//a string and then puts the string back into an integer array.
void printBig();//function that prints out an integer array.
bigNum addBig(bigNum); // function that adds two large integers together.
private:
int num[MAX_DIGITS];//an array of type int that will be used to store arrays.
};
implementation file below:
#include "bigNum.h"
#include <iostream>
#include <string>
using namespace std;
// Function that reads an integer and turns it into a string and then puts
// the string back into an integer array.
void bigNum::readBig()
{
string read="";
cin>>read;
int len=0, i=0, save=0;
len= read.length();
while(i<=MAX_DIGITS-1)
{
num[i]=0;
i++;
}
for (i=0; i <= len-1; i++)
{
num[i] = int (read.at(i)-'0');
}
for (i=0;i<=len/2-1;i++)
{
save=num[i];
num[i]=num[len-1-i];
num[len-1-i]=save;
}
}
)// Function with a while loop that skips leading zeros and then uses a for loop to print the number.
void bigNum::printBig(
{
int digit=MAX_DIGITS-1;
while(num[digit]==0 && digit>0)
{
digit--;
}
for (int i=digit; i>=0; i--)
{
cout<<num[i];
}
}
// Function that adds the corresponding digits of the first two arrays and
//stores the answer in the third array. In a second loop, it performs the carry operation.
bigNum bigNum::addBig(bigNum num2)
{
bigNum sum;
int i=0;
while(i<=MAX_DIGITS-1)
{
sum.num[i]=0;
i++;
}
for (int i = MAX_DIGITS - 1; i >= 0; i--)
{
sum.num[i] = num[i] + num2.num[i];
}
//checks for carries by changing stored values of 10.
for (int i = 0; i <= MAX_DIGITS - 2; i++)
{
sum.num[i + 1] += sum.num[i] / 10;
sum.num[i] = sum.num[i] % 10;
}
return sum;
}
Source code below:
#include "bigNum.h"
#include <iostream>
using namespace std;
int main()
{
// Declare the three numbers, the first, second and the sum:
bigNum num1, num2, sum;
bool finished = false; // a boolean that tests if program is done running.
char response;
while (! finished) //while program is not done running
{
cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
num1.readBig(); //Performs readBig on num1
cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
num2.readBig(); //Performs readBig on num1
sum=num1.addBig(num2); //Equates sum from two inputs called into addBig.
num1.printBig(); //Performs printBig on num1.
cout << " + ";
num2.printBig(); //Performs printBig on num2.
cout << " = ";
sum.printBig(); //Performs printBig on sum.
cout << " ";
cout << "test again?";
cin>>response;
cin.ignore(900,' ');
finished = toupper(response)!= 'Y';
}
return 0;
}
Explanation / Answer
You can overload operator like +. Add its prototype in header file and add code of addBig() function in operator+ function like I have done in your code below
//Header file
const int MAX_DIGITS = 100;
class bigNum
{
public:
void readBig();//function that reads an integer and turns it into
//a string and then puts the string back into an integer array.
void printBig();//function that prints out an integer array.
bigNum addBig(bigNum); // function that adds two large integers together.
bigNum bigNum::operator+ (bigNum num2);
private:
int num[MAX_DIGITS];//an array of type int that will be used to store arrays.
};
Add this function in your cpp file
bigNum bigNum::operator+ (bigNum num2)
{
bigNum sum;
int i=0;
while(i<=MAX_DIGITS-1)
{
sum.num[i]=0;
i++;
}
for (int i = MAX_DIGITS - 1; i >= 0; i--)
{
sum.num[i] = num[i] + num2.num[i];
}
//checks for carries by changing stored values of 10.
for (int i = 0; i <= MAX_DIGITS - 2; i++)
{
sum.num[i + 1] += sum.num[i] / 10;
sum.num[i] = sum.num[i] % 10;
}
return sum;
}
// Test your program with this main function
int main()
{
// Declare the three numbers, the first, second and the sum:
bigNum num1, num2, sum;
bool finished = false; // a boolean that tests if program is done running.
char response;
while (! finished) //while program is not done running
{
cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
num1.readBig(); //Performs readBig on num1
cout << "Please enter a number up to " << MAX_DIGITS << " digits: ";
num2.readBig(); //Performs readBig on num1
sum=num1+num2; //Equates sum from two inputs called into addBig.
num1.printBig(); //Performs printBig on num1.
cout << " + ";
num2.printBig(); //Performs printBig on num2.
cout << " = ";
sum.printBig(); //Performs printBig on sum.
cout << " ";
cout << "test again?";
cin>>response;
cin.ignore(900,' ');
finished = toupper(response)!= 'Y';
}
return 0;
}
// Similarly you can overload -, * and / operators
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.