Repository navigation
Expand file tree
/
Copy pathAddressBook.cpp
More file actions
125 lines (113 loc) · 2.71 KB
/
Copy pathAddressBook.cpp
File metadata and controls
125 lines (113 loc) · 2.71 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
Thomas Ruland
CSCI 3250 Data Structures and Algorithms
Programming Assignment 1
g++ on ubuntu
*/
#include<iostream>
#include<string>
#include "AddressBook.hpp"
using namespace std;
//default constructor
Contact::Contact()
{
mFirst="John"; //generic name
mLast="Doe"; //also generic name
mEmail="jdoe@my.apsu.edu"; //generic email
}
//fancier constructor
Contact::Contact(string first,string last,string email)
{
mFirst=first; //your name
mLast=last; //also your name
mEmail=email; //email that you use
}
Contact Contact::operator=(const Contact &rhs)
{
mFirst=rhs.mFirst;
mLast=rhs.mLast;
mEmail=rhs.mEmail;
next=rhs.next;
return rhs;
}
//default constructor
AddressBook::AddressBook()
{
head=NULL; //head has to by null
}
//destructor
AddressBook::~AddressBook()
{
Contact *temp=head; //temp pointer for indirection
while((temp->next)!=NULL) //while loop if you couldnt tell by the key word while
{
temp=head->next;
delete head; //deleting head after we keep track of its next then setting heads to its old next
head=temp;
}
head=NULL;
}
//copy constructor
AddressBook::AddressBook(const AddressBook& old)
{
head=old.head; //im not going to make the joke
}
void AddressBook::addContact(Contact bob)
{
Contact *newcontact=new Contact; //allocating new contact
newcontact->mFirst=bob.mFirst; //setting first name
newcontact->mLast=bob.mLast; //setting last name
newcontact->mEmail=bob.mEmail; //setting email
newcontact->next=head; //putting the newly allocated contact into the LL
head=newcontact;
}
void AddressBook::displayContacts()
{
Contact *temp=head; //almost the same thing as the destructor except it doesnt delete things
while(temp != NULL)
{
cout << temp->mFirst
<< " " << temp->mLast
<< " @ " << temp->mEmail
<< endl;
temp=temp->next;
}
}
void AddressBook::hasContact(string f,string l)
{
Contact *temp=head; //temp
bool found=false;
while(temp!=NULL) //while loop
{
if(temp->mFirst == f)
{
if(temp->mLast == l)
{
cout << temp->mFirst
<< " "
<< temp->mLast
<< " exists" << endl;
found=true; //if object found it doesnt display the error message below
}
}
temp=temp->next;
}
if(!found) //this is the error message talked about above
{
cout << f << " " << l
<< " is not in your contacts" << endl;
}
}
//operator overload
AddressBook AddressBook::operator=(const AddressBook& rhs)
{
head=NULL;
Contact *temp=rhs.head;
while(temp!=NULL) //while looping
{
Contact replacement((temp->mFirst),(temp->mLast),(temp->mEmail));
addContact(replacement); //adding the newly copied contact to the newly created LL
temp=temp->next;
}
return rhs; //returning thingies is good
}