$10

SENECA 244200 OOP WORKSHOP WS06 DIY BASKET CLASS C++

0 ratings
Pay

SENECA 244200 OOP WORKSHOP WS06 DIY BASKET CLASS C++

$10
0 ratings

DIY (50%)

Basket Module

Design and code a class named Basket that holds information about a fruit basket. Place your class definition in a header file named Basket.h and your function definitions in an implementation file named Basket.cpp.

Include in your solution all of the statements necessary for your code to compile under a standard C++ compiler and within the sdds namespace.

Add to this module a custom type called Fruit:

struct Fruit
{
  char m_name[30 + 1]; // the name of the fruit
  double m_qty;        // quantity in kilograms
};

Basket Class

Design and code a class named Basket that holds information about a fruit basket.

Basket Private Members

The class should be able to store the following data:

  • a dynamically allocated array of objects of type Fruit. This is the resource that you must manage.
  • the size of the Furits array.
  • the price of the basket.

You can add any other private members in the class, as required by your design.

Basket Public Members

  • the default constructor
  • a custom constructor that receives as parameters an array of objects of type Fruit, the size of the array received in the fist parameter, and the price of the basket (in this order); stores the parameters into the attributes if all the parameters are valid (the numbers are greater than 0, and the array is not null).
  • the copy constructor (you must do a deep-copy for the resource, and a shallow copy for the other attributes)
  • the copy assignment operator (you must do a deep-copy for the resource, and a shallow copy for the other attributes; note that the copy constructor and copy assignment operator have almost identical logic -- reuse the code)
  • the destructor
  • void setPrice(double price): updates the price attribute to the value received as parameter
  • the conversion to bool operator: returns true if the basket contains any fruits, false otherwise.
  • an overload of the += operator that receives as a parameter an object of type Fruit (by value) and adds it to the dynamic Fruits array (You need to resize the array)

Friend Helper Functions

  • overload the insertion operator (operator<<) to insert into the stream (received as the first parameter) the content of an object of type Basket (received as the second parameter).If the basket doesn't contain any fruit, print the message The basket is empty!<ENDL>.If the basket contains fruits, print the content in the format:
    Basket Content:<ENDL>
    [FRUIT_1_NAME]: [FRUIT_1_QUANTITY]kg<ENDL>
    [FRUIT_2_NAME]: [FRUIT_2_QUANTITY]kg<ENDL>
    ...
    Price: [BASKET_PRICE]<ENDL>
    The fruit names should be printed on fields of size 10, aligned to the right; the fruit quantities and basket price should be printed with two significant digits.

Tester program:

/***********************************************************************
// OOP244 Workshop 6 p2:  Classes with Resources
//
// File  main.cpp
// Version 2.1
// Author  Nargis Khan
//
//
// Revision History
// -----------------------------------------------------------
// Name            Date            Reason
/////////////////////////////////////////////////////////////////
***********************************************************************/
#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.75},
      {"banana", 1.65},
      {"pear",   0.51},
      {"mango",  0.75},
      {"plum",   2.20},
   };

   {
      printHeader("T1: Default Constructor");

      Basket aBasket;
      cout << aBasket;

      // conversion to bool operator
      if(aBasket)
         cout << "Test failed: the basket should be empty!\n";
      else
         cout << "Test succeeded: operator said the basket is empty!\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 has content!\n";
      else
         cout << "Test failed: the basket should NOT be empty!\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;
}

Data Entry

No data entry is needed

Execution Sample

----------------------------------------
|> T1: Default Constructor
----------------------------------------
The basket is empty!
Test succeeded: operator said the basket is empty!

----------------------------------------
|> T2: Custom Constructor
----------------------------------------
Basket Content:
     apple: 0.75kg
    banana: 1.65kg
Price: 6.99
Test succeeded: operator said the basket has content!

----------------------------------------
|> T3: += operator
----------------------------------------
Basket Content:
      pear: 0.51kg
     apple: 0.75kg
      plum: 2.20kg
Price: 12.23

----------------------------------------
|> T4: Copy Constructor
----------------------------------------
Basket #1 -> The basket is empty!
Basket #2 -> The basket is empty!
Basket #3 -> Basket Content:
     mango: 0.75kg
Price: 3.50

----------------------------------------
|> T5: Copy Assignment
----------------------------------------
Basket #1 -> The basket is empty!
Basket #2 -> The basket is empty!
Basket #1 -> Basket Content:
     apple: 0.75kg
    banana: 1.65kg
      pear: 0.51kg
     mango: 0.75kg
      plum: 2.20kg
Price: 19.95
Basket #3 -> The basket is empty!

Reflection

Study your final solutions for each deliverable of the workshop, reread the related parts of the course notes, and make sure that you have understood the concepts covered by this workshop. This should take no less than 30 minutes of your time and the result is suggested to be at least 150 words in length.

Create a file named reflect.txt that contains your detailed description of the topics that you have learned in completing this workshop and mention any issues that caused you difficulty.

You may be asked to talk about your reflection (as a presentation) in class.

Part 2 Submission (DIY)

Files to submit:

reflect.txt and:

Item.cpp
Item.h
main.cpp

Data Entry

No data entry needed

Submission Process:

Upload your the files listed above to your matrix account. Compile and run your code using the g++ compiler as shown in Compiling and Testing Your Program and make sure that everything works properly.

Then, run the following command from your account

  • replace profname.proflastname with your professor’s Seneca userid
  • replace ?? with your subject code (200 or 244)
  • replace # with the workshop number
  • replace X with the workshop part number (1 or 2)
~profname.proflastname/submit 2??/w#/pX

and follow the instructions.

Important: Please note that a successful submission does not guarantee full credit for this workshop. If the professor is not satisfied with your implementation, your professor may ask you to resubmit. Re-submissions will attract a penalty.

Pay
Copy product URL