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

objective: This homework is an advanced introduction to C++ classes with operato

ID: 3855528 • Letter: O

Question

objective: This homework is an advanced introduction to C++ classes with operator overloading. The goal is to gain better knowledge of classes and utilize operator overloading where appropriate. Checklist: 1. Review the assignment specification 2. Review Binary Numbers 3. Please review how to separate your class into an implementation, definition, and driver sections. These will all become separate files as outlined below. 4. Create a binary·cpp (should contain the class implementation only) , binary.h (should contain your class definition, no functions), binary-sandbox.cpp (should contain the main() and #include "binary.h") file within your IDE. 5. Develop a Binary class definition. Do not bother at first to create the individual functions. You can do that later. Have it checked by your instructor. 6. Use the binary-sandbox.cpp to test your Class. 7. Create and test each operator one at a time. The easiest operators for this assignment may be to start with the "cin" and "cout" operators. 8. Verify sample output by providing corresponding sample hand calculations. 9. Make sure that your code complies with the C++ coding standards. 10.Submit the binary.cpp, binary-sandbox.cpp, and binary.h Specification

Explanation / Answer

Given below is the needed for the question along with output. In case of any issues, post a comment and I will reply. If happy with the answer, please rate it . Thank you

binary.h

#ifndef binary_h

#define binary_h

#include <iostream>

#include <fstream>

using namespace std;

class binary

{

string binary_value; //the binary string

long decimal_value; // the decimal equivalent

long convertToDecimal(string binary); // a function to convert a binary value to decimal

string convertToBinary(long decimal); // a function to convert from decimal to binary string

public:

binary(); //default constructor

binary(string ); //constructor to initalize using a binary string

binary operator + (const binary &other); //operator +

binary operator - (const binary &other); //operator -

binary operator / (const binary &other); //operator /

binary operator * (const binary &other); //operator *

friend ostream & operator << (ostream &out, const binary &bin); //cout operator

friend istream & operator >> (istream &in, binary &bin); //cin operator

string toString();

  

};

#endif /* binary_h */

binary.cpp

#include "binary.h"

#include <cmath>

binary::binary()

{

binary_value = "0";

decimal_value = 0;

}

binary::binary(string b) //constructor to initalize using a binary string

{

binary_value = b;

decimal_value = convertToDecimal(binary_value);

}

binary binary::operator + (const binary &other) //operator +

{

long ans = decimal_value + other.decimal_value;

string bin = convertToBinary(ans);

return binary(bin);

}

binary binary::operator - (const binary &other) //operator -

{

long ans = decimal_value - other.decimal_value;

string bin = convertToBinary(ans);

return binary(bin);

}

binary binary::operator / (const binary &other) //operator /

{

long ans = decimal_value / other.decimal_value;

string bin = convertToBinary(ans);

return binary(bin);

}

binary binary::operator * (const binary &other) //operator *

{

long ans = decimal_value * other.decimal_value;

string bin = convertToBinary(ans);

return binary(bin);

}

ostream& operator << (ostream &out, const binary &bin) //cout operator

{

out << bin.binary_value;

return out;

}

istream& operator >> (istream &in, binary &bin) //cin operator

{

in >> bin.binary_value;

bin.decimal_value = bin.convertToDecimal(bin.binary_value); //store the decimal equivalent as well

return in;

}

string binary::toString()

{

return binary_value;

}

long binary::convertToDecimal(string binary)

{

long num = 0;

int idx = binary.length() - 1, power = 0;

char bit;

for( ; idx >= 0; idx--, power++)

{

bit = binary[idx];

if(bit == '1')

num = num + pow(2, power);

}

return num;

}

string binary::convertToBinary(long num)

{

long n = num;

string bin = "";

int rem;

char digit_char;

while(n > 0)

{

rem = n % 2; //remainder wil be 0 or 1

digit_char = rem + '0'; //convert to a character from numeric 0 or 1

bin = digit_char + bin;

  

n = n / 2;

}

  

if(bin.empty()) //when num is 0

bin = "0";

  

return bin;

}

binary-sandbox.cpp

#include "binary.h"

#include <iostream>

using namespace std;

int main()

{

binary b1, b2;

  

cout << "Enter binary number 1: ";

cin >> b1;

  

cout << "Enter binary number 2: ";

cin >> b2;

  

binary b3 = b1 + b2;

binary b4 = b1 - b2;

binary b5 = b1 / b2;

binary b6 = b1 * b2;

  

cout << "b1 = " << b1 << endl;

cout << "b2 = " << b2 << endl;

cout << "b3 = b1 + b2 = " << b3 << endl;

cout << "b4 = b1 - b2 = " << b4 << endl;

cout << "b5 = b1 / b2 = " << b5 << endl;

cout << "b6 = b1 * b2 = " << b6 << endl;

}

output

Enter binary number 1: 1001
Enter binary number 2: 0011
b1 = 1001
b2 = 0011
b3 = b1 + b2 = 1100
b4 = b1 - b2 = 110
b5 = b1 / b2 = 11
b6 = b1 * b2 = 11011