-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCS60 HW4.cpp
More file actions
150 lines (107 loc) · 3.05 KB
/
CS60 HW4.cpp
File metadata and controls
150 lines (107 loc) · 3.05 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// CS60 HW4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class IntBag {
const static int CAPACITY = 128;
const static int MULT = 2;
public:
IntBag(); //Defualt Constructor
IntBag(const IntBag &a);//Copy Constructor
~IntBag();//Deconstructor
void operator =(const IntBag& a);//= operator overload(same as Copy Construcor)
int operator [](int index);//[] operator that returns [index]
void add(const int item);//add method which adds int item to the array
int getSize()const;//getter function for size_
void fill(int c);//fill method which fills in the entire array with int c
void setSize(int newsize);//setSize method which changes the size to int newsize
void clear();//clear method which wipes the array, resets to intial value
void deleteFunc(int pos);//delete method which will delete one index from the array
private:
int *array_;
int size_;
};
IntBag::IntBag() {
size_ = 100;
array_ = new int[size_];
}
IntBag::IntBag(const IntBag &a) {
size_ = a.getSize();
array_ = new int[size_];
for (int i = 0; i < size_; i++) {
array_[i] = a.array_[i];
}
}
IntBag::~IntBag() {
delete[] array_;
}
void IntBag::operator =(const IntBag& a) {
size_ = a.getSize();
array_ = new int[size_];
for (int i = 0; i < size_; i++) {
array_[i] = a.array_[i];
}
}
int IntBag::operator [](int index) {
return array_[index];
}
void IntBag::add(const int item) {
if (size_ >= CAPACITY) {
size_ *= MULT;
}
size_++;
array_ = new int[size_];
array_[size_ - 1] = item;
}
int IntBag::getSize()const {
return size_;
}
void IntBag::setSize(int newsize) {
size_ = newsize;
array_ = new int[size_];
}
void IntBag::fill(int c) {
for (int i = 0; i < size_; i++) {
array_[i] = c;
}
}
void IntBag::clear() {
delete[] array_;
size_ = 100;
array_ = new int[size_];
}
void IntBag::deleteFunc(int pos) { //Do i need to make a temp array and then swap values?
array_[pos] = 0;
}
ostream& operator <<(ostream& out, IntBag &a) {
out << "[ ";
for (int i = 0; i < a.getSize(); i++) {
out << a[i] << " ";
}
out << " ]";
return out;
}
int main()
{
IntBag a;
cout << "Size:" << a.getSize() << endl << "Array:" << a << endl << endl;
a.setSize(3);
cout << "Size:" << a.getSize() << endl << "Array:" << a << endl << endl;
a.add(55);
cout << "Size:" << a.getSize() << endl << "Array:" << a << endl << endl;
IntBag b(a);
cout << "Size:" << b.getSize() << endl << "Array:" << b << endl << endl;
cout << "b[3]: " << b[3] << endl << endl;
b.fill(6);
cout << "Size:" << b.getSize() << endl << "Array:" << b << endl << endl;
b.deleteFunc(3);
cout << "Size:" << b.getSize() << endl << "Array:" << b << endl << endl;
a.clear();
cout << "Size:" << a.getSize() << endl << "Array:" << a << endl << endl;
b = a;
cout << "Size:" << b.getSize() << endl << "Array:" << b << endl << endl;
system("pause");
return 0;
}