-
Notifications
You must be signed in to change notification settings - Fork 7
/
nesting_member_function.cpp
90 lines (79 loc) · 1.68 KB
/
nesting_member_function.cpp
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
// OOPs - Classes and objects
// C++ --> initially called --> C with classes by stroustroup
// class --> extension of structures (in C)
// structures had limitations
// - members are public
// - No methods
// classes --> structures + more
// classes --> can have methods and properties
// classes --> can make few members as private & few as public
// structures in C++ are typedefed
// you can declare objects along with the class declarion like this:
/* class Employee{
// Class definition
} harry, rohan, lovish; */
// harry.salary = 8 makes no sense if salary is private
// Nesting of member functions
#include <iostream>
#include <string>
using namespace std;
class binary
{
private:
string s;
void chk_bin(void);
public:
void read(void);
void ones_compliment(void);
void display(void);
};
void binary::read(void)
{
cout << "Enter a binary number" << endl;
cin >> s;
}
void binary::chk_bin(void)
{
for (int i = 0; i < s.length(); i++)
{
if (s.at(i) != '0' && s.at(i) != '1')
{
cout << "Incorrect binary format" << endl;
exit(0);
}
}
}
void binary::ones_compliment(void)
{
chk_bin();
for (int i = 0; i < s.length(); i++)
{
if (s.at(i) == '0')
{
s.at(i) = '1';
}
else
{
s.at(i) = '0';
}
}
}
void binary::display(void)
{
cout<<"Displaying your binary number"<<endl;
for (int i = 0; i < s.length(); i++)
{
cout << s.at(i);
}
cout<<endl;
}
int main()
{
binary b;
b.read();
// b.chk_bin();
b.display();
b.ones_compliment();
b.display();
return 0;
}