#include using namespace std; template class MyArray { private:    BT *array;    int size;    int curr_size; public:    MyArray(int x) : array(nullptr), size(0){        if(x <= 1){            cout<<"Can't construct array size"<= curr_size){    cout << "exiting";    exit(0);    }    return array[arr_ind];    }    ~MyArray()    {        delete[] array;    } };   Output from main function above: 1 2 3 4 5 6 7 8 9 10

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter13: Overloading And Templates
Section: Chapter Questions
Problem 12PE
icon
Related questions
Question

#include<iostream>
using namespace std;
template<typename BT>
class MyArray
{
private:
   BT *array;
   int size;
   int curr_size;
public:
   MyArray(int x) : array(nullptr), size(0){
       if(x <= 1){
           cout<<"Can't construct array size"<<x<<endl;
           exit(EXIT_FAILURE);
       }
       size = x;
       curr_size=0;
       array = new BT[x];
   }
   MyArray(const MyArray &object) : array(new BT[object.size]), size(object.size){
       copy(object.array, object.array + object.size, array);
   }
   MyArray& operator=(const MyArray &object){
       BT *new_arr = new BT[object.size];
       copy(object.array, object.array + object.size, new_arr);
       delete[] array;
       array = new_arr;
       size = object.size;
       return *this;
   }
   void gr(){
       BT *temp_arr = new BT[size*2];
       for(int i=0;i<size;i++){
           temp_arr[i] = array[i];
       }
       array = temp_arr;
       size*=2;
   }

   void add(BT data){
       if(curr_size<size){
           array[curr_size] = data;
           curr_size++;
       }else{
           gr();
           array[curr_size] = data;
           curr_size++;
       }
   }
   int getSize(){
       return curr_size;
   }
   void output(){
       for(int i=0;i<curr_size;i++){
           cout<<array[i]<<" ";
       }
       cout<<endl;
   }
   int & operator[](int arr_ind)
   {
   if (arr_ind >= curr_size){
   cout << "exiting";
   exit(0);
   }
   return array[arr_ind];
   }
   ~MyArray()
   {
       delete[] array;
   }
};

 

Output from main function above:
1 2 3 4 5 6 7 8 9 10

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.
Test using following main function.
int main() {
cout <« endl;
MyArray<SomeObj> a(1);
Someobj o1(1);
SomeObj o2(2);
Someobj o3(3);
Someobj o4(4);
Someobj o5(5);
SomeObj o6(6);
Someobj o7(7);
SomeObj o8(8);
Someobj 09(9);
Someobj o10(10);
a.add(01);
a.add(02);
a.add(03);
a.add(04);
a.add(05);
a.add(06);
a.add(07);
a.add(08);
a.add(09);
a.add(010);
a.output();
cout <« endl;
return 0;
}
Transcribed Image Text: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. Test using following main function. int main() { cout <« endl; MyArray<SomeObj> a(1); Someobj o1(1); SomeObj o2(2); Someobj o3(3); Someobj o4(4); Someobj o5(5); SomeObj o6(6); Someobj o7(7); SomeObj o8(8); Someobj 09(9); Someobj o10(10); a.add(01); a.add(02); a.add(03); a.add(04); a.add(05); a.add(06); a.add(07); a.add(08); a.add(09); a.add(010); a.output(); cout <« endl; return 0; }
Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage