/* 7. Write a C++ program to create a class OCTAL which has the characteristics
of an octal number. Implement the following by writing an appropriate constructor and an overloaded operator +. Display the OCTAL object by
overloading the operator <<. Also display the values of k and y.
(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. */
#include<iostream.h>
#include<conio.h>
class OCTAL
{
int val;
public:
OCTAL(int x);
void Display();
friend int operator +(OCTAL x,int y);
};
int operator +(OCTAL x,int y)
{
int r1,r2,res,cy=0,num1=x.val,num2,mul=1,rem,sum=0;
OCTAL Temp(y);
Temp.Display();
num2=Temp.val;
while (num1 != 0 || num2 != 0)
{
r1 = num1 % 10;
r2 = num2 % 10;
res = r1 + r2 + cy;
if (res > 7)
{
cy = 1;
res = res - 8;
}
else
cy = 0;
sum += res * mul;
mul *= 10;
num1 = num1 / 10;
num2 = num2 / 10;
}
sum += cy*mul;
cout<<"\n\nResult : (OCTAL) "<<x.val<<" + (OCTAL) "<<Temp.val<<" = (OCTAL) ";
return (sum);
}
OCTAL::OCTAL(int x)
{ int sum=0,rem,mul=1,num = x;
while (num > 0)
{
rem = num % 8;
sum += rem * mul;
mul *= 10;
num = num / 8;
}
num = x;
val = sum;
}
void OCTAL::Display()
{
cout<<"\n\nOctal equivalent is "<<val;
}
void main()
{
int x;
cout<<"\nEnter an integer number : ";
cin>>x;
OCTAL h=x;
h.Display();
int k;
cout<<"\n\nEnter an integer number to add with the OCTAL value : ";
cin>>k;
int y=h+k;
cout<<y;
getch();
}