Question
C++ Answers only Description: In this project you will build on your code from Assignment 4. You will need to overload the following operators to
C++ Answers only
Description:
In this project you will build on your code from Assignment 4. You will need to overload the following operators to perform the specified functions within your objects
operator=
Assignment operator.
Should be cascadable.
operator==
Equality operator.
operator!=
Inequality operator.
operator+
Addition operator.
In the case of the myArray object, addition is defined to be a concatenation of two myArray objects and their contents.
Should be cascadable.
operator-
Negation operator.
operator++(int)
Post-increment operator.
operator++
Pre-increment operator.
operator[]
Square bracket operator for extraction of an element (ie. double element = obj[0]).
operator[]
Square bracket operator for insertion of an element (ie. obj[0] = 10.5).
operator>>
Stream extraction operator
Allows printing of the object via: cout obj;
Should be cascadable
operator
Stream insertion operator
Allows insertion of elements into the arr member variable via: cin >> obj;
Should be cascadable
What I have for a .h file right now
class myArray { friend ostream & operatorostream &lhs, const myArray &rhs); friend istream & operator>>(istream &lhs, myArray &rhs); public: myArray(); myArray(int _size); myArray(int _size, double value); myArray(const myArray& orig); ~myArray();
void print() const; void expand();
int getSize() const; double getData(int index) const; double operator[](int index) const;
void setData(int index, double value); bool equal(const myArray& rhs) const; bool operator==(const myArray& rhs) const; bool operator!=(const myArray& rhs) const; void operator=(const myArray& rhs); const myArray operator++(); //prefix ++myArray const myArray operator++(int dummy); //postfix myArray++ const myArray operator+(const myArray & rhs) const; const myArray operator-(); const myArray operator[](); const myArray & operator[](); void printMyInfo() const;
private: double *data; int size; void setSize(int value); };
My .cpp file
#include \"myArray.h\"
myArray::myArray() : size(10) { // size = 10; data = new double [size]; for (int i = 0; i i++) { data[i] = i; } }
myArray::myArray(int _size) : size(_size) { size = _size; data = new double [size]; for (int i = 0; i i++) { data[i] = i; } }
myArray::myArray(int _size, double value) : size(_size) { // size = _size; data = new double [size]; for (int i = 0; i i++) { data[i] = value; } }
myArray::myArray(const myArray& orig) { setSize(orig.getSize()); data = new double [getSize()]; for (int i = 0; i getSize(); i++) { setData(i, orig.getData(i)); } }
myArray::~myArray() { delete [] data; }
int myArray::getSize() const { return size; }
void myArray::setSize(int value) { if (value > 0) { size = value; } }
void myArray::setData(int index, double value) { if ((index >= 0) && (index data[index] = value; } else { cout endl; } }
double myArray::getData(int index) const { if ((index >= 0) && (index return data[index]; } else { return data[size - 1]; } }
double myArray::operator[](int index) const { if ((index >= 0) && (index return data[index]; } else { return data[size - 1]; } }
void myArray::print() const { for (int i = 0; i i++) { cout i] } cout endl; }
void myArray::expand() { double *localArray = new double[size + 1];
for (int i = 0; i i++) { localArray[i] = data[i]; } localArray[size] = size;
delete [] data; setSize(size + 1); data = localArray; // myArray = new int[size]; // // //Is this a deep-copy or a shallow-copy? // //Can you replace one with the other? // //What are the advantages and disadvantages? // for(int i=0; i i++) { // myArray[i] = localArray[i]; // } // delete [] localArray; }
bool myArray::equal(const myArray& rhs) const { bool result(true);
if (getSize() != rhs.getSize()) { result = false; } else { for (int i = 0; i getSize(); i++) { if (getData(i) != rhs.getData(i)) { result = false; } } } return result; }
bool myArray::operator==(const myArray& rhs) const { bool result(true);
if (getSize() != rhs.getSize()) { result = false; } else { for (int i = 0; i getSize(); i++) { if (getData(i) != rhs.getData(i)) { result = false; } } } return result; }
bool myArray::operator!=(const myArray& rhs) const { bool result(false);
if (getSize() != rhs.getSize()) { result = true; } else {
for (int i = 0; i getSize(); i++) { if (data[i] != rhs[i]) { result = true; } } } return result; // return !(*this == rhs); }
void myArray::operator=(const myArray& rhs) { delete [] data; setSize(rhs.getSize()); data = new double [getSize()]; for (int i = 0; i i++) { data[i] = rhs[i]; } }
const myArray myArray::operator+(const myArray & rhs) const { myArray Arr(rhs); if (getSize() != rhs.getSize()) { cout endl; } else { for (int i = 0; i getSize(); i++) { Arr.setData(i, rhs[i] + data[i]); } } return Arr; }
const myArray myArray::operator++() { for(int i=0; i getSize(); i++) { data[i]++; } return *this; }
const myArray myArray::operator++(int dummy) { myArray temp(*this); for(int i=0; i getSize(); i++) { data[i]++; } return temp; }
void myArray::printMyInfo() const { cout endl; cout endl; cout endl; cout endl; cout endl; }
ostream & operatorostream &lhs, const myArray &rhs) { for (int i = 0; i rhs.getSize(); i++) { lhs rhs[i] } cout endl; return lhs; }
istream & operator>>(istream &lhs, myArray &rhs) { double temp; for (int i = 0; i rhs.getSize(); i++) { lhs >> temp; rhs.setData(i, temp); } return lhs; }
My main.cpp
#include cstdlib> #include \"myArray.h\"
using namespace std;
/* * */ int main(int argc, char** argv) {
myArray Array1; myArray Array2(10); myArray Array3(Array2); myArray Array4(10, -10);
cout printInfo for Array1\" endl; cout endl; // Array1.printMyInfo(); cout endl; cin >> Array1; cout endl; cout endl; cout endl; cout endl; cout endl; cout endl; cout endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started