-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.cpp
122 lines (91 loc) · 1.86 KB
/
data.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
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
#define _CRT_SECURE_NO_DEPRECATE // stop deprecation warnings, needs to be on first line
#include "data.h"
#include <iostream>
#include <iomanip>
#pragma warning(disable:4996)
using namespace std;
/** Data: constructor
* in: name
* out: name
* return:
**/
data::data(char const * const name) : name(new char[strlen(name)+1])
{
strcpy(this->name , name); // copy name
}
/** data: destructor
* in:
* out: none
* return:
**/
data::~data()
{
delete [] name ;
}
/** data: assignment operator overload
* in:
* out: none
* return:
**/
data& data::operator=(const data& data2)
{
if ( this == &data2) //check ofr self assignment
{
return *this;
}
delete [] name;
name = NULL;
name = new char[strlen(data2.name)+1]; // allocate new space
strcpy(this->name, data2.name); //copy
return *this;
}
/** data: getname
* in:
* out: none
* return: name
**/
char const * const data::getName() const
{
return this->name;
}
/** data: setname
* in: name
* out: none
* return:
**/
void data::setName (char const * const name)
{
delete [] this->name;
this->name = NULL;
this->name = new char[strlen(name)+1]; //alocate space
strcpy(this->name, name); //copy
}
/** data: less than operator overload - return true if d1 is "less than" d2, false otherwise
* in:
* out: none
* return: bool
**/
bool operator< (const data& d1, const data& d2)
{
return strcmp(d1.getName(), d2.getName()) < 0;
}
/** data: equal to operator overload - return true if d1 is "equal to" d2, false otherwise
* in:
* out: none
* return:
**/
bool operator== (const data& d1, const data& d2)
{
return strcmp(d1.getName(), d2.getName()) == 0;
}
/** data: operator<< - print the data instance referred to by outData
* in:
* out: none
* return:
**/
ostream& operator<< (ostream& out, const data& outData)
{
out << outData.name;
//out << endl;
return out;
}