Instruction:
For practice write a header file code that includes your code, and write a .cpp file that tests your header file.
Your .cpp file must test for the size, and capacity of the underlying vector. It also must have a for loop that sends index values to append function, and then have your .cpp file to print all the values in your underlying array in the form of {1, 2, 3, ..., 25}.
My code:
There are some bugs which I don't know how to fix. Any help?
Question:
1. Is it possible to print things out using friend ostream& operator<< ?
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
|
// tutorial.h
#include <iostream>
#include <vector>
#include "cmpt_error.h"
#include <cassert>
#include <iomanip>
#include <cassert>
#include <cmath>
using namespace std;
class test {
private:
int cap; // length of underlying pvay
vector <int*> pv;
//int* pv; // pointer to the underlying pvay
int size; // # of elements in this test from user's perspective
public:
// Default constructor (takes no parameters)
test()
: cap(10), pv{new int[cap]}, size(0)
{ }
test(int sz, int fill_value)
: cap(10), size(sz)
{
if (size < 0) cmpt::error("can't construct test of negative size");
if (size > 0) cap += size;
pv.push_back(new int[cap]);
for(int i = 0; i < size; ++i){
pv[i].push_back(fill_value);
}
}
// Copy constructor
test(const test& other)
: cap(other.cap), pv(new int[cap]), size(other.size)
{
for(int i = 0; i < size; ++i) {
pv[i] = other.pv[i];
}
}
~test() {
delete[] pv;
}
int get_size() const {
return size;
}
int get_cap() const {
return cap;
}
int get(int i) const {
if (i < 0 || i > size) cmpt::error("get: index out of bounds");
return pv[i];
}
// Note that set is not a const method because it modifies the underlying
// pvay.
void set(int i, int x) {
if (i < 0 || i > size) cmpt::error("get: index out of bounds");
pv[i] = x;
}
friend ostream operator<< (ostream& output, const test& use){
if (pv.size() == 0) {
cout << "{}";
} else {
cout << "{";
cout << pv[0];
for (int i = 1; i < pv.size(); ++i) { // i starts at 1 (not 0)
cout << ", " << pv[i];
}
cout << "}";
}
cout << "\n";
}
void append(int x) {
if (size >= cap) {
cap = 2 * cap; // double the cap
int* new_pv = new int[cap]; // make new underlying pvay
// twice size of old one
for(int i = 0; i < size; ++i) { // copy elements of v
new_pv[i] = pv[i]; // into new_pv
}
delete[] pv; // delete old pv
pv = new_pv; // assign new_pv
}
assert(size < cap);
pv[size] = x;
size++;
}
};
|
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
|
// .cpp file
#include <iostream>
#include "Tutorials.h"
#include "cmpt_error.h"
#include <vector>
#include <cassert>
#include <iomanip>
#include <cassert>
#include <cmath>
using namespace std;
int main() {
int_vec v;
cout << " v.size(): " << v.get_size() << "\n";
cout << "v.capacity(): " << v.get_capacity() << "\n";
for(int i = 0; i < 25; ++i) {
v.append(i);
}
// Question: How to use ostream operator for output ?
}
|
Note: There are no outputs, since I my code contains bugs and no output option in main.
Note: To debug the code, you need to have cmpt_error.h header file, if not, just comment it out.