so im writing this code, trying to do an exercise with friend functions and all that
I have that part down, I was told to take my original code and put in what you see now
"Make function add of type AltMoney. Thus, this function now computes the sum of dollars and cents and returns it as AltMoney. Note that in the above version of the program, you have passed the object sum as call_by_reference"
right so i get that...except now when i try to compile
Error 1 error C2248: 'AltMoney::cents' : cannot access private member declared in class 'AltMoney'
I get that 9 times, from lines 64-69
I'm about to mash my head on the keyboard, I'm just not getting why this is not working, can anyone help me out?
#include <stdafx.h>
#include <iostream>
#include <cstdlib>
using namespace std;
class AltMoney
{
public:
AltMoney();
AltMoney(int d, int c);
friend AltMoney add(AltMoney m1, AltMoney m2, AltMoney& sum);
void display_money();
AltMoney read_money ();
private:
int dollars;
int cents;
};
AltMoney read_money();
int main( )
{
int d, c;
AltMoney sum;
read_money();
AltMoney m1(d, c);
cout << "The first money is:";
m1.display_money();
read_money();
AltMoney m2(d, c);
cout << "The second money is:";
m2.display_money();
add(m1,m2, sum);
cout << "The sum is:";
sum.display_money();
system ("PAUSE");
return 0;
}
AltMoney::AltMoney()
{
dollars = 0;
cents = 0;
}
AltMoney::AltMoney(int d, int c)
{
dollars = d;
cents = c;
}
void AltMoney::display_money()
{
cout << "$" << dollars << ".";
if(cents <= 9)
cout << "0"; //to display a 0 in the left for numbers less than 10
cout << cents << endl;
}
AltMoney add(AltMoney m1, AltMoney m2)
{
int extra = 0;
AltMoney sum;
sum.cents = m1.cents + m2.cents;
if(sum.cents >=100){
sum.cents = sum.cents - 100;
extra = 1;
}
sum.dollars = m1.dollars + m2.dollars + extra;
return sum;
}
AltMoney read_money()
{
int d, c;
cout << "Enter dollar \n";
cin >> d;
cout << "Enter cents \n";
cin >> c;
if( d < 0 || c < 0)
{
cout << "Invalid dollars and cents, negative values\n";
exit(1);
}
}