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...
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 */...
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()...
In a header file Record.h, create a Record structure that contains the following information: recordID, firstName,...
In a header file Record.h, create a Record structure that contains the following information: recordID, firstName, lastName, startYear. recordID is an integer. In testRecord, first create a record (record1) using “normal” pointers. Set the values to 1001, Fred, Flintstone, 1995 In testRecord, create a new record (record2) using a unique smart pointer. Set the values to 1002, Barney, Rubble, 2000 Create a function (or functions) that will print the records to any output stream. Since the print function does not...
Given the following frame, answer the questions that follow. No Ethernet header!
Given the following frame, answer the questions that follow. No Ethernet header!
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Create a header file, iofunctions.h, containing the following function prototypes. (don't forget to follow the coding...
Create a header file, iofunctions.h, containing the following function prototypes. (don't forget to follow the coding style) int readfile( struct pokemon pokearray[ ], int* num, char filename[ ] ); int writefile( struct pokemon pokearray[ ], int num, char filename[ ] ); Then, create a source file, iofunctions.c, defining the functions above. Specifications The readfile function will read the data from a text file and store it in the array called by pokearray. It must not load the pokemons more than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT