Answer to Question #270943 in C++ for jye

Question #270943

Defines the Array class that represents an array of integers. Please fulfill the following requirements

constructor, destructor, copy constructor

The function allows inputting data from the keyboard and the function displays to the screen for Array (2 functions)

The function allows to assign (write) and the function to get (read) the value of a certain element in the Array (2 functions)

Write a main program that illustrates how to use it


1
Expert's answer
2021-11-24T08:04:30-0500
#include <iostream>
using namespace std;
class Array{
    static const int MAX_SIZE = 100;
    int size = 0;
    int array[MAX_SIZE];


    public:
    Array(){}
    void inputData(){
        cout<<"Enter size of array: ";
        cin>>size;
        cout<<"Enter elements\n";
        for(int i = 0; i < size; i++){
            cin>>array[i];
        }
    }
    void display(){
        for(int i = 0; i < size; i++){
            cout<<array[i]<<" ";
        }
        cout<<endl;
    }
    void write(int index, int data){
        if(0 <= index && index < size){
            array[index] = data;
        }
    }
    int read(int index){
        if(0 <= index && index < size) return array[index];
        return INT_MIN;
    }
    ~Array(){}
    Array(Array &other){
        this->size = other.size;


        for(int i = 0; i < size; i++){
            this->array[i] = other.array[i];
        }
    }
};


int main(){
    Array array;
    array.inputData();
    cout<<"Your array is\n";
    array.display();


    Array array2(array);
    cout<<"Using copy constructor\n";
    array2.display();


    cout<<"Changing element at index 0 to 123\n";
    array.write(0, 123);


    cout<<"Your array is\n";
    array.display();
    cout<<"Element at index 0 is "<<array.read(0)<<endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS