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

Notice that the non-member function does not have a prototype in the class decla

ID: 3606535 • Letter: N

Question

Notice that the non-member function does not have a prototype 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 Class_name::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 member function of a class with chaining. Enter, save, compile and execute the following program in MSVS. Call the new project “MemberOpOverloadingExp” and the program “MemberOpOverloading.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( ); //accessor function

      Bank_Acct & operator+(double amount); //mutator 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()

{

      cout<<endl<<"Object "<<name;

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

}

Bank_Acct & Bank_Acct::operator+(double amount)

{

      balance += amount;

      return *this;

}

int main()

{

      Bank_Acct my_Acct;

      cout.setf(ios::showpoint);

      cout.setf(ios::fixed);

      cout.precision(2);

      cout<<"Original balance of my_Acct"<<endl;

      my_Acct.Print( );

      //the following statement contains chaining

      my_Acct + 18.75 + 14.35 + 10054.96;

      cout<<"The balance of my_Acct after addition to balance 3 times"<<endl;

      my_Acct.Print();

      return 0;

}

Question 1:Referring to the program in Step 1, if the arity of the “+” operator is two, why is there only one formal parameter in operator+’s function header?

Question 2:What is the return type of the operator+ function?

Question 3:Referring to the operator+ function, what is the name of the Bank_Acct object it returned?

Question 4:Why is there a “Bank_Acct::” prefixed to the header of the operator+ function?

Question 5:Can you explain how chaining is performed for the operator+ function in the program in Step 1.

Question 6:Please explain why the operator “<<” cannot be overloaded as a member function? (hint: modify the code that was used in the laboratory “Classes: Part 2” to overload the “<<” operator as a friend member function.)


C++ any spam or wrong answers will be reported; otherwise, 5 stars :) please answer Q1-Q6

Explanation / Answer

1. Because the second argument will be implicitly passed as this pointer or the calling object on which the operation is performed. It is equivalent to writing.
my_Acct + 18 == my_Acct.operator+(18)

2. operator+ returns reference to the same object that it is called upon after modifying.

3. In example it will return same name as it was before that is "NoName"

4. Because it is defined as a member function(non static function) of the class Bank_Acct.

5. When operators are overloaded their associativity and precedence remains the same. So in example when chaining is performed. The expression my_Acct + 18.75 + 11.25 + 10054.00; will evaluate like (((my_Acct + 18.75) + 11.25) + 10054.00); (my_Acct + 18.75) will return reference to the object of type Bank_Acct which will be evaluated with (my_Acct(refernce to object of type Bank_Acct) + 11.25) which again do the same and at end we will have a reference to object which is not assigned to any variable. So control moves to next statement.

6. The operator << is a binary operator and infact it can be overloaded as a member function in the class but then it cannot perform on the ostream object like cout. To call it on the ostream object we must have to declare it as a friend of our class.

Think of it like this: when you want to stream to ostream, you're calling the << operator on the stream object. And you're not allowed to directly modify the ostream's 'private' method. So you have to create an overloaded version, and make it a friend.

When you create your own operator<< method in your class, you're creating a << method that will operate on your class, not an ostream object. You can hold an ostream internally to your class and stream to it. Example.
You can do something like.

Bank_Acct& Bank_Acct::operator << (Bank_Acct& acct)
{
std::cout << this->name << " " << this->balance << std::endl;
return *this;
}
This will work like my_Acct << my_Acct; == my_Acct.operator<<(my_Acct);
You can also chain this as:
my_Acct << my_Acct << my_Acct; == my_Acct.operator<<(my_Acct.operator<<(my_Acct));
and if you declare it as a friend you can do something like
cout << my_Acct; == operator<<(std::cout , my_Acct);

Inside Class declaration:
It can be private or public
friend std::ostream & operator<<(std::ostream &os, const Bank_Acct & acct);

Outside class definition:
std::ostream & operator<<(std::ostream &os, const Bank_Acct& acct){
return os << acct.name << " " << acct.balance << std::endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote