SJSU CMPE 126 S17 –Midterm 1 Exam
Assume there is a stock class which has symbol (string), cost (int) and shares (int) as public members.
Write the minimum declaration that supports both statements
stock s2(“APPL”, 209, 77), s3(“FB”, 100, 1); // 77 shares of APPL at $209, 1 share of FB at $100
stock s4(s3);
Write member operator overload function declaration that supports
int value=s2+s3; // value is the sum of (cost*shares)
Given the stock class has the following declaration:
friend ostream& operator>> (istream&, stock&);
Answer each of the following questions in less than 10 words.
Can this function be implemented as a “member function”? If yes, how? If no, why?
Can this function be implemented without the “friend” declaration? If yes, how? If no, why?
If we create a stockDB class which stores variable number of stocks in a dynamic stock array. Complete the minimum stockDB.h class definition such that
stockDB frank(5), obama(700); // frank can have at most 5 stocks, obama 700 max
class stockDB {
(3pt) Write the implementation code (cpp) that supports frank = obama;
(3pt) Further assume that stock has operator< overloaded and there is a global swap(stock&, stock&) function. Complete the following implementation code for stockDB selection sort.
void stockDB::selectionSort() {
Add additional code below to cause a dangling pointer issue.
int * a = new int(5);
int * b = a;
Given the declaration int * d = new int[5]; Check ALL statements that prints out the address of the 2nd elements in d array
cout << d + 1;
cout << d + 4;
cout << &d[1];
cout << &d+4;
cout << d++; // this is a bit tricky
Is the following code correct? If yes, what’s the output? If no, what’s wrong?
int *p; //Line 1
int *q; //Line 2
p = new int; //Line 3
*p = 43; //Line 4
q = p; //Line 5
*q = 52; //Line 6
delete q; //Line 7
cout << *p << " " << *q << endl; //Line 8