Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help Error Checking C++ Answers Only In file included from main.cpp:9:0: myArray

ID: 3595449 • Letter: H

Question

Help Error Checking C++ Answers Only

In file included from main.cpp:9:0:
myArray.h:41:22: error: ‘const myArray& myArray::operator[](int)’ cannot be overloaded
const myArray & operator[](int index);
^
myArray.h:40:19: error: with ‘const myArray myArray::operator[](int)’
const myArray operator[](int index);
^
myArray.h:41:22: error: ‘const myArray& myArray::operator[](int)’ cannot be overloaded
const myArray & operator[](int index);
^
myArray.h:40:19: error: with ‘const myArray myArray::operator[](int)’
const myArray operator[](int index);

CODE

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 << "Test of printInfo for Array1" << endl;
cout << &Array1 << endl;
// Array1.printMyInfo();
  
cout << Array1 << endl;
cin >> Array1;
cout << Array1 << endl;
cout << "PREFIX" << endl;
cout << ++Array1 << endl;
cout << Array1 << endl;
cout << "POSTFIX" << endl;
cout << Array1++ << endl;
cout << Array1 << endl;
cout << "NEGATION" <<endl;
cout << -Array1 <<endl;

return 0;
}

myArray.H

#ifndef ARRAY_H
#define ARRAY_H

#include <iostream>
using namespace std;

class myArray {
friend ostream & operator<<(ostream &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[](int index);
const myArray & operator[](int index);
void printMyInfo() const;
  


private:
double *data;
int size;
void setSize(int value);
};

#endif /* ARRAY_H */

myArray.cpp

#include "myArray.h"

myArray::myArray() : size(10) {
// size = 10;
data = new double [size];
for (int i = 0; i < size; i++) {
data[i] = i;
}
}

myArray::myArray(int _size) : size(_size) {
size = _size;
data = new double [size];
for (int i = 0; i < size; i++) {
data[i] = i;
}
}

myArray::myArray(int _size, double value) : size(_size) {
// size = _size;
data = new double [size];
for (int i = 0; i < size; 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 < size)) {
data[index] = value;
} else {
cout << "NO!" << endl;
}
}

double myArray::getData(int index) const {
if ((index >= 0) && (index < size)) {
return data[index];
} else {
return data[size - 1];
}
}

double myArray::operator[](int index) const {
if ((index >= 0) && (index < size)) {
return data[index];
} else {
return data[size - 1];
}
}

void myArray::print() const {
for (int i = 0; i < size; i++) {
cout << data[i] << " ";
}
cout << endl;
}

void myArray::expand() {
double *localArray = new double[size + 1];

for (int i = 0; i < size; 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 < size; 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 < size; i++) {
data[i] = rhs[i];
}
}

const myArray myArray::operator+(const myArray & rhs) const {
myArray Arr(rhs);
if (getSize() != rhs.getSize()) {
cout << "Exception" << 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;
}

const myArray myArray::operator-(){
myArray temp(*this);

for(int i = 0;i < getSize(); i++) {
data[i] *-1;

}
return temp;
}

void myArray::printMyInfo() const {
cout << "I am at: " << this << endl;
cout << "My data is at: " << &data << endl;
cout << "My size is at: " << &size << " with a value of : " << size << endl;
cout << "My data points to: " << data << endl;
cout << "The first item in data is: " << *data << endl;
}

ostream & operator<<(ostream &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;
}

Explanation / Answer

myArray.h

#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
using namespace std;
class myArray {
friend ostream & operator<<(ostream &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()(int index);
const myArray &operator[](int index);
void printMyInfo() const;
  

private:
double *data;
int size;
void setSize(int value);
};