C++ 代写|编程代写

CS 202 – Assignment #6

Why we need Operator Overloading in C++? With the use of operator overloading, we can create operators that can work for our user-defined classes. You can redefine the majority of the C++ operators. We can give a special meaning to the existing operator, and we can redefine it to work with our user-defined class. However, the operator operations like +, -, *, and / work only on primitive data types (like int, double, char, etc.) by default. But for the user-defined data types to perform arithmetic, logic, relational, and boolean operations, there is a need for operator overloading. An example of operator overloading is that we can use ‘+’ to add two instance variables or objects of the same class, which is not possible by default.

Video Link:
https://unlv.zoom.us/rec/share/ter4QJkwJaRgUxTCSGMGzIKrsDi5VNkLPBxPt3smb8ub9dcJcK-axkYTuvBSwH1.GCkX53-vld-nGYWU?pwd=zfH9uNIOy_rtMi5jp8ziuzru7bRVCE_e

Assignment: In this assignment you will learn to use operator overloading both as member and non-member functions, friend functions, this pointer, copy constructor. This assignment demonstrates the usage of several classes such as COrder, CUser, CFirm, and CFile for managing the orders, users, and firms in a financial context. The header files for the above classes are provided and you have to implement each class. The COrder class has member variables like stockName, Price, sQuantity, totalPrice, user, and member functions, operator overloading function. COrder class allows you to set the above member variables for a user. The CUser class has a member variable UserName and a vector to store COrder objects (vector<COrder> Orders), member functions regarding orders, and operator overloading functions with member functions and non-member functions (friend functions). CUser class allows to add multiple orders of each user and store it to the vector of COrder that is declared in CUser class. The CFirm class has member variables like portfolioValue, vector to store CUser objects (vector<CUser> Users), member functions for portfolio calculations, and multiple operator overloading functions with member functions and non-member functions (friend functions).

The main() function serves as the entry point for the program. It begins by prompting the user to enter order details and specifying the number of orders to be processed. In main(), two vectors are declared to store dynamically allocated COrder objects, while another vector is designated to store CUser objects. A loop is used to create orders. For the first three orders, the symbol, price, quantity, and user are hardcoded. For orders beyond the third, the user is prompted to enter the order details manually. Each order is dynamically allocated and added to the orders vector. The corresponding user is found or created in the users vector based on the order's user name. The program uses pointers and dynamic allocation for the above operations. As a designer, it is your job to free up memory so that your program is free from memory leaks. However, in this assignment, you do not see pointer member variables in any particular class, so you do not see any difficulty with memory leaks. You can use

Valgrind to check the memory leaks.

COrder class: The COrder class stores information about a stock order, including the stock name, price, quantity, total price, and the user associated with the order. It provides functionality to initialize the order, retrieve the price and quantity, display the order details, and perform operator overloading for addition, friend functions to overload insertion and extraction operators.

Member Variables Descriptions: -string stockName: A variable to store the stock name -double Price: A variable to store the price of each stock -int sQuantity: A variable to store the quantity of each stock -double totalPrice: A variable to store the total price of each stock type (totalPrice = Price * sQuantity) -string User: A variable to store the user name that is associated with the order

Member Functions Descriptions: COrder(); The default constructor initializes the COrder object with default values:
COrder(const std::string& sName, double price, int quantity, const std::string& user); This parameterized constructor initializes the COrder object with the provided values. In other words, this is to to initialize the object’s attributes with the provided parameters accordingly. The other member variable/attribute “totalPrice” must be initialized in this constructor. Here, the attribute totalPrice is the product of price and quantity. (totalPrice = price * quantity) double getPrice() const; This accessor function return the Price of the stock. int getQuantity() const; This accessor function return the sQuantity of the stock. void orderDisplay(); This function prints the stock name, price, quantity, and total price of the order to the console. COrder operator+();

This is unary operator overloading, which means only one operand. This operator overloading function adds 10% to the price and increases the stock quantity by 10 for the order object. You can declare local variables for price and quantity and make changes as described above. The attribute totalPrice is the product of local variables price and quantity. It returns a new COrder object (use the COrder parameterized constructor in the return statement with updated values) with the above updated values. Note: This operator overloading reflects only the COrder object and this change doesn’t reflect the CFirm object. Reflecting CFirm object requires additional functions and this is not the scope of this assignment.
friend std::istream& operator>>(std::istream& is, COrder& order); This is a friend function declaration for overloading the >> operator. This friend function allows overloading the >> operator to read and assign values to a COrder object from an input stream. The input format is: <stock name> <price> <quantity> <user> Finally, return the input stream after reading and assigning values to the COrder object. Check the sample output file for more details. friend std::ostream& operator<<(std::ostream& os, COrder& order); This is a friend function declaration for overloading the << operator. This friend function allows overloading the << operator to output the COrder object's information to an output stream. Finally, return The output stream after writing the COrder object's information. This function prints the order details like stock name, price, quantity, total price, and user. Check the sample output file for more details.
CUser class: The CUser class represents a user and maintains a collection of orders associated with that user. It provides functionality to initialize a user, retrieve the orders (getters), and add new orders.

