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

#include <iostream> #include <string> using namespace std; const int SIZE = 10;

ID: 647027 • Letter: #

Question

#include <iostream>

#include <string>

using namespace std;

const int SIZE = 10;

class Bank_Acct

{

public:

      Bank_Acct( ); //default constructor

      Bank_Acct(double new_balance, string Cname); //explicit value

                                                   //constructor

      void Print(ostream & out); //accessor function

private:

      double balance;

      string name;

};

Bank_Acct::Bank_Acct()

{

      balance = 0;

      name = "NoName";

}

Bank_Acct::Bank_Acct(double amount, string Cname)

{

      balance = amount;

      name = Cname;

}

void Bank_Acct::Print(ostream & output)

{

      output<<endl<<"Object "<<name;

      output<<endl<<"The new balance is "<<balance<<endl;

}

ostream & operator<<(ostream & output, Bank_Acct & Org)

{

      Org.Print(output);

      return output;

}

int main()

{

      Bank_Acct my_Acct;

      Bank_Acct DrB(2000.87, "Dr. Bullard");

      //the following statement contains chaining

      cout<<DrB<<endl<<my_Acct<<endl;

      return 0;

}

Question 1:Please explain how chaining works? (hint: look at the cout statement in main)

Question 2:What is the arity of the

Explanation / Answer

1)

The cout statement calls the << function and the << function for DrB which in turn calls the print function. The print function takes the parameters from parameterized constructor and displays the parameter values as name which is Dr. Bullard and new balance which is 2000.87.

The cout statement calls the << function and the << function for my_Acct which in turn calls the print function. The print function invokes the default constructor and sets the name as NoName and new balance as 0 and displays the name and new balance.

2)

The arity of a function or operation is the number of arguments or operands the function or operation accepts. The << function accepts the output and Org as arguments and calls the function print with output as parameter.

3)

The return type of << is ostream.

4)

The print function is used to print the user name and new balance. If the default constructor is invoked with print function, then the print function sets the name to NoName and new balance to 0. If the parameterized constructor is invoked with print function, then the print function prints the arguments passed.

5)

Operator overloading of “<<” insertion operator is not member function. Member functions only prefixed with operator ::(scope resolution) along with class name it belongs to.