Ahh
I had tried that earlier, and when I received an error I assumed it was because of the syntax.
But since you just told me it is, indeed, the correct syntax, I looked elsewhere for the error, and found it in my .cpp file. Thanks for the help!
I also have another question, regarding copy constructors.
From what I read online, the purpose of the copy constructors is to allow you to duplicate all of the pointers and objects of a class from one instance to another instance, since if you don't do that you will have pointers of each object basically pointing to the same thing.
I guess I understand that, but I really don't see where I would use the constructor in my code. Althouh I created a copy constructor in my code, I never used it once in the program and everything works perfectly.
I'm trying to learn how to program in the most efficient way, however, and so I'm trying to learn how to utilize the copy constructor effectively.
So my question is this: where in my code would a copy constructor be beneficial? As of now, I see no reason to ever use it when I can just use the overloaded "=" operator.
This is my new code (I revised my copy constructor from my previous post after doing some online research):
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#ifndef Array_h
#define Array_h
#include<iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
template <typename T>
class Array
{
public:
//Array(T const& value = T());
friend ostream & operator << (ostream& os, const Array<T> & out)
{
for (int i = 0; i < out.size; i++)
{
os << out.arr[i];
}
return (os);
}
friend istream & operator >> (istream & is, Array<T> & in)
{
int count = 0;
for (int i = 0; i < in.size; i--)
{
cin >> in.arr[i];
}
return (is);
}
Array(int s = DEFAULTSIZE); // default constructor
Array(const Array &); // copy constructor
~Array(); // destructor
int getsize() const; // return size
// assignment operator
const Array & operator= (const Array &);
//equality operator
bool operator == (const Array &) const;
// inequality operator; returns opposite of == operator
bool operator != (const Array &) const
{
return ! (*this == right); //invokes Array::operator==
} // end function operator !-
static int getDELIM()
{
return(DELIM);
}
int getsize()
{
return(size);
}
T & operator[] (int);
T operator [] (int) const;
private:
static const int DELIM = -999; // example of using a delimiter to signal end of input
static const int DEFAULTSIZE = 10; // default array size
int size;
T *arr;
};
#endif
template<class T> Array<T>::Array(int s)
{
size = s;
arr = new T [size];
if(!arr)
{
delete [] arr;
cerr << "error\n";
exit(EXIT_FAILURE);
}
}
template<class T> Array<T>::Array(const Array ©)
{
arr = new T (*arr.copy);
}
template<class T> Array<T>::~Array()
{
delete [] arr;
arr = NULL;
}
template<class T> T & Array<T>::operator[] (int subscript)
{
if (subscript < 0 || subscript >= size)
{
cerr << "Error";
exit(EXIT_FAILURE);
}
return(arr[subscript]);
}
template<class T> T Array<T>::operator[] (int subscript) const
{
if (subscript < 0 || subscript >= size)
{
cerr << "Error";
exit(EXIT_FAILURE);
}
return (arr[subscript]);
}
template<class T> bool Array<T>::operator == (const Array<T> & op2) const
{
bool arrayEqual = true;
if(size != op2.size)
arrayEqual = false;
else
{
for (int i = 0; i < op2.size; i++)
{
if(arr[i] != op2.arr[i])
arrayEqual = false;
if(!arrayEqual) // If arrayEqual is false once, it is no longer necessary to check and continue the loop
i = op2.size;
}
}
return (arrayEqual);
}
template<class T> const Array<T> & Array<T>::operator = (const Array<T> & op2)
{
if (size >= op2.size)
{
size = op2.size;
}
else
{
delete [] arr;
size = op2.size;
arr = new T [size];
}
for (int i = 0; i <= op2.size; i++)
{
arr[i] = op2.arr[i];
}
}
|