C++ Inline Function to convert an octal number into binary
Write a program in C++ to convert an octal number into binary using inline function.
The inline function inline int T4Tutorials::in()
and inline int T4Tutorials::cal()
helps to increase the execution time of a program. The programmer can make a request to the compiler to make the inline function as inline int T4Tutorials::in()
and inline int T4Tutorials::cal()
.
Making inline means that compiler can replace the function definitions of inline int T4Tutorials::in()
and inline int T4Tutorials::cal()
with the place where this function is called obj. in();
and b.cal();
.
The compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.
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 |
#include<iostream> using namespace std; class T4Tutorials { private: long int octalnum,p=1; long int dec=0,i=1,j,d; long int binno=0; public: int in(); int cal(); }; inline int T4Tutorials::in()
{ cout<<"Enter an octal number to convert: "; cin>>octalnum; } inline int T4Tutorials::cal() { for(j=octalnum;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 "<<octalnum<<" equalant to T4Tutorials "<<binno; } int main() { T4Tutorials b; b.in(); b.cal(); } |
Output
Enter an octal number to convert: 12
Octal number 12 is equivalent to Binary: 1100