-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.cc
More file actions
99 lines (86 loc) · 1.75 KB
/
String.cc
File metadata and controls
99 lines (86 loc) · 1.75 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
// Implementation of variable length string class
// ECE4893/8893 Fall 2012
// George F. Riley
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "String.h"
using namespace std;
// Static counts
int String::DefaultCount;
int String::CreateCount;
int String::CopyCount;
int String::AssignCount;
int String::DestructCount;
String::String()
: st(0)
{ // Default to empty string
DefaultCount++;
}
String::String(const char* st0)
{
st = (char*)malloc(strlen(st0) + 1);
strcpy(st, st0);
CreateCount++;
}
// Copy constructor
String::String(const String& st0)
: st(0)
{
if (st0.c_str() != 0)
{ // Not Null string on rhs
st = (char*)malloc(strlen(st0.c_str()) + 1);
strcpy(st, st0.c_str());
}
CopyCount++;
}
// Destructor
String::~String()
{
free(st);
DestructCount++;
}
// Assignment operator
String& String::operator=(const String& rhs)
{
// Insure not self assignment
if (&rhs != this)
{
// Free original string
free(st);
st = (char*)malloc(strlen(rhs.c_str()) + 1);
strcpy(st, rhs.c_str());
}
AssignCount++;
return *this;
}
// Underlying string accessor
char* String::c_str() const
{
return st;
}
// ClearCounts
void String::ClearCounts()
{
DefaultCount = 0;
CreateCount = 0;
CopyCount = 0;
AssignCount = 0;
DestructCount = 0;
}
// ClearCounts
void String::PrintCounts()
{
cout << "DefaultCount " << DefaultCount << endl;
cout << "CreateCount " << CreateCount << endl;
cout << "CopyCount " << CopyCount << endl;
cout << "DestructCount " << DestructCount << endl;
cout << "AssignCount " << AssignCount << endl;
ClearCounts();
}
// Implement the printing function
ostream& operator<<(ostream& os, const String& st)
{
os << st.c_str();
return os;
}