# Constructor Implementation

Write a C++ program to create a class called OCTAL which has the characteristics of an octal number. Implement the following operations by writing an appropriate constructor and an overloaded operator +.
(i) OCTAL h = x; where x is an integer.
(ii) int y = h + k; where h is an OCTAL object and k is an integer
Display the OCTAL result by overloading the operator <<. Also, display the values of h and y.

Implementation of the above problem:
#include <iostream.h>
#include <conio.h>
#include <math.h>
class octal
{
private:
int o;
public:
octal();
octal(int);
~octal();
int dectooct(int x);
int octtodec(int x);
friend ostream &operator<<(ostream &print,octal);
int operator +(int);
};
octal::octal()
{
}
octal::octal(int x)
{
o=dectooct(x);
}
octal::~octal()
{
}
int octal::dectooct(int x)
{
int i=0,sum=0,rem;
while(x!=0)
{
rem=x%8;
sum=sum+rem*pow(10,i);
i++;
x=x/8;
}

return sum;
}
int octal::octtodec(int x)
{
int i=0,sum=0,rem;
while(x!=0)
{
rem=x%10;
sum=sum+rem*pow(8,i);
i++;
x=x/10;
}
return sum;
}
ostream &operator<<(ostream &print,octal x)
{
print<<x.o;
return print;
}
int octal::operator+(int x)
{
return octtodec(o)+x;
}
main()
{
clrscr();
int x,y,k;
cout<<endl<<"Enter the value of x in decimal notation:";
cin>>x;
octal h(x);
cout<<endl<<"Corresponding value of x in octal notation,h="<<h;
cout<<endl<<"Enter the value of k in decimal notation:";
cin>>k;
cout<<"The value of k="<<k;
y=h+k;
cout<<endl<<"The value of h+k in decimal notation,y="<<y;
getch();
return 0;
}

Thanks
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..