/*
* Copyright ©2026 Soham Pardeshi. All rights reserved.
* Permission is hereby granted to students registered for University of
* Washington CSE 333 for use solely during Summer Quarter 2026 for
* purposes of the course. No other use, copying, distribution, or
* modification is permitted without prior written consent. Copyrights
* for third-party components of this work must be honored. Instructors
* interested in reusing these course materials should contact the author.
*/
#ifndef INTARRAY_H_
#define INTARRAY_H_
#include <cstddef> // for size_t
// The running example for this lecture: a class that OWNS a heap buffer.
//
// Rule of Three: because IntArray manages an owned resource (the heap array),
// it needs all three of the destructor, copy constructor, and copy assignment
// operator. If you write one, you almost certainly need all three.
class IntArray {
public:
explicit IntArray(size_t size); // acquire the resource
~IntArray(); // 1. release the resource
IntArray(const IntArray& other); // 2. deep-copy on construction
IntArray& operator=(const IntArray& other); // 3. free old, then deep-copy
size_t Size() const { return size_; }
int At(size_t i) const { return data_[i]; }
void Set(size_t i, int v) { data_[i] = v; }
private:
// Members are initialized in DECLARATION order, not init-list order, so
// size_ must be declared BEFORE data_ (the constructor reads size_ to size
// the buffer).
size_t size_; // declared FIRST
int* data_; // heap buffer we OWN
};
#endif // INTARRAY_H_