Question

Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...

Please do it carefully

Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class.

Hint: read all the explanations in the header with attention.


MyArray.h :


#ifndef MYARRAY_H
#define MYARRAY_H

#include <iostream>

using namespace std;

class MyArray
{

friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array
friend istream& operator>>( istream & input, MyArray & rhs); // to input the values of the array
public:
//constructors
MyArray(); //empty constructor , SIZE <- 1, data <- {0}
MyArray(int n); //SIZE <- n, data <- {0, ..., 0}
MyArray(int n, double val); //SIZE <- n, data <- {val, ..., val}
MyArray(const MyArray& src); //copy constructor
MyArray(int n, double* arr ); //SIZE <- n, data <- {arr[0], ..., arr[n-1]}
//utility functions
void printArray() const;
void fill(double val);
int getArraySize() const;
double* getArray();
double* getArray() const;

//indexing
double& operator[](int ind); //subscript operator for non-const objects returns modifiable lvalue
double operator[](int ind) const; //subscript operator for non-const objects returns rvalue

//assignment
MyArray& operator=(const MyArray& rhs);

//comparison
bool operator!=(const MyArray& rhs) const; //equality operator 1
bool operator==(const MyArray& rhs) const; //equality operator 2
//math functions
MyArray& operator+=(const MyArray& rhs) ; // additive assignment
MyArray& operator-=(const MyArray& rhs) ; // subtractive assignment
MyArray& operator*=(const MyArray& rhs) ; // (element-wise) multiplicative assignment
MyArray& operator/=(const MyArray& rhs) ; // (element-wise) divisive assignment
MyArray& operator^(const int& pwr) ; // element-wise power
private:
double* array;
int SIZE;
};

#endif



/* you need Dynamic memory Allocation , and example is given for empty constructor
MyArray::MyArray()
:SIZE(1)
{
//cout<<" constructor MyArray::MyArray()"<<endl;
SIZE=1;
array = new double[1];
fill(0);
}
*/
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//MyArray.h

#ifndef MYARRAY_H
#define MYARRAY_H
#include <iostream>

using namespace std;

class MyArray
{

   friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array
   friend istream& operator>>( istream & input, MyArray & rhs); // to input the values of the array
public:
   //constructors
   MyArray(); //empty constructor , SIZE <- 1, data <- {0}
   MyArray(int n); //SIZE <- n, data <- {0, ..., 0}
   MyArray(int n, double val); //SIZE <- n, data <- {val, ..., val}
   MyArray(const MyArray& src); //copy constructor
   MyArray(int n, double* arr ); //SIZE <- n, data <- {arr[0], ..., arr[n-1]}
  
   //utility functions
   void printArray() const;
   void fill(double val);
   int getArraySize() const;
   double* getArray();
   double* getArray() const;

   //indexing
   double& operator[](int ind); //subscript operator for non-const objects returns modifiable lvalue
   double operator[](int ind) const; //subscript operator for non-const objects returns rvalue

   //assignment
   MyArray& operator=(const MyArray& rhs);

   //comparison
   bool operator!=(const MyArray& rhs) const; //equality operator 1
   bool operator==(const MyArray& rhs) const; //equality operator 2
  
   //math functions
   MyArray& operator+=(const MyArray& rhs) ; // additive assignment
   MyArray& operator-=(const MyArray& rhs) ; // subtractive assignment
   MyArray& operator*=(const MyArray& rhs) ; // (element-wise) multiplicative assignment
   MyArray& operator/=(const MyArray& rhs) ; // (element-wise) divisive assignment
  
   MyArray& operator^(const int& pwr) ; // element-wise power
  
private:
   double* array;
   int SIZE;
};
#endif

//MyArray.cpp

#include "MyArray.h"
#include<math.h>
MyArray::MyArray(){
   SIZE = 1;
   array = new double[SIZE];
   for(int i=0;i<SIZE;i++)array[i] = 0;
}
MyArray::MyArray(int n){
   SIZE = n;
   array = new double[SIZE];
   for(int i=0;i<SIZE;i++)array[i] = 0;
}
MyArray::MyArray(int n, double val){
   SIZE = n;
   array = new double[SIZE];
   for(int i=0;i<SIZE;i++)array[i] = val;
}
MyArray::MyArray(const MyArray& src){
   SIZE = src.SIZE;
   array = new double[SIZE];
   for(int i=0;i<SIZE;i++)array[i] = src.array[i];
}
MyArray::MyArray(int n, double* arr ){
   SIZE = n;
   array = new double[SIZE];
   for(int i=0;i<SIZE;i++)array[i] = arr[i];
}
  
   //utility functions
void MyArray::printArray() const{
   for(int i=0;i<SIZE;i++){
       cout<<array[i]<<" ";
   }
}
void MyArray::fill(double val){
   for(int i=0;i<SIZE;i++)array[i] = val;
}
int MyArray::getArraySize() const{
   return SIZE;
}
double* MyArray::getArray(){
   return array;
}
double* MyArray::getArray() const{
   return array;
}

   //indexing
