Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below.
#include "Array.h"
main()
{
Array<char> c(3);
c.setValue(0,'c');
c.setValue(1,'s');
c.setValue(2,'c');
cout << c;
Array<int> i(3);
i.setValue(0,1);
i.setValue(1,2);
i.setValue(2,5);
cout << i;
Array<int> j(3);
j.setValue(0,10);
j.setValue(1,20);
j.setValue(2,50);
cout << j;
Array<int> ij;
ij = i + j;
cout << ij;
}
Array.h
#ifndef ARRAY_H_
#define ARRAY_H_
#include <iostream>
using namespace std;
class Array
{
public:
Array(void);
Array(int n);
Array(const Array &a);
~Array();
int getSize(void);
int setValue(int n, float v);
float getValue(int n);
void operator =(const Array &a);
friend Array operator +(Array &a, Array &b);
friend Array operator +(Array &a, float b);
friend ostream &operator <<(ostream &output, const
Array &a);
private:
float *f;
int n;
};
#endif
Array.cpp
#include "Array.h"
Array::Array(void)
{
this->n = 0;
this->f = 0;
return;
}
Array::Array(int n)
{
this->n = n;
this->f = new float[n];
return;
}
Array::Array(const Array &a)
{
if(f) delete[] this->f;
*this = a;
return;
}
Array::~Array()
{
if(f) delete[] this->f;
return;
}
int Array::getSize(void)
{
return this->n;
}
int Array::setValue(int n, float v)
{
if((n >= 0) && (n < this->n))
{
this->f[n] = v;
return 0; // index in range
}
return 1; // index out of range
}
float Array::getValue(int n)
{
return this->f[n];
}
void Array::operator =(const Array &a)
{
int d;
if(this != &a)
{
if(f) delete[] this->f;
this->n = a.n;
this->f = new float[this->n];
for(d = 0; d < this->n; d++) this->f[d] = a.f[d];
}
return;
}
Array operator +(Array &a, Array &b)
{
int c, d;
if(a.getSize() < b.getSize()) c = a.getSize();
else c = b.getSize();
Array n(c);
for(d = 0; d < c; d++) n.setValue(d, a.getValue(d) +
b.getValue(d));
return n;
}
Array operator +(Array &a, float b)
{
int d;
Array n(a.getSize());
for(d = 0; d < a.getSize(); d++) n.setValue(d, a.getValue(d) +
b);
return n;
}
ostream &operator <<(ostream &output, const Array
&a)
{
output << "Array:" << endl;
output << " Size: " << a.n << endl;
output << " Contents: ";
for(int b = 0; b < a.n; b++) output << a.f[b] <<
"\t";
return output;
}
main.cpp
#include "Array.h"
#include <iostream>
using namespace std;
int main(void)
{
Array a1( 3 );
a1.setValue( 0, 1.0 );
a1.setValue( 1, 22.0 );
a1.setValue( 2, 12.2 );
Array a2( 3 );
a2.setValue( 0, 3.3 );
a2.setValue( 1, 44.5 );
a2.setValue( 2, 21.7 );
Array tmp;
tmp = a1 + a2;
cout << tmp;
return 0;
}
/* Array.h */
#ifndef ARRAY_H_
#define ARRAY_H_
#include <iostream>
using namespace std;
template<class X>
class Array
{
public:
Array(void);
Array(int n);
Array(const Array &a);
~Array();
int getSize(void);
int setValue(int n, X v);
X getValue(int n);
void operator =(const Array &a);
friend Array operator +(Array &a, Array &b){
int c, d;
if(a.getSize() < b.getSize()) c =
a.getSize();
else c = b.getSize();
Array n(c);
for(d = 0; d < c; d++) n.setValue(d, a.getValue(d)
+ b.getValue(d));
return n;
}
friend Array operator +(Array &a, X b){
int d;
Array n(a.getSize());
for(d = 0; d < a.getSize(); d++) n.setValue(d,
a.getValue(d) + b);
return n;
}
friend ostream &operator <<(ostream &output, const
Array &a){
output << "Array:" << endl;
output << " Size: " << a.n <<
endl;
output << " Contents: ";
for(int b = 0; b < a.n; b++) output << a.f[b]
<< "\t";
return output;
}
private:
X *f;
int n;
};
#endif
*/ Array.cpp */
#include "Array.h"
template<class X>
Array<X>::Array(void)
{
this->n = 0;
this->f = 0;
return;
}
template<class X>
Array<X>::Array(int n)
{
this->n = n;
this->f = new X[n];
return;
}
template<class X>
Array<X>::Array(const Array <X>&a)
{
if(f) delete[] this->f;
*this = a;
return;
}
template<class X>
Array<X>::~Array()
{
if(f) delete[] this->f;
return;
}
template<class X>
int Array<X>::getSize(void)
{
return this->n;
}
template<class X>
int Array<X>::setValue(int n, X v)
{
if((n >= 0) && (n < this->n))
{
this->f[n] = v;
return 0; // index in range
}
return 1; // index out of range
}
template<class X>
X Array<X>::getValue(int n)
{
return this->f[n];
}
template<class X>
void Array<X>::operator =(const Array <X>&a)
{
int d;
if(this != &a)
{
if(f) delete[] this->f;
this->n = a.n;
this->f = new X[this->n];
for(d = 0; d < this->n; d++) this->f[d] = a.f[d];
}
return;
}
/* main.cpp */
#include "Array.cpp"
#include<iostream>
main()
{
Array<char> c(3);
c.setValue(0,'c');
c.setValue(1,'s');
c.setValue(2,'c');
cout << c;
Array<int> i(3);
i.setValue(0,1);
i.setValue(1,2);
i.setValue(2,5);
cout << i;
Array<int> j(3);
j.setValue(0,10);
j.setValue(1,20);
j.setValue(2,50);
cout << j;
Array<int> ij;
ij = i + j;
cout << ij;
}
/* OUTPUT */
Array:
Size: 3
Contents: c s c Array:
Size: 3
Contents: 1 2 5 Array:
Size: 3
Contents: 10 20 50 Array:
Size: 3
Contents: 11 22 55
/* PLEASE UPVOTE (THANK YOU IN ADVANCE) */
Redesign your Array class from lab6 as a class template to work with the application below....
IN C++ Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing the appropriate template argument. Integer template arguments will also be used to set the upper and lower bounds of the array. Provide all necessary functionality to allow the class to act as an array ([] operator, = operator etc.). The array does not need to provide input or output methods to act on...
I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...
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]....
In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...
C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class in separate files for the interface and implementation so that the implementation can be compiled separately from any application program. Submit three files: YourLastNameFirstNameInitialMoney.cpp - This file contains only the Money class implementation YourLastNameFirstNameInitialProj4.cpp - This file contains only the main function to use/test your money class YourLastNameFirstNameInitialMoney.h - This file contains the header file information. Optional bonus (10%): Do some research on make...
C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...
This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...
/* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) { x=y=0;} shape(int xvalue, int yvalue); void setShape(int new_x, int new_y); void setX(int new_x); void setY(int new_y); int getX( ) const; int getY( ) const; virtual void move(int x, int y) = 0; virtual void shift(int dx, int dy) = 0; virtual void draw( ) = 0; virtual void rotate(double r) = 0; virtual void print(ostream&)const; friend ostream & operator<<(ostream & os, const shape& s);...
When running the program at the destructor an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public: varArray(); // void constructor int arraySize() const { return size; } // returns the size of the array int check(double number); // returns index of element containg "number" or -1 if none void addNumber(double); // adds number to the array void removeNumber(double); // deletes the number from the array ...
choices: supporting function default constructor friend function static member class getter setter attribute assignment operator Identify the parts of the following class definition: class Student { string name; const int ID; float gpa; const char gender; public: Student(); Student& operator (const Student&); string getName() const {return name; } friend ostream& operator<<(ostream&, const Student); }; void outputStudent (const Student&); name [Choose Student) [Choose operator Choose) outputStudent Choose operator<< Choose) getNamel) [Choose