0% found this document useful (0 votes)
4 views2 pages

Binary Number Manipulation in C++

The document contains a C++ class named 'binary' that handles binary number operations. It includes methods for reading a binary number, checking its validity, exchanging '0's with '1's, and displaying the binary number. The main function demonstrates the usage of these methods, although there are comments indicating some design considerations regarding method accessibility.

Uploaded by

yugs35941
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Binary Number Manipulation in C++

The document contains a C++ class named 'binary' that handles binary number operations. It includes methods for reading a binary number, checking its validity, exchanging '0's with '1's, and displaying the binary number. The main function demonstrates the usage of these methods, although there are comments indicating some design considerations regarding method accessibility.

Uploaded by

yugs35941
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include<iostream>

#include<string>
using namespace std;
class binary{
private: string s;
void binary :: checkmate(void){
for (int i = 0; i < [Link](); i++)
{
if ([Link](i)!='1' && [Link](i)!='0')
{
cout<<"This isn't in a binary form."<<endl;
exit(0);
}
}
}
void checkmate(void);
public:
void read(void);
void checkmate(void);
void exchange(void);
void display(void);
};
void binary :: read(void){
cout<<"Enter binary number - ";
cin>>s;
}

void binary :: exchange(void){


checkmate(); //Here this function can be operated as it can be used by only the
methods of the class and not any outside method like int main().
for (int i = 0; i < [Link](); i++)
{
if ([Link](i)=='0')
{
[Link](i)='1';
}
else{
[Link](i)='0';
}

}
}
void binary :: display(void)
{
cout<<"Displaying your binary number = ";

for (int i = 0; i < [Link](); i++)


{
cout<<[Link](i);
}
cout<<endl;
}
int main(){
binary b;
[Link]();
// [Link]();Here if we dont want user to use this we can make this private
[Link]();
[Link]();
[Link]();
//Nested: Nesting helps us to use our functions in another function without using
the above syntaxes
//Here the code will give an error and the reason behind this is we have to write
the checkmate method code inside the private class and not outside.
}

You might also like