Question

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] and “b” hold [2,4,6,8], respectively.

"Safe array " Code Below:

/* creates safe arrays using overload [] and return by reference */

#include<iostream>

#include<cstdlib>

using namespace std;

const int LIMIT = 10;

class safearray

{

   private:

       int arr[LIMIT];  

   public:

       int& operator [](int n)       // access elements using overloade [] returning by reference

       {

           if (n<0 || n>LIMIT) { cout << "Index out of bounds \n"; exit(1); }

           return arr[n];

       }

};

int main()

{

   safearray a;

   int i;

   for (i=0;i<LIMIT;i++) { a[i]=i; }   // incert elements

   for (i=0;i<LIMIT;i++) {cout << a[i]; cout << " ";}   // display elements

   cout << endl;

   return 0;

}

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

Please go threw code and output.

#include<iostream>

#include<cstdlib>

using namespace std;

const int LIMIT = 4;

class safearray

{

private:

int arr[LIMIT];  

public:

int& operator [](int n) // access elements using overloade [] returning by reference
{

if (n<0 || n>LIMIT) { cout << "Index out of bounds \n"; exit(1); }

return arr[n];

}
safearray* operator=(safearray& a) // access elements using overloade [] returning by reference
{
//safearray temp;
for(int i =0; i<LIMIT; i++)
arr[i] = a[i];
return this;
}
safearray operator+(safearray& a) // access elements using overloade [] returning by reference
{
safearray temp;
for(int i =0; i<LIMIT; i++)
temp.arr[i] = arr[i]+a[i];
return temp;
}
void operator+=(safearray& a) // access elements using overloade [] returning by reference
{
for(int i =0; i<LIMIT; i++)
arr[i] += a[i];
}
};

int main()

{

safearray a;
safearray b;
safearray c;
int i;

for (i=0;i<LIMIT;i++) { a[i]=i+1; } // incert elements
for (i=0;i<LIMIT;i++) { b[i]=i+1; } // incert elements
c = a;
for (i=0;i<LIMIT;i++) {cout << c[i]; cout << " ";} // display elements
cout << endl;
//c =(a+b); // no idea why not working
for (i=0;i<LIMIT;i++) {cout << c[i]; cout << " ";} // display elements
cout << endl;
b+=a;
for (i=0;i<LIMIT;i++) {cout << b[i]; cout << " ";} // display elements
cout << endl;

return 0;

}

1 234 1 2 34 2 468

Add a comment
Know the answer?
Add Answer to:
Problem 1. Add overloaded “ = ”, “ + ”, “ += ” operators to the...
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
  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • Can someone explain how this C++ program runs? A line by line explanation/commentation would be great,...

    Can someone explain how this C++ program runs? A line by line explanation/commentation would be great, as well as the purpose of the program and functions/classes involved. #include <iostream> #include <vector> using namespace std; // template function vector<int> removeEvenIndexedVals(vector<int> vec); // main int main() {        static const int arr[] = { 2,5,7,9,1,3,6 };        vector<int> vec(arr, arr + sizeof(arr) / sizeof(arr[0]));        // call function        vec = removeEvenIndexedVals(vec);        // print        cout << "Displaying the Vector Elements:"...

  • write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer...

    write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer class (Computer) that describes a computer object. A computer has the following properties: Amount of Ram in GB (examples: 8, 16), defaults to 8. Hard drive size in GB (examples: 500, 1000), defaults to 500. Speed in GHz (examples: 1.6, 2.4), defaults to 1.6. Type (laptop, desktop), defaults to "desktop" Provide the following functions for the class: A default constructor (constructor with no parameters)...

  • You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function...

    You are given a Q1.h file with overloaded function prototypes for isOrdered. Implement this overloaded function into a file named Q1.cpp. Q1.cpp should only include your function implementation, the necessary #include directives if needed, and should not contain anything else such as the main function or global variable declarations. Test your code using a separate main.cpp file where you implement a sufficient number of test cases. You should use Q1.h as a header file and Q1main.cpp as a main function...

  • c++ #include using namespace std; int main() {    int n, x, num, j = 0;...

    c++ #include using namespace std; int main() {    int n, x, num, j = 0;    cin >> n >> x;    int*arr = new int[n];    for (int i = 0; i < n; i++)    {        cin >> num;        if (num < x)        {            arr[j] = num;            j++;        }    }    for (int i = 0; i < j; i++)    {...

  • graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <alg...

    graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <algorithm> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //Execution Begins Here! int main(int argc, char** argv) { int n, i, arr[50], search, first, last, middle,count=0,count_in,tot; clock_t start, end; float duration; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter numbers"; for (i=0; i<n;i++) cin>>arr[i]; cout<<"Enter a...

  • #include<iostream> #include<cstdlib> using namespace std; int main() {     // create array of size 20    ...

    #include<iostream> #include<cstdlib> using namespace std; int main() {     // create array of size 20     int arr[20];         int i;         cout<<"scores : ";     for( i = 0 ; i < 20 ; i++ )     {         // generate a random number i range 70 - 100 an add it to arr         arr[i] = rand() % 31 + 70;                 cout<<arr[i]<<" ";     }         // store the total score     int total = 0;...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Hello, im looking for help on adding overload operators to my code. It needs to consist...

    Hello, im looking for help on adding overload operators to my code. It needs to consist of + (addition) for the Fraction class * (multiplication), no need not reduce == (equality operator) = (assignment operator) << and >> so in main you can use << and >> as follows:  cin>>frac1; cout<< frac1; where frac1 is a an object of the Fraction class   the original code is: #include <iostream> using namespace std; class Fractions { private:    int numerator;    int denominator;...

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