Given the following class:

```cpp
class GiftBox {
char* labelName;
double cubeVolume;
double weight;
};
```

Answer the following questions:

1. Create a default no-argument constructor to set `cubeVolume` and `weight` to 0, and `labelName` to an empty string (start with null character '\0').

2. Create a three-argument constructor to set `cubeVolume`, `weight`, and `labelName` to the given argument values. Validate first if the double values are positive and the label name is not `nullptr` and not an empty string '\0' before using it, otherwise do nothing.

3. Define a destructor to deallocate the memory pointed by `labelName`, and print "object of type GiftBox just destroyed."

4. Implement a function called `display` that takes an `ostream` (`std::ostream& display() const;`) to display the information as shown in the sample output.

5. Implement two member operators `+= double` (adds the double value to the weight) and `+= object of the same type` (add the `cubeVolume` and `weight` to the current one; no change for the label).

6. Implement a helper operator `<<` that uses `display` implemented in part 4.

Use the following template for your answer (i.e., copy this text and insert your answers within it):

```cpp
// Name:
// ID:

// Email:

class GiftBox {
char* labelName;
double cubeVolume;
double weight;

// add all declarations that can be added here
// (the fifth one is provided to you)

void display(std::ostream& os) const;
};

// implement your functions here

// Sample tester for your code.
// NO NEED TO HAVE EXACT OUTPUT BASED ON THIS CODE.
// THIS IS TO GIVE YOU AN IDEA OF THE EXPECTED OUTPUT FOR EACH QUESTION ABOVE
int main() {
GiftBox g1, g2(4000.8, 200.2, "Sweet Box"), g3(2000, 100.2, "Box!");

cout << g1;
g2 += 100;
cout << g2;
g2 += g3;
cout << g2;
}

Output:

No data is available in this object.

Sweet Box
==========
cubeVolume=4000.8Cm3 and weight=300.2Grm

Sweet Box
==========
cubeVolume=4000.8Cm3 and weight=400.2Grm

Sweet Box
==========
cubeVolume=6000.8Cm3 and weight=500.4Grm

object of type GiftBox just destroyed

object of type GiftBox just destroyed

object of type GiftBox just destroyed
```

Answer :

Final answer:

To implement the given requirements, you need to create a class named GiftBox with constructors, destructor, display function, and member operators. Follow the steps outlined in the detailed answer to implement each requirement. Make sure to validate the input values and handle any potential errors or edge cases. Test your code using the provided sample tester code to verify its correctness.

Explanation:

To implement the given requirements, you need to have a good understanding of constructors, destructors, and member operators in C++. Constructors are special member functions that are used to initialize objects of a class. They are called automatically when an object is created. Destructors, on the other hand, are used to clean up resources allocated by the object before it is destroyed. Member operators, such as +=, are used to define how objects of a class should behave when certain operations are performed on them.

In this case, you need to implement the += operator for both double and object of the same type. To implement these requirements, you will need to use the concepts of function overloading, memory management, and operator overloading in C++. Make sure to validate the input values and handle any potential errors or edge cases.

Here is an outline of the steps you can follow to implement the given requirements:

Make sure to test your code using the provided sample tester code. The output may not match exactly, but it should give you an idea of the expected output for each question.

Learn more about implementing constructors, destructor, and member operators in a class here:

https://brainly.com/question/30024932

#SPJ14

Other Questions