Hello! We have this assignment for my OOP paper where we need to create a darray.cpp file that defines a class declared in a header file (darray.h) and should be tested in another c++ file called test.cpp
Here are the files provided:
darray.h (we were told not to change it)
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
|
#ifndef DArray_H
#define DArray_H
#include <iostream>
using namespace std;
typedef double ItemType;
void info();/* display on monitor details of the
authors of code in cpp file */
class DArray{
//IO operator overloading:
friend ostream &operator<<( ostream &, const DArray & );
friend istream &operator>>( istream &, DArray & );
public:
DArray( int = 10 ); // default constructor
DArray( const DArray & ); // copy constructor
~DArray(); // destructor
int getSize() const; // return size
const DArray &operator=( const DArray & ); // assignment operator
bool operator==( const DArray & ) const; // equality operator
// not-equal operator:
bool operator!=( const DArray &right ) const {
return ! ( *this == right );
} // end function operator!=
// subscript operator for non-const objects
ItemType &operator[]( int );
// subscript operator for const objects
ItemType operator[]( int ) const;
private:
int size; // number of elements in the array object
ItemType *ptr; /* pointer to first element
of pointer-based DArray */
}; // end class DArray
#endif
|
test.cpp (also shouldn't be changed)
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
|
// DArray class test program.
#include <iostream>
#include <cstdlib>
#include "darray.h"
using namespace std;
int main(){
info();
DArray reals1( 15 ); // fifteen-element DArray
for (int i=0;i<15;++i) reals1[i]=i*4.5;
cout << "Output operator tested.\n";
cout << reals1; //Output operator tested
//print reals1 size and contents
cout << "Size of DArray reals1 is "
<< reals1.getSize() <<endl;
const DArray reals2(reals1);
// print reals2 size and contents
cout << "\nSize of DArray reals2 is " << reals2.getSize()
<< "\nDArray after initialisation:\n" << reals2;
//use overloaded inequality (!=) operator
cout << "reals1 and reals2 are "
<< (reals1 != reals2? " not " : " ") << "equal.\n";
//use overloaded subscript operator
cout << "\n\nAssigning 345.78 to reals1[5]" << endl;
reals1[5] = 345.78;
cout << "reals1: n" << reals1;
//attempt to use out-of-range subscript
//cout << "\nAttempt to assign 1000.1 to reals1[15]\n";
//reals1[15] = 1000.1; //ERROR: out of range
//attempt to change const DArray object
//reals2[5] = 345.78; //syntax error: left value is constant
cout <<"*****All done. Bye!*****" << endl;
return EXIT_SUCCESS;
}
|
And when you run the test.cpp file, this should be what displays:
http://i.imgur.com/iSMAnAW.png
The info() function is just called to print out the heading so it isn't really important but I'm really confused about what to do for the rest of the program.
The requirements are:
- The class should define an array of double values performing range checking to ensure that subscripts remain within the bounds of the array.
(Where and how do I do this?)
- The class will allow one DArray object to be assigned to another using the assignment operator.
- Objects of the DArray class will know their size.
- Entire DArray objects can be input / output with the stream extraction / insertion operators, respectively.
- Comparison between DArray objects using operators == and != will be possible.
If anyone could help me on this, or can explain how this works, I'll be very thankful.