Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the posted file (toolBox.cpp) and make a new toolbox class (using the same T

ID: 3568675 • Letter: U

Question

Use the posted file (toolBox.cpp) and make a new toolbox class (using the same Tool class). In the new class, the data member of type Tool in this new toolbox class will be a pointer. There must be two new constructors in this new toolbox class. The default constructor will specify a specific number of tools, but the parameter constructor will have one int parameter used to specify a certain number of tools (size of array) which can be any positive number. Both constructors will use the "new" operator to allocate the array of Tool from dynamic memory. You can, if you choose, define a destructor that will use the "delete" operator to deallocate the dynamic memory. Much of ToolBox2 class can be reused with little or no change. The display function, must have a significant, but small change.

#include <iostream>
#include <string>
using namespace std;

class Tool
{
public:
string getType()
{
return type;
}
int getQuant()
{
return quantity;
}
void set(string s, int q)
{
type = s;
quantity = q;
}
private:
string type;
int quantity;
};

class ToolBox1
{
public:
void setFirst(string s, int q)
{
first.set(s, q);
}
void setSecond(string s, int q)
{
second.set(s, q);
}
void setThird(string s, int q)
{
third.set(s, q);
}
void display()
{
cout << "the tools are "
<< first.getType() << ": " << first.getQuant() << endl
<< second.getType() << ": " << second.getQuant() << endl
<< third.getType() << ": " << third.getQuant() << endl;
}
private:
Tool first, second, third;
};

class ToolBox2
{
public:
void set(int index, string s, int q)
{
t[index].set(s, q);
}
void display()
{
cout << "the tools are ";
for (int i=0; i<3; i++)
cout << t[i].getType() << ": " << t[i].getQuant() << endl;
}
private:
Tool t[3];
};

int main()
{
ToolBox1 tb1;
tb1.setFirst("screwdrivers", 8);
tb1.setSecond("pliers", 5);
tb1.setThird("wrenches", 20);
tb1.display();
ToolBox2 tb2;
tb2.set(0, "screwdrivers", 8);
tb2.set(1, "pliers", 5);
tb2.set(2, "wrenches", 20);
tb2.display();
return 0;
}

Explanation / Answer

#include <iostream>
using namespace.std;
class Tool
{
public:
string getType()
{
    return type;
}
int getQuant()
{
    return quantity;
}
void set(string s, int q)
{
    type = s;
    quantity = q;
}
private:
string type;
int quantity;
friend class Toolboxnew;
};
class ToolBoxnew
{
const specificno_tools = <user_defined>;
int no_tools;
Tool * newtools;
public:

ToolBoxnew()
{
string s;
int q;
no_tools = specifino_tools;
newtools = new tool[specificno_tools];
for(int i = 0; i < no_tools; i++)
{cout << "enter tool type and quantity :" << endl;
cin >> s >> q;
newtools[i]->set(s ,q);
}
}

ToolBoxnew(int m ) : no_tools(m)
{
newtools = new tool[no_tools];
for(int i = 0; i < no_tools; i++)
{
cout << "enter tool type and quantity :" << endl;
cin >> s >> q;
newtools[i]->set(s ,q);
}
}

~ToolBoxnew()
{
delete [] newtools;
newtools = null;
}

void display()
{
cout << "the tools are "
for(int i = 0; i < no_tools; i++)
{
cout << newtools[i]->gettype()<< ":" << newtools[i]->getquantity << endl;
}
}
};
int main()
{
int m = <user_defined>;
ToolBoxnew tb1(m);
tb1.display();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote