Assume there is a Stock class which has symbol (string), cost (int) and shares (
ID: 3812123 • Letter: A
Question
Assume there is a Stock class which has symbol (string), cost (int) and shares (int) as private members. There is also a StockNode class which inherits Stock class. Further assume there are classes for storing collection of Stock as follows:
DBfwd.. stores stocks in a forward linked list
DBdlink.. stores stocks in a doubly-linked list
DBstack .. stores stocks in a stack
1.DBfwd uses StockNode. Write the minimum StockNode definition for this purpose. (hint: StockNode is a Stock)
2. Write insert_back member function for DBfwd assuming first is the only data members of the list.
void DBfwd::insert_back(StockNode s) {
3.Write the minimum DBdlink.h (just data members, no need to write function members)
class DBdlink {
public:
4. Continue with your minimum DBdlink.h, write delete_back member function for DBdlink which returns last node in parameter.
void DBdlink::delete_back(StockNode& last) {
5. Assume DBstack is implemented with a dynamic array, write the minimum and complete DBstack.h with push and pop functions.
class DBstack {
public:
6. The postfix notation representation of a math expression 2 * ( 3 - 4 ) / ( 5 + 6 )
Its postfix notation representation is ___________________________________
(use the space below as your scratchpad so we know the answer is genuine.)
7. What are the two must-have conditions for a recursive algorithm?
_________________________________________
__________________________________________
8. Write a recursive reverse_print function for DBfwd assuming first is the only data members of the list. You can also assume Stock has operator << overload and StockNode is a Stock.
void DBfwd::reverse_print() {
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Stock
{
public:
string sym;
int noShares;
double shareVal;
double totVal;
Stock();
Stock(string s, int n, double v);
void setSym(string s);
void buy(int n, double p);
void update(double p);
string getSym();
double getVal();
int getnoShares();
void print();
Stock operator++();
Stock operator++(int);
Stock operator--();
Stock operator--(int);
private:
void setTot();
};
void Stock::setSym(string s)
{ s = sym;}
string Stock::getSym()
{return sym;}
double Stock::getVal()
{return shareVal;}
int Stock::getnoShares()
{return noShares;}
Stock::Stock(string s, int n, double v)
{
s = sym;
n = noShares;
v = shareVal;
setTot();
}
void Stock::buy(int n, double p)
{
cout << "Enter the Symbol of the Stock you would like to buy: " ;
cin >> sym;
cout << "How many shares of the stock would you like to buy?: ";
cin>> noShares;
cout << "What is the current price of the stock?: ";
cin >> shareVal;
n = noShares;
p = shareVal;
}
void Stock::sell(int n, double p)
{
cout << "Enter the Symbol of the Stock you would like to sell: " ;
cin >> sym;
cout << "How many shares of the stock would you like to sell?: ";
cin>> noShares;
cout << "What is the current price of the stock?: ";
cin >> shareVal;
n = noShares;
p = shareVal;
}
void Stock::update(double p)
{
p = shareVal;
}
void Stock::print()
{
cout << "SYMBOL: " << getSym();
cout << "Number of Shares: " << getnoShares();
cout << "Current Price of Share: " << getVal();
cout << "Total Value of Holding: " << totVal;
}
void Stock::setTot()
{
totVal = noShares * shareVal;
}
Stock Stock::operator++()
{
++noShares;
setTot();
}
Stock Stock::operator--()
{
--noShares;
setTot();
}
int main()
{
return 0;