-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMember.cpp
78 lines (70 loc) · 2.53 KB
/
Member.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
/**
*cpp file of class Member
*Authors Alexey Titov and Shir Bentabou
*Version 1.0
**/
//library
#include "Member.h"
int cnt = 0; //initialize extern variable - in h file
//returns the number of members in the chat.
int Member::count(){
return cnt;
}
//Add follower to M and increase by one 'numfollowing' to the member that called the function.
void Member::follow(Member &m){
//user wants to follow himself
if (this==&m)
return;
//Here we make sure that this is not a duplicate request
for (int i=0; i<this->following.size(); i++){
if (this->following[i]==&m)
{
return;
}
}
//if follower doesn't exist in list, request is accepted
m.followers.push_back(this);
this->following.push_back(&m);
this->numfollowing++;
m.addFollower();
}
//Delete one follower of M and decrease by one 'numfollowing' to the member that called the function.
void Member::unfollow(Member &m){
//user wants to unfollow himself
if (this==&m)
return;
for (int i=0; i<this->following.size(); i++){
if (this->following[i]==&m){
this->numfollowing--;
m.delFollower();
for (int j=0;j<m.followers.size();j++)
if (this==m.followers[j])
{
m.followers.erase(m.followers.begin()+j);
break;
}
this->following.erase(this->following.begin()+i);
}
}
}
//Add follower to the member that called this function (private attribute).
void Member::addFollower(){
this->numfollowers++;
}
//Decrease by one follower the 'numfollowers' attribute of the member that called this function (private attribute), or throw exception if there are none.
void Member::delFollower(){
if (this->numfollowers>0){
this->numfollowers--;
}
else{
throw "Number of followers must be positive.";
}
}
//A get function to the numfollowers atttribute of the memeber that called the function.
int Member::numFollowers(){
return this->numfollowers;
}
//A get function to the numfollowers atttribute of the memeber that called the function.
int Member::numFollowing(){
return this->numfollowing;
}