Member Variables Descriptions: +string UserName: A variable to store the user name +vector<COrder> Orders: The purpose of this member variable in the CUser class is to store and maintain a collection of orders associated with a user.

Member Functions Descriptions: CUser(); This default constructor initializes a CUser object with an empty UserName. CUser(const std::string& userName); This parameterized constructor initializes a CUser object with the provided UserName. const std::vector<COrder>& getOrders() const; This accessor function gets the orders associated with the user. This function returns a constant reference to the vector of COrder objects representing the orders associated with the user. This function returns a constant reference to the vector of COrder objects representing the orders associated with the user.void AddOrder(const COrder& order); This function is to add an order to the user's collection of orders. This function adds the provided COrder object to the user's collection of orders. The parameter order is to be added to the COrder object (Note: A user can have multiple orders).

CFirm class: The CFirm class provides functionality for managing a firm's portfolio, performing operations on firm objects, and overloading operators for combining, comparing, and modifying firm objects. It can be used to create and manage a collection of users and their associated orders within a firm.

Member Variables Descriptions: + double portfolioValue: A variable to store the current portfolio value of the firm. +vector<CUser> Users: The purpose of this member variable (vector) in the CFirm class is to store the users associated with the firm.

Member Functions Descriptions: CFirm(); This default constructor initializes the portfolioValue to 0.0. void calculatePortfolio(); This function calculates the portfolio value by iterating over each user and their orders, and summing the total value of all orders. double getPortfolio() const; This accessor function returns the portfolio value of the firm.
CFirm(const CFirm& other); // Copy Constructor Copy constructor that creates a new CFirm object by copying the users from another CFirm object. CFirm operator+(const CFirm& other)const; // Binary operator Overloaded operator that combines two CFirm objects by appending the users of the second object to the first object. (Example: CFirm combinedFirm = firm1 + firm2;)

[Hint: Copy the current CFirm object to any CFirm temp object; The function then enters a loop that iterates over each CUser object in the other.Users collection. This collection represents the users from the other CFirm object that will be merged into the temp object; Within the loop, each CUser object from other.Users is added to the temp.Users collection using the push_back method. This effectively appends the user to the end of the temp.Users collection; After iterating through all the users in other.Users and appending them to temp.Users, the temp object now contains the merged user data from both CFirm objects. Finally, the temp object, which represents the merged CFirm object, is returned from the function. CFirm operator+()const;//Unary operator; Deep copy of the Users vectorUnary operator that creates a deep copy of the CFirm object by copying the Users vector. (Example: CFirm firmCopy = +firm1; To test this: cout<<firm1.Users.size(); and cout<<firmCopy.Users.size(); expected output is 2 users for both) bool operator>(const CFirm& other) const; Overloaded operator that compares two CFirm objects based on the number of orders of their first user. This function returns true if object 1 is > object 2. (Example: if (firm1 > firm2)) bool operator<(const CFirm& other) const; Overloaded operator that compares two CFirm objects based on the number of orders of their first user. This function returns true if object 1 is < object 2. (Example: if (firm1 < firm2)) CFirm& operator++();//Pre-increment operator Overloaded pre-increment operator that adds a new CUser object to the Users vector of the CFirm object.CFirm operator++(int); // Post-increment operator Overloaded post-increment operator that adds a new CUser object to the Users vector of the CFirm object and returns a copy of the original object. CFirm& operator=(const CFirm& other); // Assignment operator Overloaded assignment operator that copies the users of another CFirm object to the current object. friend std::ostream& operator<<(std::ostream& os, const CFirm& firm); Friend function that overloads the << operator to output the firm information, including the names of its users.
CFile class: The CFile class provides static member functions to perform file-related operations for reading and writing firm data. These functions are designed to work with the CFirm class, allowing the reading and writing of firm information to/from files.

Member Functions Descriptions: static void WriteToFirmFile(const CFirm& firm, const std::string& fileName); This function is to write firm data to a file. This static member function writes the information of a CFirm object to a file specified by the fileName parameter. The firm data, including the Users collection and portfolio value, is written to the file in a specific format. Check the sample output for format details. It iterates over each CUser in the firm.Users collection and writes their order details to the file. The function calculates the portfolio value for each user and writes it at the end of the user's order details. [Hint: use 2 for loops; a nested loop; 1st for loop is CUser, write the username to output file and then start second for loop for the orders of that user; then write all the details like stock name, price, quantity, total price, portfolio. You can declare local variables as essential. A user can have more than one order. So write every order detail of a user to the output file.] static void ReadAndDisplayFirmFile(const std::string& fileName); This function read and display firm data from a file. This static member function reads the firm data from a file specified by the fileName parameter and displays it. The file is expected to contain firm information, including the Users collection and portfolio value, in a specific format. After reading the data, the function displays the firm information on the standard output stream. Check the sample output for format details.