Implement the following: a. A template class named MyArray. 1) MyArray is a dynamic partially filled array for primitive types. 2) data members: - a pointer for the array - any associated variables needed to manage the array. 3) Constructor must insure that specified capacity is possible. Exit the program if an illegal value is specified. 4) “The Big Three” are required to insure deep copy. 5) Private grow function is used to automatically increase the size of the array when adding elements. 6) Add function to safely append elements to the array. 7) getSize function that returns the current number of elements. 8) Overloaded the [] operator to read and update existing elements. 9) In main: a) Instantiate an integer MyArray of size 5. b) Add 20 elements. (tests add and grow functions) c) Output this array. d) Instantiate a character MyArray of size 10. e) Add all 26 letters of the alphabet to the array. f) Output this array. 2. Copy the previous program to a new file and implement the following: 1) A class SomeObj with a single integer id as a data member. 2) This class must have a default and user defined constructor. 3) Create an output function to display the id of the object. 4) Modify the MyArray class to create an array of SomeObj objects. 5) In main: a) Add 10 SomeObj objects to the array. b) Output this array.
(1)
#include
using namespace std;
template
class MyArray{
private:
T* arr;
int size , capacity;
void grow(){
capacity =
capacity*2;
T*a = new
T[capacity];
for(int
i=0;i
arr = a;
}
public:
MyArray(int n){
if(n<=0)exit(1);
capacity =
n;
arr = new
T[n];
size = 0;
}
~MyArray(){
delete []
arr;
}
MyArray(const MyArray&
Array){
capacity =
Array.capacity;
size =
Array.size;
arr = new
T[capacity];
for(int
i=0;i
}
}
MyArray & operator=(const
MyArray& Array){
MyArray
A(Array.capacity);
for(int
i=0;i
}
return A;
}
void add(T val){
if(size ==
capacity)grow();
arr[size++] =
val;
}
int getSize(){
return
size;
}
T & operator[] (int
index){
return
arr[index];
}
};
int main(){
MyArray
for(int i=0;i<20;i++){
arr1.add(i+20);
}
for(int i=0;i<20;i++){
cout<
cout<<"\n\n";
MyArray
for(int i=0;i<26;i++){
char ch = i+'A';
arr2.add(ch);
}
for(int i=0;i<26;i++){
cout<
return 0;
}
(2)
#include
using namespace std;
template
class MyArray{
private:
T* arr;
int size , capacity;
void grow(){
capacity =
capacity*2;
T*a = new
T[capacity];
for(int
i=0;i
arr = a;
}
public:
MyArray(int n){
if(n<=0)exit(1);
capacity =
n;
arr = new
T[n];
size = 0;
}
~MyArray(){
delete []
arr;
}
MyArray(const MyArray&
Array){
capacity =
Array.capacity;
size =
Array.size;
arr = new
T[capacity];
for(int
i=0;i
}
}
MyArray & operator=(const
MyArray& Array){
MyArray
A(Array.capacity);
for(int
i=0;i
}
return A;
}
void add(T val){
if(size ==
capacity)grow();
arr[size++] =
val;
}
int getSize(){
return
size;
}
T & operator[] (int
index){
return
arr[index];
}
};
class SomeObj{
private:
int id;
public:
SomeObj(){
id = 0;
}
SomeObj(int x){
id = x;
}
void print(){
cout<
};
int main(){
MyArray
for(int i=0;i<10;i++){
SomeObj ob(i+5);
arr1.add(ob);
}
for(int i=0;i<10;i++){
arr1[i].print();
}
return 0;
}
Implement the following in c++ (use "iostream" and "nsmespace std" please.)
1. Implement the following: a. A template class named MyArray. 1) MyArray is a dynamic partially filled array for primitive types. 2) data members: - a pointer for the array - any associated variables needed to manage the array. 3) Constructor must insure that specified capacity is possible. Exit the program if an illegal value is specified. 4) The Big Three" are required to insure deep copy.
In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...
#include <iostream> using namespace std; template <typename Item> class MyArray{ private: Item *myarray; int size; int used; void doubleSize(); public: MyArray(); ~MyArray(); int length(); void insertHead(Item i); void insertTail(Item i); void deleteHead(); void deleteTail(); void sortAscending(); void sortDescending(); Item operator [](int i){ return myarray[i]; } }; template <typename Item> MyArray<Item>::MyArray(){ size = 5; used = 0; myarray = new Item[size];...
Java
Equals and Copy method activity Implement a Dog class with the following features: + Attributes: age, gender, weight + Method: constructor, copy constructor, equals (if same weight and gender), toString + A main method/driver that create an array of 5 Dog objects (obtain input from user), and then display the objects that are "equal"
Equals and Copy method activity Implement a Dog class with the following features: + Attributes: age, gender, weight + Method: constructor, copy constructor, equals (if...
In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the term and creates the polynomial c x^n. (This constructor can be given default arguments easily to have a...
Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...
In c++ language
Design and implement a Queue data structure using linked list. Support the following usual operations: default constructor parameterized constructor to create a queue of user-specified capacity enqueue dequeue is_full is_empty display destructor that deallocates all the nodes copy constructor overloaded assignment operator Demonstrate using a main function.
This is code for c++. Any help would be great. #include <iostream> using namespace std; /* 1. Create a method that prints your name */ /* 2. Create an overloaded method that takes an integer and prints it out multiplied * by itself * Print out the argument inside the method and then call the method from main * and print it out * What was the typed of passing used inhere ? */ /* 3. Create an overloaded method...
use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void setSize() { cout << "Enter an integer larger than 2: "; cin >> size; } int getSize() { return size; } void fibTerms(int x) // Write a recursive function { } explicit FibObj() // Default constructor was made explicit to avoid possible implicit type conversion { setSize(); int *arr; arr = new int[getSize()]; arr[0] = 1; arr[1] = 1; // Call recursive function here...
In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following members: A pointer to hold a dynamically allocated array, of type int. A member variable to hold the size of the array. A default constructor, which will allocate an array of size 10 A parameterized constructor, which takes a size and use the size to allocate array. A copy constructor, which performs deep copy. A copy assignment operator, which performs deep copy and supports...