Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume there is a stock class which has symbol (string), cost (int) and shares (

ID: 3795217 • Letter: A

Question

Assume there is a stock class which has symbol (string), cost (int) and shares (int) as private members.

1. Write the minimum declaration that supports both statements

stock s2(“APPL”, 209, 77), s3(“FB”); // 77 shares of APPL at $209, 1 share of FB at $100

stock s4(s3);

2. Write member operator overload function declaration that supports s2 += s3;

A. Can this function be implemented as a “member function”? If yes, how? If no, why?

B. Can this function be implemented without the “friend” declaration? If yes, how? If no, why?

C. Why does the function return ostream& ?

4. If we create a stockDB class which stores variable number of stocks in a 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 {

5. Given the following two array declarations: int a[25]; int *b = new int[25]; Write ONE LINE code that will copy all contents of a into b.=

6. In 10 words or less, what causes memory leak?

7. Complete the code fragment below.. suggestion: stringstream

string s=”(3, 45)”; // representing complex number 3+45i

char c; int real, imag; cout << real << “ + ” << imag << “i” << endl; // print out 3 + 45i

8. Given the declaration: int a[5] = {10,20,30,40,50}; // assume array starts at 2000. If the following code is valid, what is the output. If it is invalid, explain why.

a. cout << a+2; ___________________________

b. cout << *a+2; _________________________________

c. cout << *(a++); __________________________________

d. cout << a+5; ________________________________

Explanation / Answer

1.
Minimum declaration for statement
stock s2(“APPL”, 209, 77) is as follows

stock s2(string symbol, int cost, int shares)
{
   this->symbol=symbol;
   this->cost=cost;
   this->shares=shares;  
}

Minimum declaration for statement
s3(“FB”) is as follows

stock s2(string symbol)
{
   this->symbol=symbol;
   this->cost=100;
   this->shares=1;
}
Minimum declaration for statement
stock s4(s3); is as follows

stock(stock &other)
{
   this->symbol=other->symbol;
   this->cost=other->cost;
   this->shares=other->shares;
}


2)
Member operator overload function declaration that supports s2 += s3;
stock &stock::operator+=(stock const &other);

3)
A. Answer is no. why beacuse friend functions are called non-member functions
that allows to access the private data members of the class.
Specifically to overload the opreators ,insertion, extraction, +,-,/ and %.

B.
Answer is no.Since without using friend keyword, the function cannot able to access the private
data members to access.

C.
The return type ostream& that returns the reference of the ostream class that does
chaining of the data values and returns the reference.


4.
The class declartaion of stockDB.h

class stockDB
{
private :
int numStocks;

public:
stockDB(int numStocks);
};

5.

Given

//declaration of array of type integer of size,25
int a[25];
//declaration of array pointer of type integer of size,25
int *b = new int[25];

The single statment that copies all elements from the a to b
b=a;

The below statement is used to print all elements into b
for(int i=0; i<10; ++i)
{
cout << *b << endl;
++b;
}

6.
Memory leak:

Memory allocated on heap storage is created
but not released.The memory created previously must be released
before assigning them to new variables.

7.
string s=”(3, 45)”; // representing complex number 3+45i

char c; int real, imag;
cout << real << “ + ” << imag << “i” << endl;
// print out 3 + 45i

//Create an object of stringstream class.
stringstream ss;
//concatenate the real with strings
ss<< real << “ + ” << imag << “i” << endl;
//This below statement will print the contenet in ss object
cout<<ss<<endl;


8. int a[5] = {10,20,30,40,50};

//The statement a+2 prints the address of the second element in array,a
//a represents the starting address ,a+2 represents the address of element ,30  
cout << a+2<<endl;//result : hexa decimal number ex : 003BFA54 (your output may be different)
//*a represents the element at starting address of a, then add 2 then the result is 10+2=12
cout << *a+2<<endl; //result :12

//Invalid statement since the a represents the address, incrementation on address of pointer array,'a' value cannot be allowed.
cout << *(a++); //invalid
//The statement a+5 prints the address of the fivth element in array,a
cout << a+5; ////result : hexa decimal number ex : 003BFA60 (your output may be different)