T
Tony Johansson
Hello experts!
I have two class template below with names Array and CheckedArray.
The class template CheckedArray is derived from the class template Array
which is the base class
This program works fine but there in one thing that I'm unsure about
and that is the inheritance statement.
What difference is it if I have this construction
class CheckedArray : public Array<T>
compared to this construction. .
class CheckedArray : public Array
Note the type parameter <T> has been removed
I don't get any compile error or run time error if I use the
last statement that with the missing <T> for the base class
See definition for the class template CheckedArray
about this class CheckedArray : public Array<T>
From here to the end is code for the two class templates Array and
CheckedArray
**************************************
Here start the definition of the class template Array
***************************************
template <typename T>
class Array
{
public:
Array(int s=100) : size(s)
{ array = new T[size]; }
Array(const Array<T>& ar) : size(ar.size)
{
array = new T[ar.size];
for(int i=0; i<size; i++)
array = ar.array;
}
bool operator==(const Array<T>& ar)
{
for (int i=0; i<size; i++)
if (array != ar.array)
return false;
return true;
}
Array<T>& operator=(const Array<T>& ar)
{
if (this == &ar)
return *this;
if (size != ar.size)
{
size = ar.size;
delete[] array;
array = new T[size];
for(int i=0; i<size; i++)
array = ar.array;
return *this;
}
}
virtual ~Array()
{ delete[] array; }
virtual const T& operator[](int i) const
{ return array; }
virtual T& operator[](int i)
{ return array; }
int getSize() const
{ return size; }
typedef T element_type;
protected:
int size;
T* array;
};
*******************************************************************
Here start the definition of the class template CheckedArray
********************************************************************
template <typename T>
//class CheckedArray : public Array // Alternative 1
class CheckedArray : public Array<T> // Alternative 2
{
public:
CheckedArray(int = 100);
CheckedArray(const CheckedArray<T>&);
CheckedArray<T>& operator=(const CheckedArray<T>&);
virtual ~CheckedArray();
virtual const T& operator[](int) const;
virtual T& operator[](int);
bool operator==(const CheckedArray<T>&);
};
template <typename T>
bool operator==(const CheckedArray<T>&, const CheckedArray<T>&);
template <typename T>
bool operator!=(const CheckedArray<T>&, const CheckedArray<T>&);
template <typename T>
CheckedArray<T>::CheckedArray(int size) : Array<T>(size) {}
template <typename T>
CheckedArray<T>::CheckedArray(const CheckedArray<T>& ar) : Array<T>(ar) {}
template <typename T>
CheckedArray<T>& CheckedArray<T>:perator=(const CheckedArray<T>& ar)
{
if (this == &ar)
return *this;
if (size != ar.size() ) cout << "Error" << endl;
Array<T>:perator=(ar);
return *this;
}
template <typename T>
bool CheckedArray<T>:perator==(const CheckedArray<T>& ar)
{
if (getSize() != ar.getSize() ) cout << "error" << endl;
return Array<T>:perator==(ar);
}
template <typename T>
CheckedArray<T>::~CheckedArray() {}
template <typename T>
T& CheckedArray<T>:perator[](int i)
{
if (i < 0 || i >= size) cout << "error" << endl;
return array;
}
template <typename T>
const T& CheckedArray<T>:perator[](int i) const
{
if (i < 0 || i >= size) cout << "error" << endl;
return array;
}
template <typename T>
bool operator!=(const CheckedArray<T>& ar1, const CheckedArray<T>& ar2)
{ return !(ar1 == ar2); }
Many thanks
//Tony
I have two class template below with names Array and CheckedArray.
The class template CheckedArray is derived from the class template Array
which is the base class
This program works fine but there in one thing that I'm unsure about
and that is the inheritance statement.
What difference is it if I have this construction
class CheckedArray : public Array<T>
compared to this construction. .
class CheckedArray : public Array
Note the type parameter <T> has been removed
I don't get any compile error or run time error if I use the
last statement that with the missing <T> for the base class
See definition for the class template CheckedArray
about this class CheckedArray : public Array<T>
From here to the end is code for the two class templates Array and
CheckedArray
**************************************
Here start the definition of the class template Array
***************************************
template <typename T>
class Array
{
public:
Array(int s=100) : size(s)
{ array = new T[size]; }
Array(const Array<T>& ar) : size(ar.size)
{
array = new T[ar.size];
for(int i=0; i<size; i++)
array = ar.array;
}
bool operator==(const Array<T>& ar)
{
for (int i=0; i<size; i++)
if (array != ar.array)
return false;
return true;
}
Array<T>& operator=(const Array<T>& ar)
{
if (this == &ar)
return *this;
if (size != ar.size)
{
size = ar.size;
delete[] array;
array = new T[size];
for(int i=0; i<size; i++)
array = ar.array;
return *this;
}
}
virtual ~Array()
{ delete[] array; }
virtual const T& operator[](int i) const
{ return array; }
virtual T& operator[](int i)
{ return array; }
int getSize() const
{ return size; }
typedef T element_type;
protected:
int size;
T* array;
};
*******************************************************************
Here start the definition of the class template CheckedArray
********************************************************************
template <typename T>
//class CheckedArray : public Array // Alternative 1
class CheckedArray : public Array<T> // Alternative 2
{
public:
CheckedArray(int = 100);
CheckedArray(const CheckedArray<T>&);
CheckedArray<T>& operator=(const CheckedArray<T>&);
virtual ~CheckedArray();
virtual const T& operator[](int) const;
virtual T& operator[](int);
bool operator==(const CheckedArray<T>&);
};
template <typename T>
bool operator==(const CheckedArray<T>&, const CheckedArray<T>&);
template <typename T>
bool operator!=(const CheckedArray<T>&, const CheckedArray<T>&);
template <typename T>
CheckedArray<T>::CheckedArray(int size) : Array<T>(size) {}
template <typename T>
CheckedArray<T>::CheckedArray(const CheckedArray<T>& ar) : Array<T>(ar) {}
template <typename T>
CheckedArray<T>& CheckedArray<T>:perator=(const CheckedArray<T>& ar)
{
if (this == &ar)
return *this;
if (size != ar.size() ) cout << "Error" << endl;
Array<T>:perator=(ar);
return *this;
}
template <typename T>
bool CheckedArray<T>:perator==(const CheckedArray<T>& ar)
{
if (getSize() != ar.getSize() ) cout << "error" << endl;
return Array<T>:perator==(ar);
}
template <typename T>
CheckedArray<T>::~CheckedArray() {}
template <typename T>
T& CheckedArray<T>:perator[](int i)
{
if (i < 0 || i >= size) cout << "error" << endl;
return array;
}
template <typename T>
const T& CheckedArray<T>:perator[](int i) const
{
if (i < 0 || i >= size) cout << "error" << endl;
return array;
}
template <typename T>
bool operator!=(const CheckedArray<T>& ar1, const CheckedArray<T>& ar2)
{ return !(ar1 == ar2); }
Many thanks
//Tony