We all low that the regular array has wealnesses, such as the size has to be kno
ID: 3883282 • Letter: W
Question
Explanation / Answer
Below is your program. Let me know if you have any issue in this
#include<iostream>
using namespace std;
const int dSize = 10;
template<class T>
class SA{
private:
// pointer of type T
T* pT;
// low index
int lIdx;
// high index
int hIdx;
public:
SA()
{
// initiaize array of size 10
pT = new T[dSize];
lIdx = 0;
hIdx = 9;
}
// parametarized constructor
SA(int n)
{
// initiaize array of size n
pT = new T[n];
lIdx = 0;
hIdx = n - 1;
}
// parametarized constructor
SA(int l, int h)
{
// initiaize array of size n
pT = new T[h - l + 1];
lIdx = l;
hIdx = h;
}
// copy constructor
SA(const SA& arr)
{
int i;
// initilize array of appropriate size
this->pT = new int[arr.hIdx - arr.lIdx + 1];
this->lIdx = arr.lIdx;
this->hIdx = arr.hIdx;
// assign the array values of arr to the current object array
for (i = arr.lIdx; i <= arr.hIdx; i++)
this->pT = arr.pT[i];
}
// overloading operator =
void operator=(const SA& rhs)
{
int i;
// initilize array of appropriate size
this->pT = new int[rhs.hIdx - rhs.lIdx + 1];
this->lIdx = rhs.lIdx;
this->hIdx = rhs.hIdx;
// assign the array values of rhs to the current object array
for (i = rhs.lIdx; i <= rhs.hIdx; i++)
this->pT = rhs.pT[i];
}
~SA()
{
delete[] pT;
}
// overload [] operator
T& operator[](int offset)
{
// if index is valid
if (offset >= lIdx && offset <= hIdx)
return this->pT[offset];
else
{
cout << "Please enter valid index ";
return this->pT[0];
}
}
// method to get size of array pT
int getSize() const
{
// return the size of array
return hIdx - lIdx + 1;
}
friend ostream& operator<<(ostream &output, const SA &ob)
{
int i;
int size = ob.getSize();
for (i = 0; i<size; i++)
output << ob.pT[i] << " ";
output << endl;
return output;
}
};
class cat {
private:
string catName;
public:
cat(string name) {
catName = name;
}
cat() {
catName = "";
}
friend ostream& operator<<(ostream &output, const cat &ob)
{
cout<<ob.catName<<" says meow"<<endl;
return output;
}
};
int main()
{
int i;
// object with values of type double
SA<int> intArr(5);
for (i = 0; i<5; i++)
// initialize array of intArr object
intArr[i] = i;
cout << "Array contents are ... ";
// print content of intArr
cout << intArr;
// print size of array
cout << " Size = " << intArr.getSize() << endl << endl;
// object with values of type double
SA<double> dArr(10);
for (i = 0; i<10; i++)
// initialize array of dArr object
dArr[i] = i * 1.1;
cout << "Array contents are ... ";
// print content of dArr
cout << dArr;
// print size of array
cout << " Size = " << dArr.getSize();
SA<cat> catArr(3);
catArr[0] = cat("catA");
catArr[1] = cat("catB");
catArr[2] = cat("catC");
cout << " Array contents are ... ";
// print content of dArr
cout << catArr;
// print size of array
cout << " Size = " << catArr.getSize();
return 0;
}
Sample Output
Array contents are ...
0 1 2 3 4
Size = 5
Array contents are ...
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
Size = 10
Array contents are ...
catA says meow
catB says meow
catC says meow
Size = 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.