Question

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. 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(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            delete(arr);
           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                arr[i] = Array.arr[i];
           }
       }
      
       MyArray & operator=(const MyArray& Array){
           MyArray A(Array.capacity);
          
           for(int i=0;i                A.arr[i] = Array.arr[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 arr1(5);
  
   for(int i=0;i<20;i++){
       arr1.add(i+20);
   }
  
   for(int i=0;i<20;i++){
       cout<    }
   cout<<"\n\n";
   MyArray arr2(10);
  
   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            delete(arr);
           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                arr[i] = Array.arr[i];
           }
       }
      
       MyArray & operator=(const MyArray& Array){
           MyArray A(Array.capacity);
          
           for(int i=0;i                A.arr[i] = Array.arr[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 arr1(5);
  
   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;
}

Add a comment
Know the answer?
Add Answer to:
Implement the following in c++ (use "iostream" and "nsmespace std" please.)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Implement the following: a. A template class named MyArray. 1) MyArray is a dynamic partially...

    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)....

    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...

    #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];...

  • Equals and Copy method activity Implement a Dog class with the following features: + Attributes: ...

    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...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    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...

    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.

  • #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode...

    #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode * left; TNode * right; TNode * parent; }; class BTree { public: //constructors and destructor BTree(); BTree(TNode *r);// initialize BTree with the root r. r is the only node. BTree(const BTree & t); // copy constructor BTree(const int *p, const int n);// similar to the copy constructor, but your input is of the array form. // input is given an array that denotes...

  • In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following...

    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...

  • Add a member function template called RemoveRandom to the ArrayBag class (so add it to ArrayBag.h...

    Add a member function template called RemoveRandom to the ArrayBag class (so add it to ArrayBag.h). The RemoveRandom member function should remove a random entry from the list (so use your srand and rand functions). Do not forget to TEST... that means you will need to add the prototype and test with a simple main. All you need to submit though is the member function implementation Add an overloaded constructor to the ArrayBag class (so add it to 2. ArrayBag.h)....

  • you will write a templated array class. Called MyArray with member variables ptr and array_size. This...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT