C++ Problem:
I need two modules. Module names are Basket.h and Basket.cpp.
The tester program which is also the main.cpp is alreadyprovided. The code should run in matrix and Visual studio withoutany errros. Thank you.
Please beware of the memory leaks. Thanks
DIY (50%)Basket Module
Design and code a class named Basket that holds informationabout a fruit basket. Place your class definition in a header filenamed Basket.h and your function definitions in an implementationfile named Basket.cpp.
Include in your solution all of the statements necessary foryour code to compile under a standard C++ compiler and within thesdds namespace.
Add to this module a custom type called Fruit:
struct Fruit { char m_name[30 + 1]; // the name of thefruit double m_qty; // quantity inkilograms };
Basket Class
Design and code a class named Basket that holds informationabout a fruit basket.
Basket Private Members
The class should be able to store the following data:
You can add any other private members in the class, as requiredby your design.
Basket Public Members
Friend Helper Functions
overload the insertion operator (operator<<) to insertinto the stream (received as the first parameter) the content of anobject of type Basket (received as the second parameter).
If the basket doesn't contain any fruit, print the message Thebasket is empty!<ENDL>.
If the basket contains fruits, print the content in theformat:
The fruit names should be printed on fields of size 10, alignedto the right; the fruit quantities and basket price should beprinted with two significant digits.
//Tester Program(main.cpp)
#include<iostream>#include<cstring>#include"Basket.h"#include"Basket.h" //intentional
using namespace std;using namespace sdds;
void printHeader(const char* title){char oldFill = cout.fill('-');cout.width(40);cout << "" << endl;
cout << "|> " << title << endl;
cout.fill('-');cout.width(40);cout << "" << endl;cout.fill(oldFill);}
int main(){sdds::Fruit fruits[]{ {"apple", 0.65}, {"banana", 1.25}, {"pear", 0.50}, {"mango", 0.75}, {"plum", 2.00},};
{ printHeader("T1: Default Constructor");
Basket aBasket; cout << aBasket;
// conversion to bool operator if (aBasket) cout << "Test failed: the basket should beempty!\n"; else cout << "Test succeeded: operator said the basket isempty!\n";
cout << endl;}
{ printHeader("T2: Custom Constructor");
Basket aBasket(fruits, 2, 6.99); cout << aBasket;
// conversion to bool operator if (aBasket) cout << "Test succeeded: operator said the basket hascontent!\n"; else cout << "Test failed: the basket should NOT beempty!\n";
cout << endl;}
{ printHeader("T3: += operator");
Basket aBasket; aBasket += fruits[2]; (aBasket += fruits[0]) += fruits[4]; aBasket.setPrice(12.234);
cout << aBasket; cout << endl;}{ printHeader("T4: Copy Constructor");
Basket b1; Basket b2(b1);
cout << "Basket #1 -> " << b1; cout << "Basket #2 -> " << b2;
b1 += fruits[3]; b1.setPrice(3.50);
Basket b3(b1); cout << "Basket #3 -> " << b3; cout << endl;}
{ printHeader("T5: Copy Assignment");
Basket b1, b2, b3(fruits, 5, 19.95);
b1 = b2; cout << "Basket #1 -> " << b1; cout << "Basket #2 -> " << b2;
b1 = b3; cout << "Basket #1 -> " << b1;
b3 = b2; cout << "Basket #3 -> " << b3;}
return 0;}
//End of tester program(main.cpp)
Expected Output:
C++ Problem: I need two modules. Module names are Basket.h and Basket.cpp. The tester program which is also the main.cpp
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am