Constructor Overloading C++ to convert an octal number into binary
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#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