Question
#include #include usingnamespacestd; constintNT =10; constintNC =2; classChocolate { chartype[NT +1]; public: Chocolate(constchar* t =nullptr) { if(t) { strncpy(type, t, NT); type[NT] ='0'; } else{
#include
#include
usingnamespacestd;
constintNT =10;
constintNC =2;
classChocolate {
chartype[NT +1];
public:
Chocolate(constchar* t =nullptr) {
if(t) {
strncpy(type, t, NT);
type[NT] ='\0';
}
else{
type[0] ='\0';
}
cout <<"C";
}
~Chocolate() {
cout <<"~"<< type << endl;
}
voiddisplay()const{
cout << type << endl;
}
};
classBox {
Chocolate ch[NC];
intnc;
public:
Box() {
nc =0;
cout <<"B";
}
Box&operator+=(constChocolate& c) {
if(nc < NC) {
ch[nc] = c;
cout <<"+";
ch[nc++].display();
}
return*this;
}
Box(constBox& b) {
nc = b.nc;
for(inti =0; i < nc; i++)
ch[i] = b.ch[i];
cout <<"E"<< endl;
}
~Box() {
cout <<"~B"<< endl;
}
voiddisplay()const{
for(inti =0; i < nc; i++)
ch[i].display();
cout << endl;
}
};
voidshow(constBox b) {
b.display();
}
intmain() {
cout <<"=Chocolate="<< endl;
Chocolate cherry("cherry");
Chocolate orange("orange");
cout <<" =Box="<< endl;
Box b;
cout <<" =++="<< endl;
b += orange;
b += cherry;
cout <<" =show="<< endl;
show(b);
cout <<" =done="<< endl;
return0;
}
1- Explain what the operatorBox& operator+=(const Chocolate& c) does.
2- How can you improve the performance the show() function in this program.
Explain the effect of your upgrade in one or two sentences.
3- Explain in one or two sentences the meaning of the keyword const in the display( )
function.
4- Describe in one or two sentences the effect of removing the default parameter
value in the definition of the Chocolate( ) constructor.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started