Thread: arity

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    arity

    If the arity of the “+” operator is 2, why is there only one formal parameter in operator+’s function header?

    Code:
    #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: " << 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;
    	cout << "----------------------------" << endl;
    	my_Acct.Print( );
    
    	// the following statement contains chaining
    	my_Acct + 18.75 + 14.35 + 10054.96;
    
    	cout << endl << endl;
    
    	cout << "The balance of my_Acct after addition to balance 3 times:" << endl;
    	cout << "---------------------------------------------------------" << endl;
    	my_Acct.Print();
    
    	cout << endl << endl;
    
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CASHOUT
    If the arity of the “+” operator is 2, why is there only one formal parameter in operator+’s function header?
    Because that overload of operator+ is a member function, hence the current object is the left hand side argument to operator+.

    EDIT:
    Also, having operator+ modify the current object is not the usual semantics. It looks like it should have been operator+= instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PREDICATE( name, arity )
    By HJoshy in forum General AI Programming
    Replies: 1
    Last Post: 06-05-2010, 01:16 PM