Like Tree2Likes
  • 2 Post By laserlight

arity

This is a discussion on arity within the C++ Programming forums, part of the General Programming Boards category; If the arity of the + operator is 2, why is there only one formal parameter in operator+s function header? ...

  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
    22,309
    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.
    CASHOUT and Elysia like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21