Question

In: Computer Science

Consider the following header file for the intArray object and the following main.cpp and answer the...

Consider the following header file for the intArray object and the following main.cpp and answer the following questions:

intArray.h

main.cpp

#include <iostream>

using namespace std;

class intArray

{

friend ostream& operator<<(ostream& outStream, intArray& rhs);

public:

    intArray();

    intArray(int _size);

    intArray(int _size, int array[]);

    intArray(const intArray&);

    ~intArray();

    void Print();

    void PrintSize();

    int Size() const;

    int operator[](int) const;

    intArray operator=(const intArray );

private:

    int size;

    int *data;

};

7 #include <iostream>
8 #include <cstdlib>
9 #include "intArray.h"
10
11 using namespace std;
12
13 /*
14 *
15 */
16 int main(int argc, char** argv) {
17
18     intArray Array1(5, 1);
19     intArray Array2(5, 2);
20     intArray Array3;
21    
22     Array3 = Array1 + Array2;
23     Array3 = Array1 + 5;
24     Array3 = 6 + Array1;

25    
26    
27     return 0;
28 }

Give the only prototype for the overloaded + operators as shown in lines 22, 23 and 24. Make sure all are cascade capable to allow the assignment to Array3.

Solutions

Expert Solution

Prototypes are written below alongwith the explanation. If you need any further clarification please feel free to ask in comments.

Line 22 Array3=Array1 +Array2;

Prototype:--->>> const intArray operator +( const intArray&);

Explain: As the assignment wants to add two intArray objects and return another intArray object, we need a overloaded function that returns intArray object and takes intArray object as parameter. The above operator overloaded function fullfills all the conditions for this assignemnt.

Line 23 Array3=Aray1 +5;

Prototype:---->>> const intArray operator +(const int);

Explain: As the assignment wants to add an intArray object and integer & return another intArray object, we need a overloaded function that returns intArray object and takes integer as parameter. The above operator overloaded function fullfills all the conditions for this assignemnt.

Line 24 Array3= 6 + Array1;

Prototype:---->>>> friend intArray operator +(const int, const intArray&);

Explain: As the assignment is done using integer as first argument and intArray object as second, We need a friend function which is operator overloaded in nature. A friend function which takes integer as first argument and intArray object as second parameter. The above friend operator overloaded function fullfills all the conditions for this assignemnt.


Related Solutions

Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below. ========================================================= // listmain.cpp #include "Linked_List.h" int main(int argc, char **argv) {      float           f;      Linked_List *theList;      cout << "Simple List Demonstration\n";      cout << "(List implemented as an Array - Do...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language A Game store sells many types of gaming consoles. The console brands are Xbox, Nintendo, PlayStation. A console can have either 16 or 8 gigabytes of memory. Use can choose the shipping method as either Regular (Cost it $5) or Expedite (Cost is $10) The price list is given as follows: Memory size/Brand Xbox Nintendo PlayStation 16 gigabytes 499.99 469.99 409.99 8 gigabytes 419.99...
HW_6b - Read and write a text file. Include the following in the main.cpp file. Add...
HW_6b - Read and write a text file. Include the following in the main.cpp file. Add code so that the numbers that are read from the data.txt file are written to an output           file named:   results.txt After the numbers are written to the file, the following message should be displayed:    The data has been written to the file. Open the results.txt file and verify that the file was written successfully. Please make the code based on C++,...
HW_6c - Append data to existing data in a file. Include the following in the main.cpp...
HW_6c - Append data to existing data in a file. Include the following in the main.cpp file. Prompt the user to enter 3 more numbers, then read the numbers and write them to             the results.txt file. Include code so that when the data is written to file, it is appended (added on to the     end of the existing data). Open the results.txt file and verify that the file was written successfully. /* OUTPUT Here are the numbers...
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function...
please use text only! Instructions Consider the provided C++ code in the main.cpp file: The function func2 has three parameters of type int, int, and double, say a, b, and c, respectively. Write the definition of func2 so that its action is as follows: Prompt the user to input two integers and store the numbers in a and b, respectively. If both of the numbers are nonzero: If a >= b, the value assigned to c is a to the...
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library....
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library. Your C code //SHOULD access your routines using these exact function // prototypes typedef struct RW_lock_s { } RW_lock_t; void RW_lock_init(RW_lock_t *lock); /* This routine should be called on a pointer to a struct variable of RW_lock_t to initialize it and ready it for use. */ void RW_read_lock(RW_lock_t *lock); /* This routine should be called at the beginning of a READER critical section */...
Given the header file (grid.h) and the source file,(grid.cpp) create the file trap.cpp. Here are the...
Given the header file (grid.h) and the source file,(grid.cpp) create the file trap.cpp. Here are the requirements for trap.cpp: Write a main program in a file called trap.cpp that solves the following scenario, using your Grid class: Giant Mole People have risen from their underground lairs and are taking over the world. You have been taken prisoner and placed in an underground jail cell. Since the Mole People are blind and don't know how to build doors, your cell has...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ polygon.h file- #ifndef POLY_RVC_H #define POLY_RVC_H #include <iostream> using namespace std; class Polygon { public:    Polygon();    Polygon(int n, double l);    //accessors - all accessors should be declared "const"    // usually, accessors are also inline functions    int getSides() const { return sides; }    double getLength()...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ Write a polygon.h file with given instructions for the polygon.cpp file #include <iostream> #include <math.h> using namespace std; #ifndef M_PI # define M_PI 3.14159265358979323846 #endif int main() {    float areaP, length, sides;    cout << "\n\n Polygon area program.\n";    cout << "---------------------------------\n";    cout << " Enter the...
Given the following frame, answer the questions that follow. No Ethernet header!
Given the following frame, answer the questions that follow. No Ethernet header!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT