Question

please help with the operator overloading lab (intArray) in c++ will provide what it is being required and the code that was given from the book.

Operator Overloading Lab (IntArray) The array construct in C is very efficient but also very dangerous for the an array of th index 100. Not only is this r example, many novice programmers fall into the trap of declari elements and then try to access an error in C, but the language wont even alert the user when the mistake made. C++ allows programmers to define safer and more flexible array constructs if they are w efficiency. The lling to sacrifice some of Cs runtime effi purpose of this lab is to see how this is done and to gain some experience in overloading operators. The heart of this assignment is a class that you will define called IntArra With it, the user will be able to declare integer arrays of any size with automafic range checking of indices. The upper and lower indices can be any integer positive or negative, rather than the fixed limits of o to SIZE-1. It will also be possible to assign entire arrays to each other, compare two arrays for and inequality, add two arrays, and output arrays using the overloaded< operator. For example: include <iostream> include IntArray.h using namespace std: int main) f IntArray a(10), w(10): IntArray b(-3, 6) IntArray c(6, 8) IntArray d (5, 5); IntArray z; // Ten elements, indexed 0 to 9 // Ten elements, indexed -3 to 6 // Three elements, indexed 6 to8 // Single element array, indexed at 5 // Ten elements, indexed 0 to 9 // hight) and low) return largest and smallest legal indices for (int í a. low ( ); î < a. high(); ++1) ali]-i10; // Access just like normal array /7 Output array contents. Note that you will overload // the << operator to do this cout << a << endl; // The code for the overloaded << operator will look // something like this: for (int i. a.low ( ) ; î <-a.high ( ); ?++) cout << al <i<- << alil < Focus on Object-Oriented Programming with C++ Page 270

media%2Fa9a%2Fa9afcb05-ce16-4154-889c-84

media%2F0ec%2F0ecd7a46-7af3-4f69-880e-82

media%2Fbb5%2Fbb59816b-cdce-470c-973b-a2

null

media%2F06d%2F06dd3a32-27fe-46e3-af9d-2f

media%2Fbd5%2Fbd5afaf1-7d0c-4516-a585-9a

the code that was provided is below

