Reimplement class Array from Figs. 10.10—10.11 of the textbook as a class template. Also write a main driver program tha

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Reimplement class Array from Figs. 10.10—10.11 of the textbook as a class template. Also write a main driver program tha

Post by answerhappygod »

Reimplement class Array from Figs. 10.10—10.11 of the textbook
as a class template. Also write a main driver program that:
Images of the class are below. I guess my biggest thing is - is
this asking for us to 'simplify' the code into a Template? Any help
with this would be great! I see some of the answers, and not all
make a template, neither do they talk about the how and the why.
Help is greatly appreciated!
Reimplement Class Array From Figs 10 10 10 11 Of The Textbook As A Class Template Also Write A Main Driver Program Tha 1
Reimplement Class Array From Figs 10 10 10 11 Of The Textbook As A Class Template Also Write A Main Driver Program Tha 1 (72.22 KiB) Viewed 38 times
Reimplement Class Array From Figs 10 10 10 11 Of The Textbook As A Class Template Also Write A Main Driver Program Tha 2
Reimplement Class Array From Figs 10 10 10 11 Of The Textbook As A Class Template Also Write A Main Driver Program Tha 2 (378.28 KiB) Viewed 38 times
Reimplement Class Array From Figs 10 10 10 11 Of The Textbook As A Class Template Also Write A Main Driver Program Tha 3
Reimplement Class Array From Figs 10 10 10 11 Of The Textbook As A Class Template Also Write A Main Driver Program Tha 3 (246.14 KiB) Viewed 38 times
1 // Fig. 10.10: Array.h 2 // Array class definition with overloaded operators. 3 #ifndef ARRAY_H 4 #define ARRAY_H 5 6 #include <iostream> 7 8 class Array { 9 friend std::ostream& operator<<(std::ostream&, const Array&); TO friend std::istream& operator>>(std::istream&, Array&); 11 12 public: 13 explicit Array(int = 10); // default constructor 14 Array (const Array&); // copy constructor 15 ~Array(); // destructor 16 size_t getSize() const; // return size 17 18 const Array& operator=(const Array&); // assignment operator 19 bool operator==(const Array&) const; // equality operator 20 21 // inequality operator; returns opposite of operator 22 bool operator !=(const Array& right) const { 23 return ! (*this == right); // invokes Array::operator== 24 } 25 26 // subscript operator for non-const objects returns modifiable Ivalue 27 int& operator[](int); 28 29 // subscript operator for const objects returns rvalue 30 int operator[] (int) const; 31 private: 32 size_t size; // pointer-based array size 33 int* ptr; // pointer to first element of pointer-based array 34 }; 35 36 #endif == Fig. 10.10 Array class definition with overloaded operators.
1 // Fig. 10.11: Array.cpp 2 // Array class member- and friend-function definitions. 3 #include <iostream> 4 #include <iomanip> 5 #include <stdexcept> > 6 7 #include "Array.h" // Array class definition 8 using namespace std; ; 9 10 // default constructor for class Array (default size 10) 11 Array::Array (int arraySize) 12 : size{(arraySize > O ? static_cast<size_t>(arraySize) : 13 throw invalid argument{"Array size must be greater than 0"})}, 14 ptr{new int[size]{}} { /* empty body */ } 15 16 // copy constructor for class Array; 17 // must receive a reference to an Array 18 Array::Array(const Array& arrayToCopy) 19 : size{arrayToCopy.size}, ptr{new int[size]} { 20 for (size_t i{0}; i < size; ++i) { 21 ptr = arrayToCopy.ptr; // copy into object 22 } 23 } 24 25 // destructor for class Array 26 Array: :~Array() { 27 delete[] ptr; // release pointer-based array space 28 } 29 30 // return number of elements of Array 31 size_t Array::getSize() const { 32 return size; // number of elements in Array 33 } 34 35 // overloaded assignment operator; 36 // const return avoids: (al = a2) = a3 37 const Array& Array::operator=(const Array& right) { 38 if (&right != this) { // avoid self-assignment 39 // for Arrays of different sizes, deallocate original 40 // left-side Array, then allocate new left-side Array 41 if (size != right.size) { 42 delete[] ptr; // release space 43 size = right.size; // resize this object 44 ptr = new int[size]; // create space for Array copy 45 } } 46 47 for (size_t i{0}; i < size; ++i) { 48 ptr = right.ptr; // copy array into object 49 } 50 } 51 52 return *this; // enables x = y = z, for example 53 } 54 55 // determine if two Arrays are equal and 56 // return true, otherwise return false 57 bool Array::operator==(const Array& right) const { 58 if (size != right.size) { 59 return false; // arrays of different number of elements 60 } 61 62 for (size_t i{0}; i < size; ++i) { 63 if (ptr != right.ptr) { 64 return false; // Array contents are not equal 65 } 66 } 67
69 ug的和722乃B789 68 return true; // Arrays are equal } 70 // overloaded subscript operator for non-const Arrays; 72 // reference return creates a modifiable Ivalue 73 int& Array::operator[](int subscript) { 74 // check for subscript out-of-range error 75 if (subscript < 0 || subscript >= size) { 76 throw out_of_range{"Subscript out of range"}; 77 } return ptr(subscript]; // reference return 80 } 81 82 // overloaded subscript operator for const Arrays 83 // const reference return creates an rvalue 84 int Array::operator[] (int subscript) const { 85 // check for subscript out-of-range error 86 if (subscript < 0 || subscript >= size) { 87 throw out_of_range{"Subscript out of range"}; 88 } 89 90 return ptr(subscript]; // returns copy of this element 91 } 92 93 // overloaded input operator for class Array; 94 // inputs values for entire Array 95 istream& operator>>(istream& input, Array& a) { for (size_t i{0}; i < a.size; ++i) { input >> a.ptr; 98 } 96 97 99 100 return input; // enables cin >> X >> y; 101 } 102 103 // overloaded output operator for class Array 104 ostream& operator<<(ostream& output, const Array& a) { 105 // output private ptr-based array 106 for (size_t i{0}; i < a.size; ++i) { 107 output « a.ptr « " "; 108 } 109 110 output << endl; III return output; // enables cout << x < y; 112} Fig. 10.11 Array class member- and friend -function definitions.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply