Help with c++ //This is same program that we developed in our classroom. //The c
ID: 3935138 • Letter: H
Question
Help with c++
//This is same program that we developed in our classroom.
//The class models a list with a capacity specified by the
//user at runtime.
//Implement the stated functions at line 35.
#include
using namespace std;
class MyList
{
private:
static const int DEF=10;
int* a;
int csz;
int cap;
//helper functions
int get_index(int) const;
public:
//Already implemented functions:
MyList();
MyList(int);
MyList(const MyList&);
~MyList();
bool add(int);
bool remove(int);
int size() const;
bool is_present(int) const;
void display() const;
void operator=(const MyList&);
//Implement the following:
//Points: 10
//This constructor will create a list of capacity cap with each element
//in the list initialized with the value initVal.
MyList(int cap, int initVal);
//Points: 10
//This function removes every occurrence of val from the list.
//Returns false if val is not in the list, true otherwise.
bool remove_all(int val);
//Points: 10
//This function swaps the contents of this list with that of other.
//The two lists may be of different size and/or capacity.
void swap(MyList& other);
//Points: 20
//This function changes the capacity of the list to newCap.
//If newCap is lesser than current cap, then the list shrinks
//and the items toward the end of the list are pruned to fit.
//If newCap is greater than the current cap, then the list needs
//to be moved to a bigger array.
//Returns false if newCap is an invalid capacity (e.g. newCap<=0),
//true otherwise.
bool change_capacity(int newCap);
};
void MyList::operator=(const MyList& other)
{
if (this==&other)
return;
cap=other.cap;
csz=other.csz;
delete[] a; //deallocates this object's array
a=new int[cap]; //create a new array with the same cap as that of other's array
for (int i=0;i a[i]=other.a[i];
}
MyList::MyList(const MyList& other)
{
cout << "copy constructor called. ";
cap=other.cap;
csz=other.csz;
a = new int[cap];
for (int i=0;i a[i]=other.a[i];
cout << "done. ";
}
MyList::MyList()
{
cap=DEF;
csz=0;
a=new int[cap];
}
MyList::MyList(int ucap)
{
if (ucap>0)
cap=ucap;
else
cap=DEF;
csz=0;
a=new int[cap];
}
MyList::~MyList()
{
cout << "A MyList object is being destroyed. " ;
delete[] a;
cout << "done. ";
}
bool MyList::add(int x)
{
if (csz==cap)
return false;
a[csz]=x;
csz++;
return true;
}
int MyList::size() const
{
return csz;
}
bool MyList::is_present(int x) const
{
for (int i=0;i if (a[i]==x)
return true;
return false;
}
void MyList::display() const
{
cout << "The list is of capacity: " << cap << endl;
cout << "The current size is: " << csz << endl;
cout << "Content is: ";
for (int i=0;i cout << a[i] << " ";
cout << endl;
}
bool MyList::remove(int x)
{
if (!is_present(x))
return false;
int index=get_index(x);
if (index==-1)
return false;
a[index]=a[csz-1];
csz--;
return true;
}
int MyList::get_index(int x) const
{
if (!is_present(x))
return -1;
for (int i=0;i if (a[i]==x)
return i;
return -1;
}
//Try the new functions you implemented here:
int main()
{
}
Explanation / Answer
#include <iostream>
using namespace std;
class MyList
{
private:
static const int DEF=10;
int* a;
int csz;
int cap;
//helper functions
int get_index(int) const;
public:
//Already implemented functions:
MyList();
MyList(int);
MyList(const MyList&);
~MyList();
bool add(int);
bool remove(int);
int size() const;
bool is_present(int) const;
void display() const;
void operator=(const MyList&);
//Implement the following:
//Points: 10
//This constructor will create a list of capacity cap with each element
//in the list initialized with the value initVal.
MyList(int cap, int initVal);
//Points: 10
//This function removes every occurrence of val from the list.
//Returns false if val is not in the list, true otherwise.
bool remove_all(int val);
//Points: 10
//This function swaps the contents of this list with that of other.
//The two lists may be of different size and/or capacity.
void swap(MyList& other);
//Points: 20
//This function changes the capacity of the list to newCap.
//If newCap is lesser than current cap, then the list shrinks
//and the items toward the end of the list are pruned to fit.
//If newCap is greater than the current cap, then the list needs
//to be moved to a bigger array.
//Returns false if newCap is an invalid capacity (e.g. newCap<=0),
//true otherwise.
bool change_capacity(int newCap);
};
void MyList::operator=(const MyList& other)
{
if (this==&other)
return;
cap=other.cap;
csz=other.csz;
delete[] a; //deallocates this object's array
a=new int[cap]; //create a new array with the same cap as that of other's array
for (int i=0;i<csz; ++i)
a[i]=other.a[i];
}
MyList::MyList(const MyList& other)
{
cout << "copy constructor called. ";
cap=other.cap;
csz=other.csz;
a = new int[cap];
for (int i=0;i<csz; ++i)
a[i]=other.a[i];
cout << "done. ";
}
MyList::MyList()
{
cap=DEF;
csz=0;
a=new int[cap];
}
MyList::MyList(int ucap)
{
if (ucap>0)
cap=ucap;
else
cap=DEF;
csz=0;
a=new int[cap];
}
MyList::~MyList()
{
cout << "A MyList object is being destroyed. " ;
delete[] a;
cout << "done. ";
}
bool MyList::add(int x)
{
if (csz==cap)
return false;
a[csz]=x;
csz++;
return true;
}
int MyList::size() const
{
return csz;
}
bool MyList::is_present(int x) const
{
for (int i=0;i<csz; ++i)
if (a[i]==x)
return true;
return false;
}
void MyList::display() const
{
cout << "The list is of capacity: " << cap << endl;
cout << "The current size is: " << csz << endl;
cout << "Content is: ";
for (int i=0;i<csz; ++i)
cout << a[i] << " ";
cout << endl;
}
bool MyList::remove(int x)
{
if (!is_present(x))
return false;
int index=get_index(x);
if (index==-1)
return false;
a[index]=a[csz-1];
csz--;
return true;
}
int MyList::get_index(int x) const
{
if (!is_present(x))
return -1;
for (int i=0;i<csz; ++i)
if (a[i]==x)
return i;
return -1;
}
//Try the new functions you implemented here:
MyList::MyList(int cap, int initVal){
if (cap>0)
this->cap=cap;
else
this->cap=DEF;
csz = 0;
a = new int[cap];
for(int i = 0; i < this->cap; ++i){
a[i] = initVal;
}
}
bool MyList::remove_all(int val){
if(!is_present(val))
return false;
else{
while(remove(val));
return true;
}
}
void MyList::swap(MyList& other){
int temp = other.csz;
other.csz = csz;
csz = temp;
temp = other.cap;
other.cap = cap;
cap = temp;
int *arr = other.a;
other.a = a;
a = arr;
}
bool MyList::change_capacity(int newCap){
if(newCap <= 0)
return false;
else{
int *newarr = new int[newCap];
int size = newCap < cap ? newCap : cap;
for(int i = 0; i < size; ++i){
newarr[i] = a[i];
}
delete[] a;
a = newarr;
cap = newCap;
csz = csz > newCap ? newCap : csz;
return true;
}
}
int main()
{
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.