1.

Solve : Nonmember functions vs. member functions.?

Answer»

I am having trouble understanding why a prefix is USED and not used in these coding. The first code will be for a non member function, and the next will be for the member function. There is a prefix that is used in the Member Function code, but not in the NonMember Function code. The prefix that is used in the Member Function code is "Bank_Acct::" This is found in the header of the operator<< function. But this header of the operator<< function in the NonMember Function is not used. I'm trying to figure out why, but I can't seem to figure it out. Also, in the NonMember Function, I am having trouble figuring out the purpose of the Print function.

NonMember Function
Code: [Select]#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;
}

Member Function
Code: [Select]#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;
}


Sorry I can't help you. What compiler are you using?
Also, your use of WHITE space is confusing.it's nothing along the lines of compiler issues. its just a question. the question is howcome in the member function, there is the "Bank_Acct::" prefixed in front of the header of the operator<< function, but there is none is non-member function? Quote from: helpme101 on November 18, 2011, 09:27:22 PM

it's nothing along the lines of compiler issues. its just a question. the question is howcome in the member function, there is the "Bank_Acct::" prefixed in front of the header of the operator<< function, but there is none is non-member function?

Probably because the member function version doesn't use << on the Bank_Acct type and thus doesn't need one defined. it uses print() instead.ok, yea that makes SENSE. thanks. do u think you can help me out with my other post. not sure if you took a look at it. it's titled C++ STL map.


Discussion

No Comment Found