THE CODE FOR THE ABOVE QUESTION IS :
#include <vector>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
template <typename T>
class ArrayBag
{
private:
vector<T> bag;
public:
// constructor
ArrayBag(vector<T> arrayBag)
{
this->bag = arrayBag;
}// end constructor
// remove an item randomly
void RemoveRandom(int index_to_remove)
{
T removed =
bag.at(index_to_remove);
bag.erase((bag.begin() +
index_to_remove));
cout << "Removed Element: "
<< removed << endl;
}// end RemoveRandom
// function to print the vector
void printBag()
{
int size =
this->bag.size();
for(int i = 0; i < size ;
i++)
cout <<
this->bag.at(i) << " ";
cout << endl;
}// end printBag
};
int main()
{
int n = 5;
// creating vectors
vector<int> v1 = {1,2,3,4,5};
vector<float> v2 = {1.1,2.2,3.3,4.4,5.5};
vector<string> v3 =
{"Item1","Item2","Item3","Item4","Item5"};
// creating array bag objects and initialising using
constructor
ArrayBag<int> a1(v1);
ArrayBag<float> a2(v2);
ArrayBag<string> a3(v3);
srand(time(0));
// testing out prototype
cout << "ArrayBag 1: " ;
a1.printBag();
cout << "Removing an item randomly. " ;
a1.RemoveRandom(rand() % n);
cout << "ArrayBag 1: " ;
a1.printBag();
cout<<endl;
cout << "ArrayBag 2: " ;
a2.printBag();
cout << "Removing an item randomly. " ;
a2.RemoveRandom(rand() % n);
cout << "ArrayBag 2: " ;
a2.printBag();
cout<<endl;
cout << "ArrayBag 3: " ;
a3.printBag();
cout << "Removing an item randomly. " ;
a3.RemoveRandom(rand() % n);
cout << "ArrayBag 3: " ;
a3.printBag();
return 0;
}
SAMPLE OUTPUT:
Add a member function template called RemoveRandom to the ArrayBag class (so add it to ArrayBag.h...
This homework involves ADDING MEMBER FUNCTIONS to ArrayBag.h. This homework is important because we do some thing like this on exams. All you will need to submit is your member function code BUT your code should compile and run if I were to add it to ArrayBag.h. That means you should put your code in ArrayBag.h and make sure it compiles before submitting. You should test with a simple main. DO NOT submit main or your ArrayBag.h file just submit...
you will write a templated array class. Called MyArray with member variables ptr and array_size. This array must use dynamic memory--see program shell, names should remain unchanged. The following is a list of required member functions of the array class along with a rubric: (1pts) program compiles (2pts) default constructor (2pts) parametered constructor which feeds MyArray a size. (10pts) copy constructor 8pts for performing a *deep* copy 2pts for correct syntax (10pts) overloaded = operator 7pts for functioning properly 3pts...
Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower named Rose. The Rose class has one member variable name. Add a constructor which expects color and name. Pass color to the base constructor and set name to it's member variable.Write a program that has an array of ...
Create a class called Flower. Add one member variables Color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower named Rose. The Rose class has one member variable name. Add a constructor which expects color and name. Pass color to the base constructor and set name to it's member variable.Write a program that has an array of...
in C++ creat a DynamicQueue class, add a new data member called count to trace the total number of node you have in current queue (you need to modify some member functions for adding count). Add a member function called displayQueue() to display values stored in each node in the current queue, also the total number of nodes in the queue. You also need to have a driver program (refer to Tester) to test your modified new class and new...
Write a class called Book that has two private member variables called page (integer) and topic (string). It also has a static private member variable called count (integer). This class has only one constructor with default argument for page and topic. Write this constructor. Also overload the addition operator for this class such that it will add an integer value to the page variable of the book. For example when in main we say: book2 = book1 + 4; then...
in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159*radius*radius. Add a default constructor to the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign its value to the radius...
CODE IN C++Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius. Add a default constructor the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign radius...
Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...
C++ / Visual Studio Implement the member function below that returns true if the given value is in the container, and false if not. Your code should use iterators so it works with any type of container and data. template< typename Value, class Container > bool member( const Value& val, const Container& cont ); just implement this function in the code below #include <iostream> #include <array> #include <deque> #include <list> #include <set> #include <string> #include <vector> using namespace std; #define...