Determine the cost of the twoday package represented by the


Question 1:

Define a class RewardCard as an ADT that uses separate files for the interface and the implementation. The class represents a card on which members can earn points, which are in turn converted to vouchers. This class has four member variables:

• name, a string that holds the name of the member
• id, a string containing 6 digits to identify the member uniquely
• store, a string representing the store for which this card is valid
• points, an int representing the number of points that the member has earned.

The class should contain a default constructor that initializes name and store to an empty strings, id to a string containing six 0's and points to 0. It should also contain an overloaded constructor that initialises a new member's name, ID and the store and sets points to 0. The destructor should not perform any action.

Include accessor functions that return the values stored in each of an object of class RewardCard's member variables respectively (i.e. get functions), as well as mutator functions to update each of the member variables of an object of class RewardCard respectively (i.e. set functions with a parameter to set each member variable to a value specified by the parameter).

Overload the equality operator == as a friend function for class RewardCard. This function returns true if the name, id and store member variables of card1 is identical to those of card2 and false otherwise. Use the following prototype:

bool operator==(const RewardCard & card1, const RewardCard & card2)

Also overload the + and - operators for class RewardCard as friend functions. Operator + is used when a person uses the reward card to purchase goods. Operator + should add the values of the points member variables of two RewardCard objects if the name, id and store member variables of the two RewardCard objects are identical. Operator + should return an object of type RewardCard with the same values for the name, id and store member variables as the two operands. Use the equality operator == for RewardCard and the assert macro to check that the operands for the overloaded + operator are identical.

Similarly, operator - is used to determine the difference between points when a person returns goods that had earned points with the reward card. Operator - should subtract the values of the points member variables of two RewardCard objects if the name, id and store member variables of the two RewardCard objects are identical. Operator - should return an object of type RewardCard with the same values for the name, id and store member variables as the two operands. Again, use the equality operator == for RewardCard and the assert macro to check that the operands for the overloaded - operator are identical.

Also define an overloaded prefix operator ++ (implemented as a friend function) to add bonus points to the card. Operator ++ should add 100 points to the points member variable of the RewardCard object.

Overload the stream extraction operator >> and the stream insertion operator << as friend functions for class RewardCard. The stream insertion operator << should print a voucher for the object. The voucher should display the name, ID, store and an amount for which it can be traded at the relevant store, or a message in the case of insufficient points earned. Once a card has more than 500 points, the points can be converted to an amount for which it can be traded at the relevant store. 100 points is equal to R1.00. If a card has less than 500 points, the voucher will display the message "Sorry, this is not enough points yet for a voucher! Keep on buying". Use the following prototype: ostream& operator << (ostream& outs, const RewardCard & RC)

Test your class with the following program:
#include
#include
#include "Reward.h"
using namespace std;

int main() {
RewardCard ACard;
RewardCard MyCard("Hansie", "111111", "OurStore", 100);
cout << "This is ACard: " << ACard << endl;
cout << "This is MyCard: "<< MyCard << endl;

//Setting ACard to a new value
ACard.setName("Hansie");
ACard.setId("111111");
ACard.setStore("OurStore");
ACard.setPoints(0);
if (MyCard == ACard)
cout << "These cards are the same" << endl;
else cout << "These cards are not the same" << endl;

//Giving Hansie 200 bonus points
++MyCard;
++MyCard;
cout << "After giving Hansie 200 bonus points:" << endl;
cout << MyCard << endl;

//Hansie has bought goods which earned 350 points
cout << "Enter details for purchase of goods with points value of "
<< "350 points:" << endl;
cin >> ACard;
MyCard = MyCard + ACard;
cout << "Result after earning 350 points:" << endl << MyCard
<< endl;

//Hansie returned goods which has earned 100 points - this must now be
//deducted
cout << "Enter details for return of goods with points value of "
<< "100 points:" << endl;
cin >> ACard;
MyCard = MyCard - ACard;
cout << "Result after deducting 100 points:" << endl << MyCard
<< endl;

return 0;
}

Supply appropriate input values for the two input statements (cin >> ACard;) according to the comments in the program.

Enrichment exercise:

Overload the +, - and ++ operators for objects of class RewardCard in question 4, as member functions. Use the same program as in question 3 to test these member functions.

