-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice13.cpp
74 lines (58 loc) · 1.36 KB
/
practice13.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
#include <iostream>
#include <string>
using namespace std;
const int MAX_FRIENDS = 20;
struct Person
{
string name;
int age;
Person *bestFriend;
Person *friends[MAX_FRIENDS];
Person getBestFriend();
void addFriend(Person &newFriend);
void viewFriends();
int noOfFriends = 0;
};
void Person::addFriend(Person &newFriend)
{
friends[noOfFriends] = &newFriend;
noOfFriends += 1;
}
Person Person::getBestFriend()
{
return *bestFriend;
}
void Person::viewFriends()
{
cout << name << "'s friends: " << endl;
for (int i = 0; i < noOfFriends; i++)
{
cout << (*friends[i]).name << endl;
}
}
int main()
{
// pointers are variables the store memory
// addresses instead of value.
Person p1;
Person p2;
Person p3;
p1.name = "John";
p1.age = 20;
p1.bestFriend = &p2;
p2.name = "Adam";
p2.age = 18;
p2.bestFriend = &p3;
p3.name = "Alex";
p3.age = 18;
p3.bestFriend = &p1;
cout << p1.name << " and " << p1.getBestFriend().name << " are best friends." << endl;
cout << p2.name << " and " << p2.getBestFriend().name << " are best friends." << endl;
cout << p3.name << " and " << p3.getBestFriend().name << " are best friends." << endl;
cout << endl
<< endl;
p1.viewFriends();
p1.addFriend(p2);
p1.addFriend(p3);
p1.viewFriends();
}