Constructor Overloading C++ to convert an octal number into binary
Write a program in C++ to convert an octal number into binary using constructor overloading.
#include<iostream> using namespace std; class binary { private: long int p=1; long int dec=0,i=1,j,d; long int binno=0; public: binary(int n) { for(j=n;j>0;j=j/10) { d=j%10; if(i==1) { p=p*1; } else { p=p*8; } dec=dec+(d*p); i++; } i=1; for(j=dec;j>0;j=j/2) { binno=binno+(dec%2)*i; i=i*10; dec=dec/2; } cout<<"Octal number "<<n<<" equalant to Binary "<<binno; } binary(double x) { for(j=x;j>0;j=j/10) { d=j%10; if(i==1) { p=p*1; } else { p=p*8; } dec=dec+(d*p); i++; } i=1; for(j=dec;j>0;j=j/2) { binno=binno+(dec%2)*i; i=i*10; dec=dec/2; } cout<<"Octal number "<<x<<" equalant to Binary "<<binno; } }; int main() { int select; cout<<"Enter option to Select(0-1): "; cin>>select; switch(select) { case 0: { int n; cout<<"Enter octal number to convert: "; cin>>n; binary b(n); break; } case 1: { double x; cout<<"Enter octal number to Convert: "; cin>>x; binary b(x); break; } default: cout<<"Invilide Choice:"; } }
Output
Enter an octal number to convert: 4
Octal number 4 is equivalent to Binary: 100