Array and struct ExampleΒΆ

// array_intro.cpp

#include "cmpt_error.h"
#include <iostream>
#include <cmath>
#include <cassert>

using namespace std;

// By default, everything is public in a struct.
struct Point {
    int x, y;

    // default constructor (takes no inputs)
    Point()
    : x(0), y(0)
    { }

    Point(int a, int b)
    : x(a), y(b)
    { }

    // copy constructor: makes a copy of another Point object
    Point(const Point& other)
    : x(other.x), y(other.y)
    { }

    // Every C++ object has a special pointer called this that points to the
    // object itself.
    double dist(const Point& other) {
        double dx = this->x - other.x;
        double dy = this->y - other.y;
        return sqrt(dx*dx + dy*dy);
    }
}; // struct Point

void print(const Point& p) {
    cout << "(" << p.x << ", " << p.y << ")";
}

void println(const Point& p) {
    print(p);
    cout << "\n";
}

void test1() {
 Point origin;
    Point p{-2, 4};
    Point q{p};

    println(origin);
    println(p);
    println(q);

    cout << "dist(p, origin): " << p.dist(origin) << "\n";
}

class Intvec {
private:
    int* arr; // pointer to the underlying array
    int cap; // capacity of the underlying array, i.e. its size
    int sz; // number of elements in the array

    // doubles the capacity of the underlying array
    void resizeArray() {
        cap = 2 * cap;

        // make the new array
        int* newArr = new int[cap];

        // copy elements into the new array
        for(int i = 0; i < sz; ++i) {
            newArr[i] = arr[i];
        }

        // delete the old array
        delete[] arr;

        // set up the new array
        arr = newArr;
    }

public:
    // Default constructor that creates an empty array of size 0.
    // The initial capacity of 10 is just a guess.
    Intvec()
    : cap(10), sz(0)
    {
        arr = new int[cap];
    }

    // This destructor is called automatically when an Intvec is deleted or
    // leaves scope.
    ~Intvec() {
        delete[] arr;  // note the use of []
    }

    // Some of the following functions have "const" before their bodies. This
    // indicates that the function does not modify the object. In other words,
    // using "const" like this makes the function read-only.
    bool empty() const {
        return sz == 0;
    }

    int size() const {
        return sz;
    }

    int capacity() const {
        return cap;
    }

    int get(int i) const {
        if (i < 0 || i >= sz) cmpt::error("get: index out of bounds");
        return arr[i];
    }

    void set(int i, int x) {
        if (i < 0 || i >= sz) cmpt::error("set: index out of bounds");
        arr[i] = x;
    }

    // Add x to the end of this Intvec, re-sizing the underlying array if
    // necessary.
    void append(int x) {
        if (sz >= cap) {
            resizeArray();
        }
        arr[sz] = x;
        sz++;
    }

    void print() const {
        cout << "[";
        for(int i = 0; i < sz; ++i) {
            cout << arr[i] << " ";
        }
        cout << "]";
    }

    void println() const {
        print();
        cout << "\n";
    }

}; // class Intvec

void test2() {
    Intvec v;
    assert(v.size() == 0);
    assert(v.empty());

    for(int i = 1; i <= 150; ++i) {
        v.append(i);
        v.print();
        cout << " size=" << v.size() << ", cap=" << v.capacity() << "\n";
    }

}

int main() {
   // test1();
    test2();
}