-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path40nesting_of_member_functions.cpp
58 lines (55 loc) · 1.41 KB
/
40nesting_of_member_functions.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
#include<iostream>
#include<string>
using namespace std;
class binary{
private:
void check(); // is a private function
public:
string s;
void getS(); // are public functions
void rev();
void display();
};
void binary :: getS(){
cout<<"Enter a binary number: ";
cin>>s;
check(); // this private function can be used from here
// validity of entered binary number will be checked in this same function
// didn't even need to mention the object here
// this is called nesting of member functions
}
void binary :: check(){
for(int i=0; i<s.length(); i++){
if(s.at(i)!='0' && s.at(i)!='1'){
cout<<"Incorrect binary number entered.\n";
exit(0);
}
}
cout<<"Valid binary number entered.\n";
}
void binary :: rev(){
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(){
cout<<"It's one's compliment is: ";
// for (int i=0; i<s.length(); i++){
// cout<<s.at(i);
// }
cout<<s;
cout<<endl;
}
int main(){
binary num; // binary is class and num is an object
num.getS();
// num.check(); // private function is inaccesible from here
num.rev();
num.display();
return 0;
}