complex
is just like an array of doubles:
complex nums[100];
class ManyDoubles { private: double nums[100]; public: ManyDoubles(); double set(int ix, double val) { nums[ix] = val; } double sum() const; }; ManyDoubles::ManyDoubles() { // fails: double nums[100] = {0.0}; // declares a new array!! // also doesn't work: // nums = {}; - does nothing! // correct initialization: for(int i = 0; i < 100; ++i) nums[i] = 0.0; } double ManyDoubles::sum() const { double result = 0.0; for(int i = 0; i < 100; ++i) result += nums[i]; return result; }
Time *submissions[20] = { new Time(10, 22), new Time(13, 45), new MilTime(13, 18), new MilTime(23, 0), new MilTime(5, 10), new Time(1, 15) }; ... for(int i = 0; submissions[i] != nullptr; ++i) { submissions[i]->print(cout); cout << endl; }
class Insect { public: Insect(std::string species); }; class Firefly : public Insect { public: Firefly(std::string species) : Insect(species) { // more code } };
new
, it
stays on the heap until the program exits
delete
to "undo" allocating the space:
Task *todo = new Task("eat", 0.75); // code to process the item delete todo;
new
operation
todo
:
delete todo; cout << todo->description(); // dangling pointer reference! // (random result)
todo
to point to nothing:
delete todo; todo = nullptr;so later references to
todo
will cause run-time errors.
delete
for every new
TaskList *items = new TaskList(); items->add(new Task("sleep", 8.0)); items->add(new Task("eat breakfast", 0.25)); items->add(new Task("learn something", 6.0)); ... delete items;
TaskList
:
class TaskList { ... public: ~TaskList() { for(int i = 0; i < count; ++i) delete todo[i]; }
TaskList
also deletes any tasks
currently on the list
TaskList *items = new TaskList(); Task *x = new Task("do something", 1.0); items->add(x); delete items; delete x; // breaks the heap!
class Student { string name; string address; double total_grade_points, total_credits; Schedule *schedule; public: Student() : schedule{new Schedule}, total_grade_points{0.0}, total_credits{0.0}, name(""), address("") { } virtual ~Student() { delete schedule; } double gpa() const { return total_grade_points / total_credits; } virtual void add(Course *new_course); }; class SpecialStudent : public Student { Certificate *cert; public: ~SpecialStudent() { delete cert; } void add(Course *new_course); ... };
~Student() { delete schedule; }
virtual ~Student() { delete schedule; }
void doSomethingNice() { Student someStudent; ... // destructor called here }
class AbstractContainer { public: virtual double& operator[](int index) = 0; virtual int size() = 0; virtual ~AbstractContainer() { } };
void swapFirstLast(AbstractContainer &nums) { int last_index = nums.size() - 1; assert(last_index > 0); // last_index can be 0! double save = nums[0]; nums[0] = nums[last_index]; nums[last_index] = save; } ... declare xs to be some class derived from AbstractContainer ... and read data into the container // first shall be last, last shall be first: swapFirstLast(xs);
double* p = &nums[0]; *p = nums[last_index];
{ int n; cin >> n; FixedVector xs(n); ... { FixedVector ys(n * 2); ... } // ys destroyed here ... } // xs destroyed here
void initializeUpSequence(AbstractContainer *container, double start, double step) { double value = start; for(int ix = 0; ix < container->size(); ++ix) { (*container)[ix] = value; value += step; } } ... AbstractContainer *some_numbers = new MegaVector(); initializeUpSequence(some_numbers, 1.0, 0.1); double sum = 0.0; for(int i = 0; i < some_numbers->size(); ++i) sum += (*some_numbers)[i];
void initializeUpSequence(AbstractContainer container, double start, double step);(with no *)
void initializeUpSequence(AbstractContainer &container, double start, double step);however, the pointer version makes it more obvious that the object is on the heap
double first_and_last(MegaVector nums) { double sum = nums[0] + nums[nums.size() - 1]; return sum; }
MegaVector xs; ... double s = first_and_last(xs);
double first_and_last(MegaVector &nums) { double sum = nums[0] + nums[nums.size() - 1]; return sum; }
double sum = (nums[0] = 0.0) + nums[nums.size() - 1];
double first_and_last(const MegaVector &nums) { double sum = nums[0] + nums[nums.size() - 1]; return sum; }
bool operator<(const Student &a, const Student &b) { return a.name() < b.name(); }