Notice the location of the non-member function of the class in the class declara
ID: 3606538 • Letter: N
Question
Notice the location of the non-member function of the class in the class declaration and the syntax for the implementation of the non-member function body in the following code:
class Class_name
{
public:
constructors
destructor
member functions
accessors
mutators
public data
private:
helper functions
data
};
---------------------
return_type function_name(formal parameter list)
{
body
}
More information on classes can be found in your course textbook and on the web.
Experiments
Step 1: In this experiment you will investigate the implementation for overloading the operator “<<” as a non-member function of a class with chaining. Enter, save, compile and execute the following program in MSVS. Call the new project “NonMemberOpOverloadingExp” and the program “NonMemberOpOverloading.cpp”. Answer the questions below:
#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 “<<” operator in the program?
Question 3:What is the return type of the operator<< function in the program?
Question 4:What is the purpose of the Print function in the program?
Question 5:Why isn’t there a “Bank_Acct::” prefixed to the header of the operator<< function?
Q6;What are the similarities and differences you observed in overloading the operator<< as a friend function and as a non-member function of a class? [Answer after completing the Friend Operator Overloading lab.]
Please answer Q1-Q6 C++, any unwanted or spam answers will be reported, otherwise, a 5 stars :)
Explanation / Answer
Question 1:Please explain how chaining works? (hint: look at the cout statement in main)
Ans) chaining is an handy technique which is used to call multiple functions in the same line.
____________
Question 2:What is the arity of the “<<” operator in the program?
Ans) this is used to display the contents of an object (Here it is Bank_Acct).
_____________
Question 3:What is the return type of the operator<< function in the program?
Ans) the return type is output stream(ostream) which contains data
_____________
Question 4:What is the purpose of the Print function in the program?
Ans) this Print function is used to display the contents of an object (Bank_Acct).It displays the name and current balance of the Bank accout
_____________
Question 5:Why isn’t there a “Bank_Acct::” prefixed to the header of the operator<< function?
Ans) while implementing friend functions we no need to use prefix Bank_Acct::
_____________
Q6;What are the similarities and differences you observed in overloading the operator<< as a friend function and as a non-member function of a class? [Answer after completing the Friend Operator Overloading lab.]
Ans)
While implementing the friend functions its no need to be prefixed with the class name.But while implementing the non member functions it has to be prefixed with the class name.
____________Thank YOu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.