-------------------------------------------------------------------------------------------------------------------------
// iadrv.h
#ifndef _IADRV_H
#define _IADRV_H
#include "intarray.h"
int main();
void test1();
void test2();
void test3();
void test4();
void test5();
void test6();
void test7();
void test8();
void test9();
void test10();
void test11();
void test12();
void test13();
void test14();
void test15();
void test16();
void test17();
void test18();
void test19();
void test20();
void wait();
#endif
-------------------------------------------------------------------------------------------------------------------------
// iadrv.cpp - driver program for testing IntArray class
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include "iadrv.h"
using namespace std;
ofstream csis;
int main() {
csis.open("csis.txt");
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
test10();
test11();
test12();
test13();
test14();
test15();
test16();
test17();
test18();
test19();
test20();
csis.close();
}
void test1() {
cout << "1. Array declared with single integer: IntArray a(10);" << endl << endl;
csis << "1. Array declared with single integer: IntArray a(10);" << endl << endl;
IntArray a(10);
for(int i = a.low(); i <= a.high(); i++)
a[i] = i * 10;
a.setName("a");
cout << a << endl;
csis << a << endl;
wait();
}
void test2() {
cout << "2. Array declared with two integers: IntArray b(-3, 6);" << endl << endl;
csis << "2. Array declared with two integers: IntArray b(-3, 6);" << endl << endl;
IntArray b(-3, 6);
for(int i = b.low(); i <= b.high(); i++)
b[i] = i * 10;
b.setName("b");
cout << b << endl;
csis << b << endl;
wait();
}
void test3() {
cout << "3. Array declared with two integers: IntArray c(6, 8);" << endl << endl;
csis << "3. Array declared with two integers: IntArray c(6, 8);" << endl << endl;
IntArray c(6, 8);
for(int i = c.low(); i <= c.high(); i++)
c[i] = i * 10;
c.setName("c");
cout << c << endl;
csis << c << endl;
wait();
}
void test4() {
cout << "4. Array declared with two identical integers: IntArray d(5, 5);" << endl << endl;
csis << "4. Array declared with two identical integers: IntArray d(5, 5);" << endl << endl;
IntArray d(5, 5);
for(int i = d.low(); i <= d.high(); i++)
d[i] = i * 10;
d.setName("d");
cout << d << endl;
csis << d << endl;
wait();
}
void test5() {
cout << "5. Array declared with no integers: IntArray z;" << endl << endl;
csis << "5. Array declared with no integers: IntArray z;" << endl << endl;
IntArray z;
for(int i = z.low(); i <= z.high(); i++)
z[i] = i * 10;
z.setName("z");
cout << z << endl;
csis << z << endl;
wait();
}
void test6() {
cout << "6. Array declared with another object of type IntArray: IntArray c(6, 8);" << endl;
cout << " Intarray e(c);" << endl << endl;
csis << "6. Array declared with another object of type IntArray: IntArray c(6, 8);" << endl;
csis << " Intarray e(c);" << endl << endl;
IntArray c(6, 8);
for(int i = c.low(); i <= c.high(); i++)
c[i] = i * 10;
c.setName("c");
cout << c << endl;
csis << c << endl;
IntArray e(c);
e.setName("e");
cout << e << endl;
csis << e << endl;
wait();
}
void test7() {
cout << "7. Array assigned to another array w/ different indices: IntArray f(1, 4);" << endl;
cout << " IntArray g(5, 8);" << endl;
cout << " f = g;" << endl << endl;
csis << "7. Array assigned to another array w/ different indices: IntArray f(1, 4);" << endl;
csis << " IntArray g(5, 8);" << endl;
csis << " f = g;" << endl << endl;
IntArray f(1, 4);
for(int i = f.low(); i <= f.high(); i++)
f[i] = i * 10;
f.setName("f");
cout << f << endl;
csis << f << endl;
IntArray g(5, 8);
for(int i = g.low(); i <= g.high(); i++)
g[i] = i * 10;
g.setName("g");
cout << g << endl;
csis << g << endl;
wait();
f = g;
cout << f << endl;
cout << g << endl;
csis << f << endl;
csis << g << endl;
wait();
}
void test8() {
cout << "8. Multiple array assignment with different indices: IntArray j(3, 6);" << endl;
cout << " IntArray k(6, 9);" << endl;
cout << " IntArray l(1, 4);" << endl;
cout << " j = k = l;" << endl << endl;
  
csis << "8. Multiple array assignment with different indices: IntArray j(3, 6);" << endl;
csis << " IntArray k(6, 9);" << endl;
csis << " IntArray l(1, 4);" << endl;
csis << " j = k = l;" << endl << endl;
IntArray j(3, 6);
for(int i = j.low(); i <= j.high(); i++)
j[i] = i * 10;
j.setName("j");
cout << j << endl;
csis << j << endl;
IntArray k(6, 9);
for(int i = k.low(); i <= k.high(); i++)
k[i] = i * 10;
k.setName("k");
cout << k << endl;
csis << k << endl;
IntArray l(1, 4);
for(int i = l.low(); i <= l.high(); i++)
l[i] = i * 10;
l.setName("l");
cout << l << endl;
csis << l << endl;
wait();
j = k = l;
cout << j << endl;
cout << k << endl;
cout << l << endl;
csis << j << endl;
csis << k << endl;
csis << l << endl;
wait();
}
void test9() {
cout << "9. Overloaded equality operator (identical elements): IntArray m(3, 7);" << endl;
cout << " IntArray n(1, 5);" << endl;
cout << " m == n" << endl << endl;
csis << "9. Overloaded equality operator (identical elements): IntArray m(3, 7);" << endl;
csis << " IntArray n(1, 5);" << endl;
csis << " m == n" << endl << endl;
IntArray m(3, 7);
for(int i = m.low(); i <= m.high(); i++)
m[i] = i * 10;
m.setName("m");
cout << m << endl;
csis << m << endl;
IntArray n(1, 5);
for(int i = n.low(); i <= n.high(); i++)
n[i] = i * 10;
n.setName("n");
cout << n << endl;
csis << n << endl;
wait();
  
m = n;
cout << m << endl;
cout << n << endl;
cout << "Returns " << (m == n ? "True." : "False.") << endl << endl;
csis << m << endl;
csis << n << endl;
csis << "Returns " << (m == n ? "True." : "False.") << endl << endl;
wait();
}
void test10() {
cout << "10. Overloaded equality operator (different elements): IntArray o(3, 7);" << endl;
cout << " IntArray p(1, 5);" << endl;
cout << " o == p" << endl << endl;
csis << "10. Overloaded equality operator (different elements): IntArray o(3, 7);" << endl;
csis << " IntArray p(1, 5);" << endl;
csis << " o == p" << endl << endl;
IntArray o(3, 7);
for(int i = o.low(); i <= o.high(); i++)
o[i] = i * 10;
o.setName("o");
cout << o << endl;
csis << o << endl;
IntArray p(1, 5);
for(int i = p.low(); i <= p.high(); i++)
p[i] = i * 10;
p.setName("p");
cout << p << endl;
cout << "Returns " << (o == p ? "True." : "False.") << endl << endl;
csis << p << endl;
csis << "Returns " << (o == p ? "True." : "False.") << endl << endl;
wait();
}
void test11() {
cout << "11. Overloaded equality operator (different size arrays): IntArray q(1, 3);" << endl;
cout << " IntArray r(1, 4);" << endl;
cout << " q == r;" << endl << endl;
csis << "11. Overloaded equality operator (different size arrays): IntArray q(1, 3);" << endl;
csis << " IntArray r(1, 4);" << endl;
csis << " q == r;" << endl << endl;
IntArray q(1, 3);
for(int i = q.low(); i <= q.high(); i++)
q[i] = i * 10;
q.setName("q");
cout << q << endl;
csis << q << endl;
IntArray r(1, 4);
for(int i = r.low(); i <= r.high(); i++)
r[i] = i * 10;
r.setName("r");
cout << r << endl;
cout << "Returns " << (q == r ? "True." : "False.") << endl << endl;
csis << r << endl;
csis << "Returns " << (q == r ? "True." : "False.") << endl << endl;
wait();
}
void test12() {
cout << "12. Overloaded inequality operator (identical elements): IntArray s(3, 7);" << endl;
cout << " IntArray t(1, 5);" << endl;
cout << " s != t;" << endl << endl;
csis << "12. Overloaded inequality operator (identical elements): IntArray s(3, 7);" << endl;
csis << " IntArray t(1, 5);" << endl;
csis << " s != t;" << endl << endl;
IntArray s(3, 7);
for(int i = s.low(); i <= s.high(); i++)
s[i] = i * 10;
s.setName("s");
cout << s << endl;
csis << s << endl;
IntArray t(1, 5);
for(int i = t.low(); i <= t.high(); i++)
t[i] = i * 10;
t.setName("t");
cout << t << endl;
csis << t << endl;
wait();
s = t;
cout << s << endl;
cout << t << endl;
cout << "Returns " << (s != t ? "True." : "False.") << endl << endl;
csis << s << endl;
csis << t << endl;
csis << "Returns " << (s != t ? "True." : "False.") << endl << endl;
wait();
}
void test13() {
cout << "13. Overloaded inequality operator (different elements): IntArray u(3, 7);" << endl;
cout << " IntArray v(1, 5);" << endl;
cout << " u != v;" << endl << endl;
csis << "13. Overloaded inequality operator (different elements): IntArray u(3, 7);" << endl;
csis << " IntArray v(1, 5);" << endl;
csis << " u != v;" << endl << endl;
IntArray u(3, 7);
for(int i = u.low(); i <= u.high(); i++)
u[i] = i * 10;
u.setName("u");
cout << u << endl;
csis << u << endl;
IntArray v(1, 5);
for(int i = v.low(); i <= v.high(); i++)
v[i] = i * 10;
v.setName("v");
cout << v << endl;
cout << "Returns " << (u != v ? "True." : "False.") << endl << endl;
csis << v << endl;
csis << "Returns " << (u != v ? "True." : "False.") << endl << endl;
wait();
}
void test14() {
cout << "14. Overloaded inequality operator (different size arrays): IntArray w(1, 3);" << endl;
cout << " IntArray x(1, 4);" << endl;
cout << " w != x;" << endl << endl;
csis << "14. Overloaded inequality operator (different size arrays): IntArray w(1, 3);" << endl;
csis << " IntArray x(1, 4);" << endl;
csis << " w != x;" << endl << endl;
IntArray w(1, 3);
for(int i = w.low(); i <= w.high(); i++)
w[i] = i * 10;
w.setName("w");
cout << w << endl;
csis << w << endl;
IntArray x(1, 4);
for(int i = x.low(); i <= x.high(); i++)
x[i] = i * 10;
x.setName("x");
cout << x << endl;
cout << "Returns " << (w != x ? "True." : "False.") << endl << endl;
csis << x << endl;
csis << "Returns " << (w != x ? "True." : "False.") << endl << endl;
wait();
}
void test15() {
cout << "15. Sum of two arrays assigned to third array: IntArray a(1, 5);" << endl;
cout << " IntArray b(4, 8);" << endl;
cout << " IntArray c = a + b;" << endl << endl;
csis << "15. Sum of two arrays assigned to third array: IntArray a(1, 5);" << endl;
csis << " IntArray b(4, 8);" << endl;
csis << " IntArray c = a + b;" << endl << endl;
IntArray a(1, 5);
for(int i = a.low(); i <= a.high(); i++)
a[i] = i * 10;
a.setName("a");
cout << a << endl;
csis << a << endl;
IntArray b(4, 8);
for(int i = b.low(); i <= b.high(); i++)
b[i] = i * 10;
b.setName("b");
cout << b << endl;
csis << b << endl;
wait();
IntArray c = a + b;
c.setName("c");
cout << c << endl;
csis << c << endl;
wait();
}
void test16() {
cout << "16. Sum of two arrays assigned to first array: IntArray d(10, 13);" << endl;
cout << " IntArray e(30, 33);" << endl;
cout << " d += e;" << endl << endl;
csis << "16. Sum of two arrays assigned to first array: IntArray d(10, 13);" << endl;
csis << " IntArray e(30, 33);" << endl;
csis << " d += e;" << endl << endl;
IntArray d(10, 13);
for(int i = d.low(); i <= d.high(); i++)
d[i] = i * 10;
d.setName("d");
cout << d << endl;
csis << d << endl;
IntArray e(30, 33);
for(int i = e.low(); i <= e.high(); i++)
e[i] = i * 10;
e.setName("e");
cout << e << endl;
csis << e << endl;
d += e;
cout << d << endl;
csis << d << endl;
wait();
}
void test17() {
cout << "17. Array declared with illegal array bounds: IntArray f(5, 2);" << endl << endl;
csis << "17. Array declared with illegal array bounds: IntArray f(5, 2);" << endl << endl;
IntArray f(5, 2);
for(int i = f.low(); i <= f.high(); i++)
f[i] = i * 10;
f.setName("f");
cout << f << endl;
csis << f << endl;
wait();
}
void test18() {
cout << "18. Array with index out of range: IntArray g(10);" << endl;
cout << " g[10] = 1;" << endl << endl;
csis << "18. Array with index out of range: IntArray g(10);" << endl;
csis << " g[10] = 1;" << endl << endl;
IntArray g(10);
for(int i = g.low(); i <= g.high(); i++)
g[i] = i * 10;
g.setName("g");
cout << g << endl;
csis << g << endl;
g[10] = 1;
wait();
}
void test19() {
cout << "19. Arrays with length mismatch: IntArray m(1, 4);" << endl;
cout << " IntArray n(2, 4);" << endl;
cout << " m = n;" << endl << endl;
csis << "19. Arrays with length mismatch: IntArray m(1, 4);" << endl;
csis << " IntArray n(2, 4);" << endl;
csis << " m = n;" << endl << endl;
IntArray m(1, 4);
for(int i = m.low(); i <= m.high(); i++)
m[i] = i * 10;
m.setName("m");
cout << m << endl;
csis << m << endl;
IntArray n(2, 4);
for(int i = n.low(); i <= n.high(); i++)
n[i] = i * 10;
n.setName("n");
cout << n << endl;
csis << n << endl;
wait();
m = n;
cout << m << endl;
cout << n << endl;
csis << m << endl;
csis << n << endl;
wait();
}
void test20() {
cout << "20. Array subscript operator: IntArray o(7, 8);" << endl;
cout << " o[7] = 25;" << endl;
cout << " o[8] = o[7];" << endl << endl;
csis << "20. Array subscript operator: IntArray o(7, 8);" << endl;
csis << " o[7] = 25;" << endl;
csis << " o[8] = o[7];" << endl << endl;
IntArray o(7, 8);
for(int i = o.low(); i <= o.high(); i++)
o[i] = i * 10;
o.setName("o");
cout << o << endl;
csis << o << endl;
o[7] = 25;
o[8] = o[7];
cout << o << endl;
csis << o << endl;
wait();
}
void wait() {
char buf;
  
cout << "Press any key to continue." << endl;
cin.get(buf);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//main.cpp

#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include "iadrv.h"

using namespace std;

ofstream csis;

int main() {
    csis.open("csis.txt");
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    test8();
    test9();
    test10();
    test11();
    test12();
    test13();
    test14();
    test15();
    test16();
    test17();
    test18();
    test19();
    test20();

    csis.close();
}

void test1() {
    cout << "1. Array declared with single integer: IntArray a(10);" << endl << endl;
    csis << "1. Array declared with single integer: IntArray a(10);" << endl << endl;
    IntArray a(10);
    for (int i = a.low(); i <= a.high(); i++)
        a[i] = i * 10;
    a.setName("\na");
    cout << a << endl;
    csis << a << endl;
    wait();
}

void test2() {
    cout << "2. Array declared with two integers: IntArray b(-3, 6);" << endl << endl;
    csis << "2. Array declared with two integers: IntArray b(-3, 6);" << endl << endl;
    IntArray b(-3, 6);
    for (int i = b.low(); i <= b.high(); i++)
        b[i] = i * 10;
    b.setName("\nb");
    cout << b << endl;
    csis << b << endl;
    wait();
}

void test3() {
    cout << "3. Array declared with two integers: IntArray c(6, 8);" << endl << endl;
    csis << "3. Array declared with two integers: IntArray c(6, 8);" << endl << endl;
    IntArray c(6, 8);
    for (int i = c.low(); i <= c.high(); i++)
        c[i] = i * 10;
    c.setName("\nc");
    cout << c << endl;
    csis << c << endl;
    wait();
}

void test4() {
    cout << "4. Array declared with two identical integers: IntArray d(5, 5);" << endl << endl;
    csis << "4. Array declared with two identical integers: IntArray d(5, 5);" << endl << endl;
    IntArray d(5, 5);
    for (int i = d.low(); i <= d.high(); i++)
        d[i] = i * 10;
    d.setName("\nd");
    cout << d << endl;
    csis << d << endl;
    wait();
}

void test5() {
    cout << "5. Array declared with no integers: IntArray z;" << endl << endl;
    csis << "5. Array declared with no integers: IntArray z;" << endl << endl;
    IntArray z;
    for (int i = z.low(); i <= z.high(); i++)
        z[i] = i * 10;
    z.setName("\nz");
    cout << z << endl;
    csis << z << endl;
    wait();
}

void test6() {
    cout << "6. Array declared with another object of type IntArray: IntArray c(6, 8);" << endl;
    cout << "                                                        Intarray e(c);" << endl << endl;
    csis << "6. Array declared with another object of type IntArray: IntArray c(6, 8);" << endl;
    csis << "                                                        Intarray e(c);" << endl << endl;
    IntArray c(6, 8);
    for (int i = c.low(); i <= c.high(); i++)
        c[i] = i * 10;
    c.setName("\nc");
    cout << c << endl;
    csis << c << endl;
    IntArray e(c);
    e.setName("\ne");
    cout << e << endl;
    csis << e << endl;
    wait();
}

void test7() {
    cout << "7. Array assigned to another array w/ different indices: IntArray f(1, 4);" << endl;
    cout << "                                                         IntArray g(5, 8);" << endl;
    cout << "                                                         f = g;" << endl << endl;
    csis << "7. Array assigned to another array w/ different indices: IntArray f(1, 4);" << endl;
    csis << "                                                         IntArray g(5, 8);" << endl;
    csis << "                                                         f = g;" << endl << endl;
    IntArray f(1, 4);
    for (int i = f.low(); i <= f.high(); i++)
        f[i] = i * 10;
    f.setName("\nf");
    cout << f << endl;
    csis << f << endl;
    IntArray g(5, 8);
    for (int i = g.low(); i <= g.high(); i++)
        g[i] = i * 10;
    g.setName("\ng");
    cout << g << endl;
    csis << g << endl;
    wait();
    f = g;
    cout << f << endl;
    cout << g << endl;
    csis << f << endl;
    csis << g << endl;
    wait();
}

void test8() {
    cout << "8. Multiple array assignment with different indices: IntArray j(3, 6);" << endl;
    cout << "                                                     IntArray k(6, 9);" << endl;
    cout << "                                                     IntArray l(1, 4);" << endl;
    cout << "                                                     j = k = l;" << endl << endl;

    csis << "8. Multiple array assignment with different indices: IntArray j(3, 6);" << endl;
    csis << "                                                     IntArray k(6, 9);" << endl;
    csis << "                                                     IntArray l(1, 4);" << endl;
    csis << "                                                     j = k = l;" << endl << endl;
    IntArray j(3, 6);
    for (int i = j.low(); i <= j.high(); i++)
        j[i] = i * 10;
    j.setName("\nj");
    cout << j << endl;
    csis << j << endl;
    IntArray k(6, 9);
    for (int i = k.low(); i <= k.high(); i++)
        k[i] = i * 10;
    k.setName("\nk");
    cout << k << endl;
    csis << k << endl;
    IntArray l(1, 4);
    for (int i = l.low(); i <= l.high(); i++)
        l[i] = i * 10;
    l.setName("\nl");
    cout << l << endl;
    csis << l << endl;
    wait();
    j = k = l;
    cout << j << endl;
    cout << k << endl;
    cout << l << endl;
    csis << j << endl;
    csis << k << endl;
    csis << l << endl;
    wait();
}

void test9() {
    cout << "9. Overloaded equality operator (identical elements): IntArray m(3, 7);" << endl;
    cout << "                                                      IntArray n(1, 5);" << endl;
    cout << "                                                      m == n" << endl << endl;
    csis << "9. Overloaded equality operator (identical elements): IntArray m(3, 7);" << endl;
    csis << "                                                      IntArray n(1, 5);" << endl;
    csis << "                                                      m == n" << endl << endl;
    IntArray m(3, 7);
    for (int i = m.low(); i <= m.high(); i++)
        m[i] = i * 10;
    m.setName("\nm");
    cout << m << endl;
    csis << m << endl;
    IntArray n(1, 5);
    for (int i = n.low(); i <= n.high(); i++)
        n[i] = i * 10;
    n.setName("\nn");
    cout << n << endl;
    csis << n << endl;
    wait();

    m = n;
    cout << m << endl;
    cout << n << endl;
    cout << "Returns " << (m == n ? "True." : "False.") << endl << endl;
    csis << m << endl;
    csis << n << endl;
    csis << "Returns " << (m == n ? "True." : "False.") << endl << endl;
    wait();
}

void test10() {
    cout << "10. Overloaded equality operator (different elements): IntArray o(3, 7);" << endl;
    cout << "                                                       IntArray p(1, 5);" << endl;
    cout << "                                                       o == p" << endl << endl;
    csis << "10. Overloaded equality operator (different elements): IntArray o(3, 7);" << endl;
    csis << "                                                       IntArray p(1, 5);" << endl;
    csis << "                                                       o == p" << endl << endl;
    IntArray o(3, 7);
    for (int i = o.low(); i <= o.high(); i++)
        o[i] = i * 10;
    o.setName("\no");
    cout << o << endl;
    csis << o << endl;
    IntArray p(1, 5);
    for (int i = p.low(); i <= p.high(); i++)
        p[i] = i * 10;
    p.setName("\np");
    cout << p << endl;
    cout << "Returns " << (o == p ? "True." : "False.") << endl << endl;
    csis << p << endl;
    csis << "Returns " << (o == p ? "True." : "False.") << endl << endl;
    wait();
}

void test11() {
    cout << "11. Overloaded equality operator (different size arrays): IntArray q(1, 3);" << endl;
    cout << "                                                          IntArray r(1, 4);" << endl;
    cout << "                                                          q == r;" << endl << endl;
    csis << "11. Overloaded equality operator (different size arrays): IntArray q(1, 3);" << endl;
    csis << "                                                          IntArray r(1, 4);" << endl;
    csis << "                                                          q == r;" << endl << endl;
    IntArray q(1, 3);
    for (int i = q.low(); i <= q.high(); i++)
        q[i] = i * 10;
    q.setName("\nq");
    cout << q << endl;
    csis << q << endl;
    IntArray r(1, 4);
    for (int i = r.low(); i <= r.high(); i++)
        r[i] = i * 10;
    r.setName("\nr");
    cout << r << endl;
    cout << "Returns " << (q == r ? "True." : "False.") << endl << endl;
    csis << r << endl;
    csis << "Returns " << (q == r ? "True." : "False.") << endl << endl;
    wait();
}


void test12() {
    cout << "12. Overloaded inequality operator (identical elements): IntArray s(3, 7);" << endl;
    cout << "                                                         IntArray t(1, 5);" << endl;
    cout << "                                                         s != t;" << endl << endl;
    csis << "12. Overloaded inequality operator (identical elements): IntArray s(3, 7);" << endl;
    csis << "                                                         IntArray t(1, 5);" << endl;
    csis << "                                                         s != t;" << endl << endl;
    IntArray s(3, 7);
    for (int i = s.low(); i <= s.high(); i++)
        s[i] = i * 10;
    s.setName("\ns");
    cout << s << endl;
    csis << s << endl;
    IntArray t(1, 5);
    for (int i = t.low(); i <= t.high(); i++)
        t[i] = i * 10;
    t.setName("\nt");
    cout << t << endl;
    csis << t << endl;
    wait();
    s = t;
    cout << s << endl;
    cout << t << endl;
    cout << "Returns " << (s != t ? "True." : "False.") << endl << endl;
    csis << s << endl;
    csis << t << endl;
    csis << "Returns " << (s != t ? "True." : "False.") << endl << endl;
    wait();
}


void test13() {
    cout << "13. Overloaded inequality operator (different elements): IntArray u(3, 7);" << endl;
    cout << "                                                         IntArray v(1, 5);" << endl;
    cout << "                                                         u != v;" << endl << endl;
    csis << "13. Overloaded inequality operator (different elements): IntArray u(3, 7);" << endl;
    csis << "                                                         IntArray v(1, 5);" << endl;
    csis << "                                                         u != v;" << endl << endl;
    IntArray u(3, 7);
    for (int i = u.low(); i <= u.high(); i++)
        u[i] = i * 10;
    u.setName("\nu");
    cout << u << endl;
    csis << u << endl;
    IntArray v(1, 5);
    for (int i = v.low(); i <= v.high(); i++)
        v[i] = i * 10;
    v.setName("\nv");
    cout << v << endl;
    cout << "Returns " << (u != v ? "True." : "False.") << endl << endl;
    csis << v << endl;
    csis << "Returns " << (u != v ? "True." : "False.") << endl << endl;
    wait();
}

void test14() {
    cout << "14. Overloaded inequality operator (different size arrays): IntArray w(1, 3);" << endl;
    cout << "                                           IntArray x(1, 4);" << endl;
    cout << "                                           w != x;" << endl << endl;
    csis << "14. Overloaded inequality operator (different size arrays): IntArray w(1, 3);" << endl;
    csis << "                                           IntArray x(1, 4);" << endl;
    csis << "                                           w != x;" << endl << endl;
    IntArray w(1, 3);
    for (int i = w.low(); i <= w.high(); i++)
        w[i] = i * 10;
    w.setName("\nw");
    cout << w << endl;
    csis << w << endl;
    IntArray x(1, 4);
    for (int i = x.low(); i <= x.high(); i++)
        x[i] = i * 10;
    x.setName("\nx");
    cout << x << endl;
    cout << "Returns " << (w != x ? "True." : "False.") << endl << endl;
    csis << x << endl;
    csis << "Returns " << (w != x ? "True." : "False.") << endl << endl;
    wait();
}

void test15() {
    cout << "15. Sum of two arrays assigned to third array: IntArray a(1, 5);" << endl;
    cout << "                                               IntArray b(4, 8);" << endl;
    cout << "                                               IntArray c = a + b;" << endl << endl;
    csis << "15. Sum of two arrays assigned to third array: IntArray a(1, 5);" << endl;
    csis << "                                               IntArray b(4, 8);" << endl;
    csis << "                                               IntArray c = a + b;" << endl << endl;
    IntArray a(1, 5);
    for (int i = a.low(); i <= a.high(); i++)
        a[i] = i * 10;
    a.setName("\na");
    cout << a << endl;
    csis << a << endl;
    IntArray b(4, 8);
    for (int i = b.low(); i <= b.high(); i++)
        b[i] = i * 10;
    b.setName("\nb");
    cout << b << endl;
    csis << b << endl;
    wait();
    IntArray c = a + b;
    c.setName("\nc");
    cout << c << endl;
    csis << c << endl;
    wait();
}

void test16() {
    cout << "16. Sum of two arrays assigned to first array: IntArray d(10, 13);" << endl;
    cout << "                                               IntArray e(30, 33);" << endl;
    cout << "                                               d += e;" << endl << endl;
    csis << "16. Sum of two arrays assigned to first array: IntArray d(10, 13);" << endl;
    csis << "                                               IntArray e(30, 33);" << endl;
    csis << "                                               d += e;" << endl << endl;
    IntArray d(10, 13);
    for (int i = d.low(); i <= d.high(); i++)
        d[i] = i * 10;
    d.setName("\nd");
    cout << d << endl;
    csis << d << endl;
    IntArray e(30, 33);
    for (int i = e.low(); i <= e.high(); i++)
        e[i] = i * 10;
    e.setName("\ne");
    cout << e << endl;
    csis << e << endl;
    d += e;
    cout << d << endl;
    csis << d << endl;
    wait();
}

void test17() {
    cout << "17. Array declared with illegal array bounds: IntArray f(5, 2);" << endl << endl;
    csis << "17. Array declared with illegal array bounds: IntArray f(5, 2);" << endl << endl;
    IntArray f(5, 2);
    for (int i = f.low(); i <= f.high(); i++)
        f[i] = i * 10;
    f.setName("\nf");
    cout << f << endl;
    csis << f << endl;
    wait();
}

void test18() {
    cout << "18. Array with index out of range: IntArray g(10);" << endl;
    cout << "                                   g[10] = 1;" << endl << endl;
    csis << "18. Array with index out of range: IntArray g(10);" << endl;
    csis << "                                   g[10] = 1;" << endl << endl;
    IntArray g(10);
    for (int i = g.low(); i <= g.high(); i++)
        g[i] = i * 10;
    g.setName("\ng");
    cout << g << endl;
    csis << g << endl;
    g[10] = 1;
    wait();
}

void test19() {
    cout << "19. Arrays with length mismatch: IntArray m(1, 4);" << endl;
    cout << "                                 IntArray n(2, 4);" << endl;
    cout << "                                 m = n;" << endl << endl;
    csis << "19. Arrays with length mismatch: IntArray m(1, 4);" << endl;
    csis << "                                 IntArray n(2, 4);" << endl;
    csis << "                                 m = n;" << endl << endl;
    IntArray m(1, 4);
    for (int i = m.low(); i <= m.high(); i++)
        m[i] = i * 10;
    m.setName("\nm");
    cout << m << endl;
    csis << m << endl;
    IntArray n(2, 4);
    for (int i = n.low(); i <= n.high(); i++)
        n[i] = i * 10;
    n.setName("\nn");
    cout << n << endl;
    csis << n << endl;
    wait();
    m = n;
    cout << m << endl;
    cout << n << endl;
    csis << m << endl;
    csis << n << endl;
    wait();
}

void test20() {
    cout << "20. Array subscript operator: IntArray o(7, 8);" << endl;
    cout << "                              o[7] = 25;" << endl;
    cout << "                              o[8] = o[7];" << endl << endl;
    csis << "20. Array subscript operator: IntArray o(7, 8);" << endl;
    csis << "                              o[7] = 25;" << endl;
    csis << "                              o[8] = o[7];" << endl << endl;
    IntArray o(7, 8);
    for (int i = o.low(); i <= o.high(); i++)
        o[i] = i * 10;
    o.setName("\no");
    cout << o << endl;
    csis << o << endl;
    o[7] = 25;
    o[8] = o[7];
    cout << o << endl;
    csis << o << endl;
    wait();
}

void wait() {
    char buf;

    cout << "Press any key to continue." << endl;
    cin.get(buf);
}

-------------------------------------------------------------------------

//iadrv.h

#ifndef OPERATOROVERLOADINGPROGRAM_IADRV_H
#define OPERATOROVERLOADINGPROGRAM_IADRV_H

#include "intarray.h"

int main();
void test1();
void test2();
void test3();
void test4();
void test5();
void test6();
void test7();
void test8();
void test9();
void test10();
void test11();
void test12();
void test13();
void test14();
void test15();
void test16();
void test17();
void test18();
void test19();
void test20();
void wait();

#endif //OPERATOROVERLOADINGPROGRAM_IADRV_H

------------------------------------------------------------------------------------------

//intarray.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>

#include "intarray.h"

using namespace std;

IntArray::IntArray() {
    size = 9;

    operator[](9);
    dArray = new int[size];

    lowIndice = 0;
    highIndice = 9;

    elements = size + 1;
}

IntArray::IntArray(int b) {
    size = b-1;

    operator[](b-1);
    dArray = new int[size];

    lowIndice = 0;
    highIndice = b - 1;

    elements = b;
}

IntArray::IntArray(int a, int b) {
    size = b;

    dArray = new int[size];

    lowIndice = a;
    highIndice = b;

    elements = highIndice - lowIndice + 1;

    if (a > b) {
        cout << "Run-time Error: Illegal Array Bounds." << endl;
        cout << "Diagonstic: 1st indice cannot be bigger than 2nd indice" << endl;
    }
}

void IntArray::setName(string name){
    arrayName = name;
}

string IntArray::getName(){
    return arrayName;
}

int IntArray::low() {;
    return lowIndice;
}

int IntArray::high() {
    return highIndice;
}

ostream& operator<<(ostream& os, IntArray& a) {
    //displays the array object
    for (int i = a.low(); i <= a.high(); i++)
        os << a.getName() << "[" << i << "] = " << a.dArray[i] << " ";
    return os;
}

int& IntArray::operator[](int k){
    //if the index of the array is bigger than the size of the array. error will be shown.
    if (k > size) {
        cout << "Run-time Error: Illegal Index." << endl;
        cout << "Dignostic: Index cannot be bigger than size" << endl;
    }
    return dArray[k];
}

IntArray& IntArray::operator=(IntArray& b){
    //if elements do not match up, they cannot be assigned to each other. error will be shown.
    if (elements != b.elements) {
        cout << "Runtime error: Length mismatch" << endl;
        cout << "Diagnostic: Unable to assign, lengths do not match up." << endl;
    }

    return *this;
}


IntArray IntArray::operator+(IntArray& b) {
    //object is copied to temp object with its indices. then temp object is added with b object to produce c object.
    IntArray temp;
    IntArray c;

    c.lowIndice = lowIndice;
    c.highIndice = highIndice;

    for (int i = lowIndice, k = b.lowIndice; k <= b.highIndice; i++, k++) {
        temp.dArray[i] = b.dArray[k];
    }

    for (int i = lowIndice; i <= highIndice; i++) {
        c.dArray[i] = dArray[i] + temp.dArray[i];
    }

    return c;
}

IntArray IntArray::operator+=(IntArray& letter){
    //letter object is copied to temp object with its indices. then temp object is added to this object.
    IntArray temp;

    for (int i = lowIndice, k = letter.lowIndice; k <= letter.highIndice; i++, k++) {
        temp.dArray[i] = letter.dArray[k];
    }

    for (int i = lowIndice; i <= highIndice; i++) {
        dArray[i] = dArray[i] + temp.dArray[i];
    }

    return *this;
}

bool IntArray::operator==(IntArray& letter){
    if (elements == letter.elements) {
        return 1; //return true
    }
    else {
        return 0; // return false
    }

    if (size == letter.size) {
        return 1; // return false
    }
    else {
        return 0; // return true
    }
}

bool IntArray::operator!=(IntArray& letter){
    if (elements == letter.elements) {
        return 0; // return false
    }
    else {
        return 1; // return true
    }

    if (size == letter.size) {
        return 0; // return false
    }
    else {
        return 1; // return true
    }
}


-------------------------------------------------------------------

//intarray.h
#ifndef OPERATOROVERLOADINGPROGRAM_INTARRAY_H
#define OPERATOROVERLOADINGPROGRAM_INTARRAY_H


#include "iadrv.h"

using namespace std;

class IntArray {
private:
    string arrayName;
    int lowIndice; //low indice
    int highIndice; //high indice
    int elements; //number of elements
    int size; //size
    int *dArray; //dynamic array
public:
    //constructors
    IntArray();
    IntArray(int);
    IntArray(int, int);

    //set methods
    void setName(string);

    //get methods
    string getName(); //returns name
    int low(); //returns lowIndice
    int high(); //returns high indice

    //operator overloading
    friend ostream& operator << (ostream&, IntArray&);
    int& operator[](int); //subscript
    IntArray operator+(IntArray&); //additon
    IntArray operator+=(IntArray&);
    IntArray& operator=(IntArray&); //asignment
    bool operator==(IntArray&); //comparsion
    bool operator!=(IntArray&); //comparsion
};


#endif //OPERATOROVERLOADINGPROGRAM_INTARRAY_H

OperatorOverloadingProgram [-/CLionProjects/Operat Program] - .main.cpp gProgrammai.pp Debuga /Users/ swapnil/CLionProjects/0

Add a comment
Know the answer?
Add Answer to:
please help with the operator overloading lab (intArray) in c++ will provide what it is being...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....

    //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm. #include <iostream> using std::cout; using std::endl; template<class T> void sort(T a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //The assignment and < operator work for values of type T. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <=... <= a[numberUsed -...

  • Problem 1. Add overloaded “ = ”, “ + ”, “ += ” operators to the...

    Problem 1. Add overloaded “ = ”, “ + ”, “ += ” operators to the code below to enable direct operations on the safearray objects without using the indices of the safearray objects, and test them in the main() function. For instance, if we have two safearray objects “a” and “b” having elements [1,2,3,4] and another safearray object “c”, the overloaded “=”, “+”, “+=” operators will allow operations like “c=a;”, “c=a+b;” and “b+=a;” to have “c” hold [1,2,3,4], [2,4,6,8]...

  • When running the program at the destructor  an exception is being thrown. Can someone help me out?...

    When running the program at the destructor  an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public:    varArray(); // void constructor    int arraySize() const { return size; } // returns the size of the array    int check(double number); // returns index of element containg "number" or -1 if none    void addNumber(double); // adds number to the array    void removeNumber(double); // deletes the number from the array   ...

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • a) Hand-trace the following program and determine and write down what is the output of the...

    a) Hand-trace the following program and determine and write down what is the output of the code.                b) Run the code and compare the program output to your hand-traced result obtained from part (a). #include <iostream> #include <iomanip> using namespace std; void f(); int x = 5; int main() {         cout << "x = " << x << endl;         int x = 6;         cout << "x = " << x << endl;         {                int...

  • Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random...

    Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random numbers between 0 and 100. The array is sent to a function that finds the indices of all items > 50. A new array is created and the indices are stored inside it. The size of the new arrays MUST BE the same as the number of items > 50. The function returns the new array which is then printed out by main. Here...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • How do i Overload the & operator to concatenate two arrays?, so elements from both arrays...

    How do i Overload the & operator to concatenate two arrays?, so elements from both arrays will be seen Below is code i have so far. everything works except the operator overload of &. The & operator only works when both array are equal, first array smaller then second, fails when first array is the largest. class smartArray{ public: int *elements; // dynamic array, memory is allocated in the constructor. Use *this to access size member. int length(); // returns...

  • Fix this C++ code so that the function prototypes come before main. Main needs to be...

    Fix this C++ code so that the function prototypes come before main. Main needs to be declared first. Then call to the functions from inside of main. #include<cstdlib> using namespace std; //prompt user for input void getGrades(int gradeArray[],int size) { for(int i=0; i<size; i++) { cout<<"Enter the grade "<<i+1<<": "; cin>>gradeArray[i]; } } // finding average of all numbers in array float computeAverage(int numbers[],int size) { float sum=0; for(int i=0; i<size; i++) { sum = sum + numbers[i]; //compute sum...

  • Part-1: find the longest block (subsequence of elements with same value) in an array. Part-2: find...

    Part-1: find the longest block (subsequence of elements with same value) in an array. Part-2: find all subsequences in an array of int that add up to a given sum. */ #include <iostream> #include <cstdlib> using namespace std; void printSeq(int* a, int s, int e); // -------------------------------------------- functions to be implemented by you void longestBlock(const int* a, int n, int& s, int& e) { // s = start index of the block // e = end index of the block...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT