Answer to Question #183453 in C++ for zain ul abdeen

Question #183453

Make a class in BaraBitKaar. Input two binary number. Sabtract two binary number.

BaraBitKaar Sabtract( BaraBitKaar b);

following example:

BaraBitKaar r;

r= p.sabtract(q); //this is how sabtract is supposed to be used to sabtract p and q. Notice r receives the return which is of type BaraBitKaar.

r.PrintBinary();



1
Expert's answer
2021-04-21T08:54:56-0400
#include <iostream>
#include <bitset>
#include <iomanip>
using namespace std;
class BaraBitKaar{
    string s;
    unsigned long n;
    public:
    BaraBitKaar(string s){
        this->s = s;
        this->n = bitset<8>(this->s).to_ulong();
    }
    void printBinary(){
        cout<<setw(9)<<this->s;
    }
    BaraBitKaar sabtract(BaraBitKaar b){
        int n = this->n - b.n;
        string r = bitset<8>(n).to_string();
        return BaraBitKaar(r);
    }
};
int main(){
    string s1 = "1010101", s2 = "101011";
    BaraBitKaar p(s1), q(s2);
    BaraBitKaar r = p.sabtract(q);
    cout<<setw(9)<<s1<<endl<<setw(1)<<"-"<<setw(8)<<s2<<endl;
    r.printBinary();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS