Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Answer all questions, C++ language. What is the difference between a friend function for a class and a member function for the class? (5 markah/
Answer all questions, C++ language.
What is the difference between a friend function for a class and a member function for the class? (5 markah/ marks) Suppose you wish to add a friend function for subtraction to the class Money defined in the code shown below. What do you need to add to the description of the class Money? The subtraction function should take two arguments of type Money and retum a value of type Money whose value is the value of the first argument minus the value of the second argument. Provide the complete definition of the subtraction function as well. class Money public: friend Money add (Money amount1, Money amount2); friend bool equal (Money anount1, Money amount2); Money (long dollars, int cents); Money(long dollars); Money f ) double get_value( ); void input (istreams ins); void output (ostreamb outs); private: long all_cents; What is output of the following codes? Give your reason. (4 markah / marks) Explain runtime polymorphism with an appropriate example. (6 markah / marks) Suppose your program contains the following codes: \begin{tabular}{|l|l|} \hline struct Box \\ i & string name; \\ & int number; \\ Box next; \end{tabular} What is the output produced by the following code? Box *head; head = new Box; head-> name = "Sal1y"; head->number =18; cout (*head) , name end ; cout head->name endl; cout (*head), number endl; cout head number endl; Suppose further that p1 points to a node of type Node that is on a linked list and is not the last node on the list. Write code that will delete the node affer the node pointed to by p1. After this code is executed, the linked list should be the same. except that there will be one less node on the linked list. (3 markah / marks) (c) i. What is wrong with the following defintion of headinsert? \begin{tabular}{|l} struct Node \\ int iten; \\ Node* link; \end{tabular} ii. Givon the following function declaration, complete the definition of function inso'rt. void insert.(Nodeptr afterMe, int num); //PhE: afterMe points to sowe node in the non-eapty list //POST: A new node containing num is inserted after afterMe. voud insert(NodePtr afterMe, int num) 1 II complete the definition 1 (4 markah / marks) (d) Consider the List. class with the following private members: head Class List: / public members here ... / private: struct Nodei ListdataType item; /f the data of the node Node* next; // points to the next node of the 1 ist I; Node* head; // point to first node in the list I: i. Draw a diagram of the above list after the following lines of code have been executed. \begin{tabular}{l} Node prev = head->next: \\ Node nodeToInsert = new Node: \\ nodeToInsert->iten =4z \\ nodeToinsert->next = prev->next; \\ prev->next = nodeTolnsert: \\ \hline (4 markah/marks) \end{tabular} II. Assume that the code represented above in part (i) has been executed. What is the value of prev-sitem? (2 markah/marks) III. In addition to the code above, assume the following code executes. Draw a diagram of the list after this code executes as welf. prev = prev->next; prev = prev->next; Node * curr = prev->next; prev->next = curr->next; delete curr; curr = NULL; (4 markah/marks) Question 3 ABC Enterprise needs to develop a simple program to store their customer details in the company portal. The details are customer's id, name and phone number. The program should be able to do the following: - Add and delete customer details - Print a complete list of customers - Determine who is calling given the customer's phone number (a) The company is given a choice to store the data in an array or a linked list. As a software engineer, propose a more suitable format of data storage (array or linked list) to the company considering the application described above and justify your answer. (3 markah / marks) (b) Assume that the customer details are stored in a structure as follows: \begin{tabular}{|l} struct Customer I \\ string name; \\ int tel; \\ int id; \\ Customer * next; \\ \hline \end{tabular} A class Portal is defined to allow the company's employee to input, modify and extract the customer details. class Portal i private: Customer "stackTop; public: Portal () ( stackTop = nullptr; , void print (); void add_entry (); void delete entry (); void searchbyPhone ( int ); li i. Write the definition of the member function add_entry where the function will allow the company employee to input the name and phone number of a customer. The customer ID will be generated randomly in the range of 001 to 999. The new customer entry should be added in the form of linked list according to the stack operation (last-in, first-out (LIFO)). ii. Imarks) Write the definition of the member function print to print a fist of customers in the linked list according to the following format. (3 markah / marks) The member function delete_entry allows the company employee to delete an entry of the customer details based on an input of the customer name. Develop an algorithm for the function delete_entry based on the partial codes shown below. Note: C++ codes are NOT required. Suggest a suitable hash function, based on the 8-digit phone number, so that the company can efficiently determine who is calling given the custamer's phone number. Explain why you choose this hash function. (2 markah / marks) Calculate the hash values for the first four customers inserted into the hash tabie, if their phone numbers are 12345678, 12345876, 12438675, and 13467852 respectively. (2 markah / marks) Customer details can also be stored in the company portal using a binary tree data structure where each node of the binary tree represents a customer. i. Assume that the following is the constructed binary tree of customer details, provide the three traversal sequences (pre-order, in-order, and post-order) of the binary tree. The number shown in each node is the customer id. (3 markah / marks) ii. Determine if the binary tree data structure provides an advantage in searching for customer's phone number as compared to the singly linked list (linear data structure) developed in part (b). Defend your answer. (2 markah / marks) 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