Programming Instruction: Make a collection of classes in c++ that handle arrays
ID: 3818660 • Letter: P
Question
Programming Instruction: Make a collection of classes in c++ that handle arrays of different types of numbers. You should have an Array base class that keeps track of the size of the array, plus DoubleArray and IntArray subclasses that implement the data storage (an array) of the appropriate type. The Array class should have a sort() method that sorts the array in place. The classes should have a way of accessing (getting and setting) the array elements that throws an exception if the index is out of bounds. These can be get/set methods. The constructors should specify the size of the array.My thoughts: I think this program needs to implement a template in the Array superclass. You will then use the template Array superclass to make arrays with the subclass DoubleArray and IntArrary. Programming Instruction: Make a collection of classes in c++ that handle arrays of different types of numbers. You should have an Array base class that keeps track of the size of the array, plus DoubleArray and IntArray subclasses that implement the data storage (an array) of the appropriate type. The Array class should have a sort() method that sorts the array in place. The classes should have a way of accessing (getting and setting) the array elements that throws an exception if the index is out of bounds. These can be get/set methods. The constructors should specify the size of the array.
My thoughts: I think this program needs to implement a template in the Array superclass. You will then use the template Array superclass to make arrays with the subclass DoubleArray and IntArrary. Programming Instruction: Make a collection of classes in c++ that handle arrays of different types of numbers. You should have an Array base class that keeps track of the size of the array, plus DoubleArray and IntArray subclasses that implement the data storage (an array) of the appropriate type. The Array class should have a sort() method that sorts the array in place. The classes should have a way of accessing (getting and setting) the array elements that throws an exception if the index is out of bounds. These can be get/set methods. The constructors should specify the size of the array.
My thoughts: I think this program needs to implement a template in the Array superclass. You will then use the template Array superclass to make arrays with the subclass DoubleArray and IntArrary.
Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
class Array
{
public:
int size;
Array(int initialSize)
{
size = initialSize;
}
template <typename T>
static void sort(T data[], int size)
{
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
if(data[i] < data[j])
{
T temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
}
};
class IntArray: public Array
{
public:
int *data;
IntArray(int i_size) : Array(i_size)
{
data = new int[size];
}
void set(int index, int value)
{
if(index<0 || index>=size)
{
cout<<"Invalid index";
}
else
data[index] = value;
}
int get(int index)
{
if(index<0 || index>=size)
{
cout<<"Invalid index";
}else
return data[index];
}
void sort()
{
Array::sort(data, size);
}
};
class DoubleArray: public Array
{
public:
double *data;
DoubleArray(int i_size): Array(i_size)
{
data = new double[size];
}
void set(int index, double value)
{
if(index<0 || index>=size)
cout<<"Invalid index";
else
data[index] = value;
}
double get(int index)
{
if(index<0 || index>=size)
{
cout<<"Invalid index";
}
else
return data[index];
}
void sort()
{
Array::sort(data, size);
}
};
int main() {
IntArray intarray(5);
intarray.set(0, 60);
intarray.set(1, 30);
intarray.set(2, 40);
intarray.set(3, 50);
intarray.set(4, 20);
intarray.sort();
cout<<intarray.get(0)<<endl;
cout<<intarray.get(1)<<endl;
cout<<intarray.get(2)<<endl;
cout<<intarray.get(3)<<endl;
cout<<intarray.get(4)<<endl;
return 0;
}
OUTPUT:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.