1- Explain what the operator  Box& 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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Consider the following code. Answer questions 1 to 4. This is a walkthrough question

#include <iostream>

#include <cstring>

using namespace std;

const int NT = 10;

const int NC = 2;

 

class Chocolate {

    char type[NT + 1];

public:

    Chocolate(const char* t = nullptr) {

        if (t) {

            strncpy(type, t, NT);

            type[NT] = '\0';

        }

        else {

            type[0] = '\0';

        }

        cout << "C";

    }

    ~Chocolate() {

        cout << "~" << type << endl;

    }

    void display() const {

        cout << type << endl;

    }

};

 

class Box {

    Chocolate ch[NC];

    int nc;

public:

    Box() {

        nc = 0;

        cout << "B";

    }

    Box& operator+=(const Chocolate& c) {

        if (nc < NC) {

            ch[nc] = c;

            cout << "+";

            ch[nc++].display();

        }

    return *this;

    }

 

Box(const Box& b) {

nc = b.nc;

    for (int i = 0; i < nc; i++)

        ch[i] = b.ch[i];

cout << "E" << endl;

}

~Box() {

cout << "~B" << endl;

}

void display() const {

    for (int i = 0; i < nc; i++)

        ch[i].display();

cout << endl;

}

};

 

 

void show(const Box b) {

b.display();

}

 

int main() {

cout << "=Chocolate=" << endl;

    Chocolate cherry("cherry");

    Chocolate orange("orange");

 

cout << "\n=Box=" << endl;

    Box b;

 

cout << "\n=++=" << endl;

b += orange;

b += cherry;

 

cout << "\n=show=" << endl;

show(b);

 

cout << "\n=done=" << endl;

    return 0;

}

 

1- Explain what the operator  Box& 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.

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY