-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path8a- octal class
99 lines (79 loc) · 1.76 KB
/
8a- octal class
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
88
89
90
91
92
93
94
95
96
97
98
99
/************************
9) a)Write a C++ program to create a class called OCTAL, which has the characteristics of an octal
number. Get the Input using >> operator , 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.
************************/
#include <iostream>
#include <math.h>
using namespace std;
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;
}
int main()
{
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<<endl<<"The value of k= "<<k;
y=h+k;
cout<<endl<<"The value of h+k in decimal notation, y = "<<y<<endl;
return 0;
}