Question
Objectives: 1. Implement friend function of a class; 2. Overload the < < as a friend function of a class with chaining; 3. Overload the
Objectives: 1. Implement friend function of a class;
2. Overload the << as a friend function of a class with chaining;
3. Overload the operator as a member function of a class with chaining; This assignment is an extension of Unit 10 Programming Assignment. You will modify your implementation of call_class. The class will still manage a dynamic array of call records. Call this program "call_stats7.cpp". I have provide the driver call_stats7cpp to help you implement this program. Your input data will be in the file callstats_data.txt. The descriptions of the functions you will implement are as follows:
1. (you implemented this in the previous program) the default constructor to initialize the state of your class. The default constructor will read the data from the file callstats_data.txt into the dynamic array call_DB. If call_DB becomes full, the function should call the function double_size to double the size (capacity) of call_DB. Remember, count and call_DB are private members of your class and do not need to be passed to an member function of your class.
2. (you implemented this in the previous program) is_empty is a Boolean public member function of the class. It has no formal parameter because count is a member of the state of the class (private member) and does not need to be passed to it because the state of the class if known to all member functions of the class. If count == 0 then true is returned; otherwise false is returned.
3. (you implemented this in the previous program) is_full is a Boolean public member function of the class. It has no formal parameters because count and size are members of the state of the class (private members) and do not need to be passed to it because the state of the class if known to all member functions of the class. If count == size then true is return; otherwise false. The size is the capacity which is the total number of cells allocated to call_DB.
4. (you implemented this in the previous program) search is an integer public member function that has only one formal parameter, the key. key is the cell phone number for the record you are search for. The array of records, call_DB and count are members of the state of the class and do not need to be passed to a member function of the class; The function will return the location of key in call_DB if it is there; otherwise -1 is returned.
5. (you implemented this in the previous program) add is a void public member function that inserts the information for a call record into call_DB. Duplicates cell numbers are ok; add will prompt the user for the firstname, lastname, cell number, relays and call length. You may call process record to re-process when you add a new record. add has no formal parameters.
6. overload operator - as a member function of call_class with chaining. This function will have the same functionality as the remove function. Recall the following about the function remove: remove is a void public member function thaqt deletes all records with the cell number stored in key. If duplicate records exist with the same cell number they must all be deleted. remove has only one formal parameter, the key. Note, because we are overloading with chaining we must return the current object *this.
7. (you implemented this in the previous program) double_size is a void public member function that doubles the capacity of call_DB. double_size has no formal parameters because size, count and call_DB are all members of the state of the class, call_class. First, size is multipled by two; second, memory is allocated using call_record *temp=new call_record[size]; third the records in call_DB are copied into temp with the statement temp[i]=call_DB[i] using a for loop. Forth, the old memory for call_DB is de-allocated using delete [ ] call_DB; Finally, call_DB is set to point to the new memory pointed to by temp using call_DB = temp.
8. (you implemented this in the previous program) process is a void public member function the has no formal parameter because call_DB and count are members of the state of the class.. The function process will calculate the net cost of a call (net_cost), the tax on a call (call_tax) and the total cost of the call (total_cost) using the number of relay stations (relays) and the length in minutes of the call (call_length) for all call records stored in call_DB. Please consider the following: a. The tax rate on a call (call_tax) is simply based on the number of relay stations (relays) used to make the call (0<= relays <=5 then call_tax = 1%; 6<= relays <=11 then call_tax = 3%; 12<= relays<=20 then call_tax = 5%; 21<= relays <=50 then call_tax = 8%; relays >50 then call_tax =12%) . b. The net cost of a call is calculated by the following formula: net_cost = ( relays / 50 x 0.40 x call_length). c. The tax on a call is equal to net_cost x call_tax / 100. d. The total cost of a call (rounded to the nearest hundredth) is calculated by the following formula: total_cost = net_cost + call_tax . All tax and cost calculations should be rounded to the nearest hundredths.
9. overload operator << as a friend function of call_class with chaining. This function will have the same functionality as the print function ( it will print call_DB to the screen). Recall the following about the function print: print is a void public member function that has no formal parameters because count and call_DB are members of the state of the class. The function will print every field of every call_record in call_DB to the screen.
10.the destructor to de-allocate all memory allocated to call_DB. This function has no formal parameters because call_DB is a member of the state of the class. It will be called automatically by the compiler. Use the driver call_stats7.cpp to help you implement this program. Output Format for the overload operator<<: Consider the following sample output table when designing and implementing "operator<<". See section Format of Output below. (The output is in the following order: firstname, lastname, cell phone number, relays, minutes, net cost, tax rate, call tax, total call cost)
Jean Hayward 9546321555 0 0 0 0.01 0 0
Marlon Brando 5612971340 5 50 2 0.01 0.02 2.02
John Kennedy 3051234567 8 25 1.6 0.03 0.05 1.65
Input Stream: In the assignment you will declare one ifstream to bind your input to the file "callstats_data.txt" Whenever a program performs file i/o you must include the "fstream" library. Format of the input data file(input filename is "callstats_data.txt"): Do not include column titles (The order of the columns are as follows: firstname, lastname, cell phone number, relays, minutes)
Jean Hayward 9546321555 0 0
Marlon Brando 5612971340 5 50
John Kennedy 3051234567 8 25
Hillary Clinton 7542346622 24 17
George Bush 3054432762 15 30
Barack Obama 9544321011 50 100
Donald Trump 8776219988 87 82
Bernie Sanders 9042224556 4 5
Harry Ford 7877176590 11 1
Michelle Obama 5617278899 20 45
Ann Dunham 9546321555 4 3
Vladimir Putin 5612971340 79 86
Harriet Tubman 3051234567 8 25
Oprah Winfrey 5611234444 24 118
Charlotte Ray 3054432762 115 25
Tina Turner 8776219988 265 22
Shirley Chisholm 9042224556 2 5
Maritza Correla 7877176590 89 67
Margaret Thatcher 5617278899 40 56
Format of Output to screen: (the order of the columns is as follows: firstname, lastname, cell phone number, relays, minutes, net cost, tax rate, call tax, total call cost)
Jean Hayward 9546321555 0 0 0 0.01 0 0
Marlon Brando 5612971340 5 50 2 0.01 0.02 2.02
John Kennedy 3051234567 8 25 1.6 0.03 0.05 1.65
Hillary Clinton 7542346622 24 17 3.26 0.08 0.26 3.53
George Bush 3054432762 15 30 3.6 0.05 0.18 3.78
Barack Obama 9544321011 50 100 40 0.08 3.2 43.2
Donald Trump 8776219988 87 82 57.07 0.12 6.85 63.92
Bernie Sanders 9042224556 4 5 0.16 0.01 0 0.16
Harry Ford 7877176590 11 1 0.09 0.03 0 0.09
Michelle Obama 5617278899 20 45 7.2 0.05 0.36 7.56
Ann Dunham 9546321555 4 3 0.1 0.01 0 0.1
Vladimir Putin 5612971340 79 86 54.35 0.12 6.52 60.87
Harriet Tubman 3051234567 8 25 1.6 0.03 0.05 1.65
Oprah Winfrey 5611234444 24 118 22.66 0.08 1.81 24.47
Charlotte Ray 3054432762 115 25 23 0.12 2.76 25.76
Mary Carter 9544321011 43 10 3.44 0.08 0.28 3.72
Tina Turner 8776219988 265 22 46.64 0.12 5.6 52.24
Shirley Chisholm 9042224556 2 5 0.08 0.01 0 0.08
Maritza Correla 7877176590 89 67 47.7 0.12 5.72 53.43
Margaret Thatcher 5617278899 40 56 17.92 0.08 1.43 19.35
Handing in your program Electronically submit the source file "call_stats7.cpp" in the Assignments area of blackboard before the due date and time. Remember, to submit an assignment even if it is latePlease start early and let me know if you have any questions ASAP.
#include #include #include
using namespace std;
class call_record { public: string firstname; string lastname; string cell_number; int relays; int call_length; double net_cost; double tax_rate; double call_tax; double total_cost; };
class call_class { public: call_class(); ~call_class(); //de-allocates all memory allocate to call_DB by operator new. bool is_empty(); //inline implementation bool is_full();//inline implementation int search(const string key);//returns location if item in listl; otherwise return -1 void add( ); //adds a call record to call_DB call_class & operator-(const string key); //removes an item from the list void double_size(); void process(); friend ostream & operator<<(ostream & out_to_file, call_class & Org); //prints all the elements in the //list to the screen. private: int count; int size; call_record *call_DB; };
/************************************************************************************************************************************/ //Name: default constructor //Precondition: //Postcondition: //Decription: Reads the data file of call information (cell number, relays and call length) into the dynamic array of call record, //call_DB. If the count because equal to the size the function double_size is called and the memory allocated to call_DB is doubled. /************************************************************************************************************************************/ call_class::call_class() {
}
/***********************************************************************************************************************************/ //Name: is_empty //Precondition: //Postcondition: //Decription: returns true if call_DB is empty /**********************************************************************************************************************************/ bool call_class::is_empty() { return count == 0; }
/**********************************************************************************************************************************/ //Name: is_full //Precondition: //Postcondition: //Decription: returns true if call_DB is full /*********************************************************************************************************************************/ bool call_class::is_full() { return count == size; }
/**********************************************************************************************************************************/ //Name: search //Precondition: //Postcondition: //Decription: locates key in call_DB if it is there; otherwise -1 is returned /*********************************************************************************************************************************/ int call_class::search(const string key) { return -1; }
/*********************************************************************************************************************************/ //Name: add //Precondition: //Postcondition: //Decription: adds the informaton for a call record to call_DB; if call_DB is full, double_size is called to increase the size of call_DB. /********************************************************************************************************************************/ void call_class::add( ) { }
/********************************************************************************************************************************/ //Name: operator- //Precondition: //Postcondition: //Decription: remove key from call_DB if it is there. /*******************************************************************************************************************************/ call_class & call_class::operator-(const string key) { return *this; }
/******************************************************************************************************************************/ //Name: double_size //Precondition: //Postcondition: //Decription: doubles the size (capacity) of call_DB /******************************************************************************************************************************/ void call_class::double_size( ) { size *=2; call_record *temp = new call_record[size];
for(int i=0; i { temp[i] = call_DB[i]; }
delete [ ] call_DB; call_DB = temp; }
/******************************************************************************************************************************/ //Name: process //Precondition: //Postcondition: //Decription: calculate the net cost, tax rate, call tax and total cost for every call record in call_DB. /*****************************************************************************************************************************/ void call_class::process() { }
/****************************************************************************************************************************/ //Name: operator<< //Precondition: //Postcondition: //Decription: Overloading operator<< as a friend function. Prints every field of every call_record in call_DB // formatted to the screen and a file called "stats7_output.txt". /***************************************************************************************************************************/ ostream & operator<<(ostream & out, call_class & Org) { for(int i=0; i { out<
//Put code to OPEN and CLOSE an ofstream and print to the file "stats7_output.txt".
return out; //must have this statement }
/****************************************************************************************************************************/ //Name: destructor //Precondition: //Postcondition: //Decription: de-allocates all memory allocated to call_DB. This should be the last function to be called before the program // is exited. /***************************************************************************************************************************/ call_class::~call_class() { }
//driver to test the functionality of your class. int main() { cout<<"TEST1: Testing the Default Constructor, is_full and double_size "; call_class MyClass; cout<<"Finish TEST1: testing the default constructor ";
cout<<"Test2: Testing add, double_size, process, and is_full() "; //MyClass.add(); cout<<"Finish TEST2 ";
cout<<"Test3: Testing operator-, serach, and is_empty "; //MyClass - "5617278899" - "9546321555" - "1234567890"; cout<<"Finish TEST3 ";
cout<<"Test4: Testing operator<< "; cout< cout<<"Fist TEST4 ";
cout<<"The destructor will be called automatically "; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started