Answered You can hire a professional tutor to get the answer.
Hi, I need help with this code. when I keep having error message in line 42 "array h". how can you help me please?
Hi, I need help with this code. when I keep having error message in line 42 "array h". how can you help me please?
here is the assignment and the code:
Reimplement class Array as a class template. I need to code a main driver program that S-22 A. I need to ask the user for integer values, store them in an integer Array, and then prints the values stored in the integer Array B. Then, ask the user for string values, store them in a string array, and then print the values stored in the string Array
array.h : --------------->>>>>>>>>>>>
#ifndef ARRAY_H
#define ARRAY_H
#include<iostream>
using namespace std;
template<class T>
class Array{
private:
size_t size;
T *ptr;
friend std::istream& operator>>(std::istream &in,Array<T> &other){
for(int i = 0;i<other.getSize();i++){
in>>other.ptr[i];
}
}
friend ostream& operator<<(ostream &out,const Array<T> &other){
for(int i = 0;i<other.getSize();i++){
out<<other.ptr[i]<<" ";
}
}
public:
explicit Array(int = 10);
Array(const Array<T>&);
size_t getSize() const;
~Array();
const Array<T>& operator=(const Array<T>&);
bool operator==(const Array<T>&) const;
bool operator!=(const Array<T> &right) const{
return (*this == right);
}
T& operator[](int);
T operator[](int) const;
};
#endif
array.cpp : ---------------->>>>>>>>>>>.
#include "array.h"
#include<stdexcept>
#include<iomanip>
template<class T>
Array<T>::Array(int arraySize){
if(arraySize <= 0){
throw invalid_argument{"Array size must be greater than 0"};
}
size = static_cast<size_t>(arraySize);
ptr = new T[size];
}
template<class T>
Array<T>::Array(const Array<T> &other){
size = other.size;
ptr = new T[size];
for(int i = 0;i<size;i++){
ptr[i] = other.ptr[i];
}
}
template<class T>
size_t Array<T>::getSize()const{
return size;
}
template<class T>
T& Array<T>::operator[](int index){
if(index >= 0 && index < size){
return ptr[index];
}else{
throw out_of_range{"Subscript out of Range"};
}
}
template<class T>
T Array<T>::operator[](int index)const{
if(index >= 0 && index < size){
return ptr[index];
}else{
throw out_of_range{"subscript out of range"};
}
return 0;
}
template<class T>
bool Array<T>::operator==(const Array<T> &other) const{
if(size != other.size){
return false;
}
for(int i = 0;i<size;i++){
if(ptr[i] != other.ptr[i]){
return false;
}
}
return true;
}
template<class T>
Array<T>::~Array(){
delete[] ptr;
size = static_cast<size_t>(0);
}
template<class T>
const Array<T>& Array<T>::operator=(const Array<T> &other){
if(&other != this){
if(size != other.size){
delete[] ptr;
size = other.size;
ptr = new T[size];
}
for(int i = 0;i<size;i++){
ptr[i] = other.ptr[i];
}
}
return *this;
}
arraycheck.cpp : ------------->>>>>>>>>
#include "array.cpp"
int main(){
Array<string> str(3);
str[0] = "style";
str[1] = "the";
str[2] = "werty";
cout<<str;
Array<int> intr(2);
intr[0] = 24;
intr[1] = 25;
cout<<endl;
cout<<intr;
}