-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_encapsulation.cpp
72 lines (59 loc) · 1.8 KB
/
class_encapsulation.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
#include <iostream>
class Account {
bool is_active = false; // private by default
public:
std::string login = "";
std::string email = "";
unsigned int id = 0;
protected:
const int connections_max_number = 10;
/* For being used in the array definition
const component must be also static. */
int connections[10];
private:
unsigned short int connections_number = 0;
public:
void display();
bool hasConnections() {
return (connections_number > 0);
};
bool addConnection(unsigned int connected_account_id) {
if (connections_number == connections_max_number)
return false;
connections[connections_number] = connected_account_id;
connections_number++;
return true;
}
};
void Account::display() {
std::cout << std::boolalpha
<< "ID: " << id << std::endl
<< "Login: " << login << std::endl
<< "E-mail: " << email << std::endl
<< "Is active: " << is_active << std::endl
<< "Has connections: " << hasConnections() << std::endl
<< std::noboolalpha;
}
class SocialMediaAccount : public Account {
public:
bool isFriend(unsigned int checking_account_id) {
for(int connection_account_id : connections) {
if (checking_account_id == connection_account_id)
return true;
}
return false;
}
};
int main() {
SocialMediaAccount timothy;
timothy.login = "tim";
timothy.email = "timothy.muppetone@gmail.com";
// timothy.is_active = true; // cannot access private component
timothy.display();
std::cout << std::endl << std::boolalpha;
timothy.addConnection(100);
std::cout << "Has connections: " << timothy.hasConnections() << std::endl;
std::cout << "Is ID 100 a friend? " << timothy.isFriend(100) << std::endl;
std::cout << "Is ID 200 a friend? " << timothy.isFriend(200) << std::endl;
std::cout << std::noboolalpha;
}