Question: Please don't copy and paste the answer to this problem from an outside source, only authentic code. Thank You!! Obey the Rules of Pain. A

Please don't copy and paste the answer to this problem from an outside source, only authentic code. Thank You!!

Obey the Rules of Pain.

A few things to remember about templates:

  1. All the code is in the h file
  2. When testing a template, any method you don't call gets deleted. So make sure you test them all, or you might secretly have one that doesn't compile.
  3. If you make your Vector not a template (change all the T's to int or something) it becomes easy to test as long as you remember to re-template it. (VS has trouble autocompleting a template since the code doesn't exist yet.) This can accidentally destroy your homework if you aren't careful.
  4. I'm going to drop your vector in to my project, so don't bother changing main unless you want to test something differently.

You need to implement all of the methods, and that's it. You cannot add any new properties. You cannot break any of the unbreakable rules in the announcements section. Your solutions must conform to the Big O notations next to each method. If you can't figure out what a method does by the name, look it up on cplusplus since the names will match.

The hard part of this is the memory management, so take special care with Reserve, the constructors, and the destructor. I provided a test file but please test any way you want. Make sure it doesn't crash. And if it doesn't compile it doesn't even count as code so that is the saddest 0 you can get. Don't let that happen.

Expecting points from homework that doesn't compile is like having a history essay due and turning in a banana.

Yes the only "new" is in reserve. That's what the comment in the constructor was supposed to mean. Don't duplicate code.

Vector.h file:

#pragma once // I am going to match the names that STL uses so you don't get confused in the real world. I'm skipping operator [] for a specific reason that // doesn't come up for a few weeks.

template // Whatever main puts in the <> gets find-replaced to every "T" class Vector { T* mData; int mSize; // Size is how much data. Capacity is how much memory. int mCapacity;// For testing purposes, initialize this to 15. Whenever you allocate new memory, double it.

T mUndefined;// Lots of STL functions say that doing something naughty gets "undefined behavior". It could throw, crash, make you eggs, or return nonsense. // Return this undefined one if anybody ever tries to go out of bounds. // I've also seen this done with a static. I'll do that in List later

public: Vector()// O(1) { mSize = 0; mData = nullptr; Reserve( 15 ); // If you put a new in here, you'd be duplicating the reserve code. Feel free to call non-virtual methods of your own here. // (You can't call a virtual method because the whole object isn't finished constructing yet.) } // Big 3 ~Vector() { // Free all memory } Vector( const Vector& tOther ) : Vector()// O(n) { } // There is a sneaky way to combine assignment and copy by having one call the other. They are 90% the same. Vector& operator =( const Vector& tRHS )// O(n) { return *this; // This line is weird so I'm just giving it to ya. It has to be the last line. It's just the definition of an = } void PushBack( const T& tItem )// O(1) { } void PopBack()// O(1) { } T& At( int tWhere )// O(1) { return mUndefined; } void Clear()// O(1) { } int Size()// O(1) { return 0; } void Reserve( int tCount )// O(n) { // This is the hard one. The rest are 1-4 lines. I'm not saying reducing line count is something anybody cares about, // I'm just pointing out the scope. Sometimes people get to fifty lines on something and then complain it's too hard. } int Capacity()// O(1) { return 0; } };

Vector.cpp file

#include "Vector.h" #include #include

using namespace std;

int main() { // Remember, you have to test all template functions since if you don't call it then it gets deleted. // Otherwise a week from now you'll call the one you forgot and it will crash. Vector tTester; tTester.PushBack( 0 ); tTester.PushBack( 1 ); tTester.PushBack( 2 ); tTester.PushBack( 3 ); tTester.PushBack( 4 ); tTester.PushBack( 5 ); tTester.PushBack( 6 ); tTester.PushBack( 7 ); tTester.PushBack( 8 ); tTester.PushBack( 9 );

cout << "If any of the lines below are false, there is a bug. This is not a comphrehensive test. Add what you need. "; cout << tTester.Size() << " == 10" << endl;// 10 cout << tTester.At( 5 ) << " == 5" << endl;// 5 cout << tTester.At( 0 ) << " == 0" << endl;// 0

cout << tTester.Capacity() << " == 15" << endl; // 15 tTester.PushBack( 11 ); tTester.PushBack( 11 ); tTester.PushBack( 11 ); tTester.PushBack( 11 ); tTester.PushBack( 11 ); tTester.PushBack( 11 ); tTester.PushBack( 111 ); tTester.PushBack( 11 ); cout << tTester.Capacity() << " == 30" << endl; // 30

tTester.Reserve( 50 ); cout << tTester.Capacity() << " == 50" << endl; // 50

tTester.PopBack(); cout << tTester.At( tTester.Size() - 1 ) << " == 111" << endl;// 111

tTester.Clear(); cout << tTester.Size() << " == 0" << endl;// 0

tTester.PushBack( 0 ); tTester.PushBack( 1 ); tTester.PushBack( 2 ); tTester.PushBack( 3 ); tTester.PushBack( 4 ); tTester.PushBack( 5 );

VectortCopy( tTester ); cout << tTester.Size() << " == 6" << endl;// 6 cout << tCopy.Size() << " == 6" << endl;// 6

tCopy.PopBack(); cout << tTester.Size() << " == 6" << endl;// 6 cout << tCopy.Size() << " == 5" << endl;// 5

Vector tAssign; tAssign.PushBack( 999 ); tAssign = tTester; // Tricky thing, if you do an initialization (Vector A = B;) even though it has a = it runs the copy constructor instead.)

cout << tTester.Size() << " == 6" << endl;// 6 cout << tAssign.Size() << " == 6" << endl;// 6

tAssign.PopBack(); cout << tTester.Size() << " == 6" << endl;// 6 cout << tAssign.Size() << " == 5" << endl;// 5

// The destructor will be called on each Vector when they go out of scope. If you want to see it explicitly, // You need to new and delete a vector object. }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!