Question 2:

Define a class PhoneCall as an ADT that uses separate files for the interface and the implementation. This class represents a phone call and has three member variables:

• number, a string that holds the phone number (consisting of 10 digits) to which a call is placed

• length, an int representing the length of the call in minutes

• rate, a float representing the rate charged per minute.

In addition, the class should contain a default constructor that initializes number to an empty string, length to 0 and rate to 0. It should also contain an overloaded constructor that accepts a new phone number and sets length and rate both to 0, as well as a destructor that does not perform any action.

Include accessor functions that returns the values stored in each of an object of class PhoneCall's member variables respectively.

Class PhoneCall also contains a member function calcCharge() to determine the amount charged for the phone call. Use the following prototype:

float calcCharge();

Overload the equality operator== as a friend function to compare two phone calls. Use the following prototype:

bool operator==(const PhoneCall & call1, const PhoneCall & call2)

This function returns true if both call1 and call2 have been placed to the same number and false otherwise.

Overload the stream extraction operator >> (implemented as a friend function) so that it can be used to input values of type PhoneCall, and the stream insertion << (implemented as a friend function) so that it can be used to output values of type PhoneCall.

Demonstrate the class in an application program (main()) that is used to determine the total amount spend on phone calls to a specific phone number in one month. Allow the user to enter the phone number for which the total amount spent should be determined. Use the overloaded constructor to initialise the PhoneCall object theCall to the number the user specified. The PhoneCall objects representing the calls made during one month is stored in a file MyCalls.dat. Use a while loop to read the phone calls from MyCalls.dat, use the overloaded equality operator== to compare the phone numbers read from MyCalls.dat one by one with theCall, and determine the total amount spend on phone calls to theCall, as well as the number of calls made to this number. Also determine the longest call made to theCall and display this call together with the total amount spent on calls to theCall, and the number of calls to theCall.

Test your program with the following data: Phone calls in file MyCalls.dat:
0123452347 12 3.50
0337698210 9 3.15
0214672341 2 1.75
0337698210 15 3.15
0442389132 8 1.75
0232189726 5 3.50
0124395623 6 3.50
0337698210 2 3.15
0337698210 5 3.15

Phone number to test: 0337698210

Question 3:

Consider the following class:
class Package
{
public:
Package(double the_cost, double the_weight,
const string& the_sender, const string& the_receipient);
double calculate_cost()const;//multiply weight by cost_per_kilogram
string get_recipient() const;
string get_sender() const;
protected:
double cost_per_kilogram;
double weight;
private:
string sender;
string recipient;
};

(a) Implement class Package.

(b) Test class Package in a driver program that does the following:

• instantiates an object of class Package, with the following details:
sender: Charles Somerset
receiver: Anne Barnard
cost per kilogram: 12.75
weight: 1.25
• use the accessor functions to display the names of the sender and receiver of the instantiated object on the console.
• Use the member function calculate_cost() to determine the cost of the package represented by the instantiated object. Display the calculated cost also on the console.

(c) Derive and implement a new class TwoDayPackage that inherits the functionality of the class Package. TwoDayPackage should redefine calculate_cost(), where a fixed fee is added to the weight-based cost. For example, a TwoDayPackage that has a weight of 10kg, a cost of R3.00 per kilogram and charged a fixed fee of R5.00 would cost R35.00 (10*3.00 + 5.00) to deliver. The class should include a member variable to represent the fixed fee. TwoDayPackage has an additional member function Print(), which outputs the cost_per_kilogram, weight, sender, recipient and the total cost of delivery of the package.

(d) Test class TwoDayPackage in a driver program that does the following:

• instantiates an object of class TwoDayPackage, with the following details: sender: Charles Somerset
receiver: Anne Barnard
cost per kilogram: 12.75
weight: 1.25

• use the accessor functions to display the names of the sender and receiver of the instantiated object on the console.

• Use the member function calculate_cost() to determine the cost of the two_day_ package represented by the instantiated object, then display the calculated cost also on the console.

Solution Preview :

Prepared by a verified Expert
Programming Languages: Determine the cost of the twoday package represented by the
Reference No:- TGS01128320

Now Priced at $60 (50% Discount)

Recommended (98%)

Rated (4.3/5)