-
Notifications
You must be signed in to change notification settings - Fork 24
/
ContactInfo.h
56 lines (46 loc) · 1.33 KB
/
ContactInfo.h
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
#ifndef CONTACTINFO_H
#define CONTACTINFO_H
// required for strlen and strcpy functions
#include <cstring>
class ContactInfo
{
private:
char* name;
char* phone;
public:
ContactInfo(char* n, char* p)
{
// allocate just enough memory for the name and phone number
name = new char[strlen(n) + 1];
phone = new char[strlen(p) + 1];
// copy the name and phone number to the allocated memory
strcpy(name, n);
strcpy(phone, p);
}
// copy constructor (const protects the argument object against modification)
ContactInfo(const ContactInfo &obj)
{
int nameSize = strlen(obj.name) + 1;
int phoneSize = strlen(obj.phone) + 1;
name = new char[nameSize];
phone = new char[phoneSize];
strcpy(name, obj.getName());
strcpy(phone, obj.getPhoneNumber());
}
// destructor
~ContactInfo()
{
delete [] name;
delete [] phone;
}
// const prevents any code calling the function from changing the name
const char* getName() const
{
return name;
}
const char* getPhoneNumber() const
{
return phone;
}
};
#endif