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

C++ problem. Please explain your code,Thank You! The Number class shown here can

ID: 3576812 • Letter: C

Question

C++ problem. Please explain your code,Thank You!

The Number class shown here can represent a number, except that each part of the number is stored in an array. For example, the number 3278 would be stored as [3, 2, 7, 8]. Copy and paste this code into your answer and complete the method to overload the == operator and print the number.

class Number {

private:
int arr[4];
int index;
int size;

public:

Number(int s) {
size = s; //number of digits in the number. Max is 4.
for(int i = 0; i < 4; i++){

arr[i] = 0;

}

index = 0;

}

void addDigit(int dig) {
// ex. arr currently = [1, 2, 0, 0], addDigit(5) is called, arr should = [1, 2, 5, 0]

arr[index] = dig;

index++;
}

void printNumber() {
// print the number such as 124 etc.

//Your logic here

}

bool friend operator==(const Number& n, const Number& n2) {
// Your Logic here
}
};

Explanation / Answer

#include <iostream>
using namespace std;

class Number {
private:
int arr[4];
int index;
int size;
public:
Number(int s) {
size = s; //number of digits in the number. Max is 4.
for(int i = 0; i < 4; i++){
arr[i] = 0;
}
index = 0;
}
void addDigit(int dig) {
// ex. arr currently = [1, 2, 0, 0], addDigit(5) is called, arr should = [1, 2, 5, 0]
arr[index] = dig;
index++;
}
void printNumber() {
// print the number such as 124 etc.
//Your logic here

for(int i=0;i<size;i++)
{
    cout<<arr[i]; //print all digits of number in the loop
}
}
bool friend operator==(const Number& n, const Number& n2) {
// Your Logic here
int flag =1 ;
if(n.size != n2.size)
return false;
else
{
for(int i=0;i<n.size;i++)
{
   if(n.arr[i] != n2.arr[i]) //check all digits of numbers and set flag =0 if on an index the values are not equal
   flag = 0;
}
if(flag ==0)
return false;
else
return true;
}
}
};
int main()
{
   Number n(4);
   Number n1(4);
  
   n.addDigit(3);
   n.addDigit(2);
   n.addDigit(7);
   n.addDigit(8);
   cout<<" n=";
   n.printNumber();
  
   n1.addDigit(1);
   n1.addDigit(2);
   n1.addDigit(3);
   n1.addDigit(4);
  
   cout<<" n1=";
   n1.printNumber();
  
   if(n == n1)
   cout<<" n and n1 are equal";
   else
   cout<<" n and n1 are not equal";
  
   return 0;
}

output:

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