Monday, 22 December 2014

C++ PROGRAM: Define a class octal that stores octal numbers(containing digits 0 to 7 only) and contains methods to do the following operations:

Define a class octal that stores octal numbers(containing digits 0 to 7 only) and contains methods to do the following operations:
a. All kind of Constructors
b. Set and Get the octal value.
c. Convert octal value to decimal
d. Convert decimal value to Octal.

SOLUTION:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class octal
{
public:
int oct;
octal(int val)
{
oct=val;
cout<<"The value of paramiterized constructor is: "<<oct;
}
octal(){
oct=0;
cout<<"\nThe value of default constructor is: "<<oct;
}
octal (octal & o){
oct=o.oct;
cout<<"\nThe value of copy constructor is: "<<oct;
}
void OctalDecimal();
void DecimalOctal();
};
void octal::OctalDecimal()
{
int n,r,s=0,i;
cout<<"\nEnter an octal number to find it's decimal equivalent:";
cin>>oct;
n=oct;
for(i=0;n!=0;i++)
{
r=n%10;
s=s+r*(int)pow(8,i);
n=n/10;
}
cout<<"\nThe equivalent number of "<<oct <<" is:"<<s<<endl;
}
void octal::DecimalOctal()
{
int n,r[10],i;
cout<<"\nEnter a decimal number to find it's octal equivalent:";
cin>>n;
cout<<"\nThe octal equivalent of "<<n<<" is:";
for(i=0;n!=0;i++)
{
r[i]=n%8;
n=n/8;
}
i--;
for(;i>=0;i--)
cout<<r[i];
}
int main()
{
clrscr();
octal c(113);
octal q;
octal p(c);
c.OctalDecimal();
c.DecimalOctal();
getch();
return 0;
}
OUTPUT:






No comments:

Post a Comment