- Details
- Parent Category: Programming Assignments' Solutions
We Helped With This C++ Programming Assignment: Have A Similar One?
Short Assignment Requirements
Assignment Description
Assignment 1 – BCS 370 Data Structures
Use good style. Make sure that you properly separate code into .h/.cpp files. Make sure that you add preprocessor guards to the .h files to allow multiple #includes.
Overview
You will be writing the classes Stock and Portfolio. You will be writing unit testing code for both classes.
Create a C++ static library in Visual Studio to store the Stock and Portfolio classes. This library should contain the Stock.h, Stock.cpp, Portfolio.h, and Portfolio.cpp files.
Part 1 – Create a Stock Class
Write a class named Stock.
Stock Class Specifications
1. Include member variables for name (string), price (double), shares (double).
2. Write a default constructor.
3. Write a constructor that takes values for all member variables as parameters.
4. Write a copy constructor.
5. Implement Get/Set methods for all member variables.
6. Implement the CalculateValue function. This function should multiply the prices by the shares and return that value. Use the following function header: double CalculateValue().
7. Add a member overload for the assignment operator.
8. Add a non-member operator<< overload. Prints the values of all member variables on the given ostream.
Stock Class Updates
The Stock class should implement all the specifications from the first assignment plus the updates and features listed below.
1. Change all member variables to pointers. You will need to update code in any functions that use these member variables. Do NOT change any function signatures for this class. All functions should operate the same as before from the user of the classes’ perspective. For example, assume the get/set functions for title have the following signatures:
std::string GetName();
void SetName(std::string n);
These signatures should remain exactly the same. The same goes for any other functions that use these member variables. Only the internal implementation of the functions will change to accommodate the use of pointers.
2. Update the constructors. The constructors should allocate memory for the pointer member variables.
3. Add a destructor. The destructor should deallocate memory for the pointer member variables.
4. Update operator= and copy constructor. The operator= and copy constructor should be updated to perform deep copies.
5. Update operator<<. Make sure it prints out the values and not the addresses.
6. Add a non-member operator>> overload. The >> operator is used for input. Reads the values of all member variables from the given istream.
IMPORTANT: You can assume that a one word string is being used for name in order to make it a little easier to code. This is important because if you did not make that assumption you could not use the >> operator to read a string value. The >> operator only reads up until the first whitespace it encounters.
Part 2 – Create a Portfolio Class
Write a class that will store a collection of Stock. This class will be used to keep track of data for multiple Stock class instances. You MUST implement ALL of the specifications below.
Portfolio Class Specifications
1. Create a private member variable that is a static array of Stock. The size of the array can be whatever you want it to be.
2. Your class must implement all of the following functions (use the given function prototypes):
a. void Set(int index, Stock s) – Sets the value at the given index to the given Stock instance. You should test the index to make sure that it is valid. If the index is not valid then do not set the value.
b. Stock Get(int index) – Return the Stock located at the given index in the array.
c. int PriceRangeCount(double lowerBound, double upperBound) – Returns the count of the number of Stocks that fall within the given range. For example, assume the following number of Stock prices: 10, 20, 15, 25, 30, 40
If lowerBound is 20 and upperBound is 30 then the returned value should be 3. Any values that fall on the boundaries should be included in the count. In this example we are getting a count of the number of stocks that have a price between $20 and $30. Remember, this function is using price and not value.
d. Stock MostShares() – Returns the Stock in the Portfolio that has the most shares.
e. bool FindByName(string name, Stock &v) – Returns true if the Stock with the given name is in the array and false otherwise. If the Stock is in the array you should copy it into the Stock reference parameter.
f. double TotalValue() – Returns the sum of all Stock values (not prices) in the collection.
g. int Size() – Returns the size of the array.
h. void Initialize() – Initializes all of the elements of the array to reasonable default values.
i. string GetAuthor() – Returns your name. Just hard code your name into the function.
3. Create a default constructor that will initialize all elements of the array to default values.
Portfolio Class Updates
The Portfolio class should implement all the specifications from the first assignment plus the updates and features listed below.
1. Dynamic array. Change the internal implementation of the array so that the array is dynamically allocated.
2. Add a size member variable to the class. This member variable should ALWAYS contain the number of elements in the array (size of the array). Some functions may cause the size of the array to change so make sure that this member variable is updated to reflect the new size.
3. Update all the necessary code in the class so that it is usable with a dynamic array. One example of this is to change the ending condition of loops that visit all elements of the array. The ending limit should not be hard coded. They should use the new size variable as the ending condition.
4. Add a one parameter constructor that takes a size. This constructor should dynamically allocate an array of the given size. It should also set the size member variable to reflect the size.
5. Add a copy constructor. This function should make a deep copy of the passed in instance.
6. Add a destructor. This function should perform any necessary cleanup.
7. Add a member overload of operator= (assignment operator). This method should perform a deep copy of the passed in instance. After this function ends the size of the current instance’s array should be the same as the other instance’s array and all the data from the other instance should be copied into the current instance’s array.
Hint: C++ arrays have a fixed size. You may need to delete and then reallocate memory for the current instance’s array in order to make the current instance’s array the same size as the other instance’s array. Be careful for memory leaks.
8. Add a non-member operator<< overload. Prints the values of all elements of the array on the given ostream.
9. Add a Resize function. Here is the function signature:
void Resize(int newSize);
This function should create a new array that has the passed in size. You MUST retain any values that were previously in the array. The new array size can be larger or smaller. If the new array size is SMALLER just retain as many elements from the previous array that can fit.
Hint: C++ arrays have a fixed size. You may need to delete and then reallocate memory. Be careful for memory leaks.
10. Add a function named Clone with the following signature:
Portfolio *Clone();
This method should allocate a new dynamic instance of Portfolio that is a deep copy of the current instance. This method should return a pointer to the new instance.
Overview
You will be writing an unsorted list of Stock with a linked implementation. You will also be writing a console application that tests the unsorted list you wrote. You must add the proper imports to the test console application.
Create the StockList Class in the Stock Static Library
Write the StockList class. Just add it to the Stock static library. You will only need the Stock static library (with StockList added to it) and the new console application that will contain the testing code.
StockList Class Specifications
1. Use a linked-list implementation for the StockList class.
2. Create a StockListNode struct to use in your implementation. This class definition should be defined as private inside the StockList class.
3. Implement the following constructors:
a. StockList()
Default constructor. Creates an empty list.
b. StockList(const StockList& otherList)
Creates a list that is a DEEP COPY of otherList.
4. Implement a destructor that performs the necessary cleanup.
a. ~StockList()
Clears the list and releases dynamically allocated memory.
5. Add a member overload of the assignment operator with the following function prototype:
StockList & operator=(StockList& rhs)
This method should make a deep copy of the list parameter. Any data in the current instance should be removed before adding the new elements.
6. Implement the following methods:
a. void Clear()
Clears the current list. Remember, when items are removed you MUST release memory.
b. int Length() const
Returns the number of items in the current list.
**Important: The runtime of this operation MUST be coded such that it is O(1). **
c. void Add(const Stock s)
Adds a Stock to the list.
d. void Add(const StockList& otherList)
Adds all elements from otherList on to the current list (appends). Any data that exists in this instance before this function call should still be in the list after the function call completes. It does not matter where you add the elements. The otherList object should not change after this method is run. This should perform a DEEP COPY of the elements from the other list.
Here is sample call (assume a and b are StockList type objects):
a.Add(b); // All elements of b will be added to a
e. bool FindByName(std::string name, Stock &result) const
Finds the Stock with the given name. If the Stock is found the result parameter should be populated with data from the target Stock. If found the function return value should be true otherwise return false.
f. void Delete(std::string name)
Deletes the Stock that matches the passed in name. Remember, when items are removed you MUST release memory.
g. Add a non-member operator<< overload. Prints the values of all elements of the list on the given ostream. Here is the method signature:
std::ostream &operator<<(std::ostream& os, StockList& rhs);
Here is a sample use:
StockList sl;
// Add Stock data to sl here…
std::string filename("StockData.txt");
std::ofstream outfile(filename);
outfile << sl;
h. Add a non-member operator>> overload. Loads the values of all elements of the list from the given istream. Here is the method signature:
std::istream &operator>>(std::istream& is, StockList& rhs);
Here is sample code that uses this overload:
StockList sl;
std::string filename("StockData.txt");
std::ifstream infile(filename);
infile >> sl;
Hint: You might want to use the >> overload on the Stock class to make it easier to write this function.
IMPORTANT: You can assume that one word strings are being used for title and rating in the input file in order to make it a little easier to code. This is important because if you did not make that assumption you could not use the >> operator to read in the string values. The >> operator only reads up until the first whitespace it encounters.
7. Big-O Runtime Costs.
For each StockList method you must include method header comments as detailed in the “Commenting Guidelines” document on Blackboard. Each method header comment must include the Big-O runtime cost of that particular method. You will lose points if you leave out any of the big-O runtime costs from the method header comments or if they are incorrect.
Console Application - Main Function
Create a C++ console application that imports and uses the static library solution that you created. The console application should have a main function. In main you should create instances of the StockList class and demonstrate that ALL functions work properly. You can write unit testing code if you want but you are not required to.
StockList File Format
Name
Price
Shares
Name
Price
Shares
…
Name
Price
Shares
Sample StockList File (there is no descriptive text, just the data, leave spaces out of the title to make it easier to read in)
Amazon
59.99
10
Microsoft
74.59
20