double& MyArray::operator[](int ind){
   return array[ind];
}
double MyArray::operator[](int ind) const{
   return array[ind];
}

   //assignment
MyArray& MyArray::operator=(const MyArray& rhs){
  
   return *(new MyArray(rhs));
}

   //comparison
bool MyArray::operator!=(const MyArray& rhs) const{
   if(SIZE != rhs.SIZE)return true;
   for(int i=0;i<SIZE;i++){
       if(array[i] != rhs.array[i])return true;
   }
   return false;
}
bool MyArray::operator==(const MyArray& rhs) const{
   if(SIZE != rhs.SIZE)return false;
   for(int i=0;i<SIZE;i++){
       if(array[i] != rhs.array[i])return false;
   }
   return true;
}
  
   //math functions
MyArray& MyArray::operator+=(const MyArray& rhs) {
   for(int i=0;i<SIZE && i<rhs.SIZE ;i++){
       array[i]+=rhs.array[i];
   }
   return *this;
}
MyArray& MyArray::operator-=(const MyArray& rhs) {
   for(int i=0;i<SIZE && i<rhs.SIZE ;i++){
       array[i]-=rhs.array[i];
   }
   return *this;
}
MyArray& MyArray::operator*=(const MyArray& rhs) {
   for(int i=0;i<SIZE && i<rhs.SIZE ;i++){
       array[i]*=rhs.array[i];
   }
   return *this;
}
MyArray& MyArray::operator/=(const MyArray& rhs){
   for(int i=0;i<SIZE && i<rhs.SIZE ;i++){
       array[i]/=rhs.array[i];
   }
   return *this;
}
  
MyArray& MyArray::operator^(const int& pwr) {
   for(int i=0;i<SIZE ;i++){
       array[i] =   pow(array[i] , pwr);
   }
   return *this;
}
ostream& operator<<( ostream & output, const MyArray & rhs){
   for(int i=0;i<rhs.SIZE;i++)
       output<<rhs.array[i]<<" ";
   return output;
}
istream& operator>>( istream & input, MyArray & rhs){
   for(int i=0;i<rhs.SIZE;i++)
       input>>rhs.array[i];
   return input;
}

//main.cpp

#include"MyArray.cpp"

int main(){
   double arr1[] = {1,2,3,4};
   MyArray myarr1(4,arr1);
   MyArray myarr2(8,2.5);
  
   cout<<myarr1<<endl;
   cout<<myarr2<<endl;
   MyArray myarr3(myarr1);
   cout<<myarr3<<endl;
  
   MyArray myarr4 = myarr3;
   cout<<myarr4<<endl;
}

Add a comment
Know the answer?
Add Answer to:
Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...
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
  • Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include...

    Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include <iostream> using namespace std; template <typename T> struct Int_Node {    T value;    Int_Node<T> *pre, *next; }; template <typename T> class Link_List {    template <typename U>    friend ostream &operator<<(ostream &, const Link_List<U> &);// print all integers in the list    template <typename U>    friend istream &operator>>(istream &, Link_List<U> &);// input a value at the back of the list, like insert_node(val);...

  • C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...

    C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it. Your goal is to overload the operators for a generic “BigInt” class. You will need to write...

  • C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that...

    C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that can grow in size. Suppose that vectors were not defined in C++. Define a class called VectorDoublethat is like a class for a vector with base type double. Your class VectorDoublewill have a private member variable for a dynamic array of doubles. It will also have two member variables of type...

  • 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...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • Please give a answer to both tasks with a screenshot of the running file. Thanks Consider...

    Please give a answer to both tasks with a screenshot of the running file. Thanks Consider that individual nodes in an unsorted linked list have the following definition class Vector public: Vector(int s = 0)( // makes Size //allocates s space, // makes all entries s, kes all entries e Vector (const Vector & rhs) // copy constructor // makes self a deep copy of rhs Vector operator (const Vector & rhs)(// makes self a deep copy of rhs nVector...

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

  • Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Template...

    Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Template Classes – Chapter 12 Write an example of a template function that can swap 2 generic elements. Create a C++ class vector using the class diagram shown in Figure 12.2. in Liangs textbook. Do not use the C++ provided header file <vector>...

  • 17. Write a non-member function called centroid(param) that takes a static array of points and the...

    17. Write a non-member function called centroid(param) that takes a static array of points and the size of the array and return the centroid of the array of points. If there is no center, return the origins private: double x, y, z public: /Constructors Point(); Point(double inX, double inY, double inZ = 0); Point(const Point& inPt); / Get Functions double getX() const; double getY) const; double getZ) const; Set Functions void setX(double inX); void setY(double inY); void setZ(double inZ); void...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

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