Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1 Create a Stock Class Write a class named Stock. Stock Class Specifications Include member variables for name (string), price (double), shares (double). Write

Part 1 Create a Stock Class

Write a class named Stock.

Stock Class Specifications

Include member variables for name (string), price (double), shares (double).

Write a default constructor.

Write a constructor that takes values for all member variables as parameters.

Write a copy constructor.

Implement Get/Set methods for all member variables.

Implement the CalculateValue function. This function should multiply the prices by the shares and return that value. Use the following function header: double CalculateValue().

Add a member overload for the assignment operator.

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.

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.

Update the constructors. The constructors should allocate memory for the pointer member variables.

Add a destructor. The destructor should deallocate memory for the pointer member variables.

Update operator= and copy constructor. The operator= and copy constructor should be updated to perform deep copies.

Update operator<<. Make sure it prints out the values and not the addresses.

Add a non-member operator>> overload. The >> operator is used for input. Reads the values of all member variables from the given istream.

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

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.

Your class must implement all of the following functions (use the given function prototypes):

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.

Stock Get(int index) Return the Stock located at the given index in the array.

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.

Stock MostShares() Returns the Stock in the Portfolio that has the most shares.

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.

double TotalValue() Returns the sum of all Stock values (not prices) in the collection.

int Size() Returns the size of the array.

void Initialize() Initializes all of the elements of the array to reasonable default values.

string GetAuthor() Returns your name. Just hard code your name into the function.

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.

Dynamic array. Change the internal implementation of the array so that the array is dynamically allocated.

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.

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.

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.

Add a copy constructor. This function should make a deep copy of the passed in instance.

Add a destructor. This function should perform any necessary cleanup.

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 instances array should be the same as the other instances array and all the data from the other instance should be copied into the current instances array.

Hint: C++ arrays have a fixed size. You may need to delete and then reallocate memory for the current instances array in order to make the current instances array the same size as the other instances array. Be careful for memory leaks.

Add a non-member operator<< overload. Prints the values of all elements of the array on the given ostream.

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.

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.

Part 4 Main Function

Main should create instances of Stock and Portfolio and contain an automated unit test for both of them.

Automated Test

Part 2 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 updated Portfolio class and demonstrate that ALL functions work properly. You can write unit testing code if you want but you are not required to.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago