Question
C++ Template for int double char C++ Help Class templates. I need to modify my Array class so that it can read int double and
C++ Template for int double char
C++ Help Class templates. I need to modify my Array class so that it can read int double and char types. I have the following to be modified: Array.h
#ifndef ARRAY_H #define ARRAY_H
#include
class Array { friend std::ostream &operator>( std::istream &, Array & );
public: explicit Array( int = 3 ); // default constructor ~Array(); // destructor size_t getSize() const; // return size
const Array &operator=( const Array & ); // assignment operator bool operator==( const Array & ) const; // equality operator // subscript operator for const objects returns rvalue int operator[]( int ) const; private: size_t size; // pointer-based array size int *ptr; // pointer to first element of pointer-based array }; // end class Array
#endif
Array.cpp
#include
#include
#include
#include "Array.h" // Array class definition
using namespace std;
// default constructor for class Array (default size 3)
Array::Array( int arraySize )
: size( arraySize > 0 ? arraySize :
throw invalid_argument( "Array size must be greater than 0" ) ),
ptr( new int[ size ] )
{
for ( size_t i = 0; i
ptr[ i ] = 0; // set pointer-based array element
} // end Array default constructor
// destructor for class Array
Array::~Array()
{
delete [] ptr; // release pointer-based array space
} // end destructor
// return number of elements of Array
size_t Array::getSize() const
{
return size; // number of elements in Array
} // end function getSize
// overloaded assignment operator;
// const return avoids: ( a1 = a2 ) = a3
const Array &Array::operator=( const Array &right )
{
if ( &right != this ) // avoid self-assignment
{
// for Arrays of different sizes, deallocate original
// left-side Array, then allocate new left-side Array
if ( size != right.size )
{
delete [] ptr; // release space
size = right.size; // resize this object
ptr = new int[ size ]; // create space for Array copy
} // end inner if
for ( size_t i = 0; i
ptr[ i ] = right.ptr[ i ]; // copy array into object
} // end outer if
return *this; // enables x = y = z, for example
} // end function operator=
// determine if two Arrays are equal and
// return true, otherwise return false
bool Array::operator==( const Array &right ) const
{
if ( size != right.size )
return false; // arrays of different number of elements
for ( size_t i = 0; i
if ( ptr[ i ] != right.ptr[ i ] )
return false; // Array contents are not equal
return true; // Arrays are equal
} // end function operator==
// overloaded subscript operator for const Arrays
// const reference return creates an rvalue
int Array::operator[]( int subscript ) const
{
// check for subscript out-of-range error
if ( subscript = size )
throw out_of_range( "Subscript out of range" );
return ptr[ subscript ]; // returns copy of this element
} // end function operator[]
// overloaded input operator for class Array;
// inputs values for entire Array
istream &operator>>( istream &input, Array &a )
{
for ( size_t i = 0; i
input >> a.ptr[ i ];
return input; // enables cin >> x >> y;
} // end function
// overloaded output operator for class Array
ostream &operator
{
// output private ptr-based array
for ( size_t i = 0; i
{
output
if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
output
} // end for
if ( a.size % 4 != 0 ) // end last line of output
output
return output; // enables cout
} // end function operator
For it te be class teamplate we need to put all the code from array.cpp to array.h
Here is the main function:
#include
#include
#include
#include "Array.h"
using namespace std;
int main()
{
Array integers1( 2 ); // 2-element Array
Array integers2; // 3-element Array by default
// print integers1 size and contents
cout
// print integers2 size and contents
cout
// input and print integers1 and integers2
cout
cin >> integers1 >> integers2;
cout
// use overloaded assignment (=) operator
cout
integers1 = integers2; // note target Array is smaller
cout
// use overloaded equality (==) operator
cout
if ( integers1 == integers2 )
cout
else
cout
// use overloaded subscript operator to create rvalue
cout
} // end main
In the main function we need to show that class tamplate works with int double and char so we need a function template for manipulations.;
Output:
ize of first Array object is 2 Array after initialization: Size of second Array object is 3 Array after initialization: Enter 5 int values: 55 50 45 40 35 After input, the Arrays contain: First Array object: 50 Second Array object: 45 40 35 Assigning second Array object to the first Array object: First Array object: 45 40 35 Second Array object: 45 40 35 Evaluating for equality The Array objects are equal Value of first element in first Array object is 45 Size of first Array object is 2 Array after initialization: Size of second Array object is 3 Array after initialization: Enter 5 double values: 5.5 5.0 4.5 4.0 3.5 After input, the Arrays contain: First Array object: Second Array object: 4.5 4 Assigning second Array object to the first Array object: First Array object: 4.5 4 Second Array object: 4.5 4 Evaluating for equality The Array objects are equal Value of first element in first Array object is 4.5 Size ot first Array object is 2 Array after initialization: Size of second Array object is 3 Array after initialization: Enter 5 char values a b c d e After input, the Arrays contain: First Array object: Second Array object
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started