Hi i have this code below i am encountering that my int a,b; got error because they are delcare in private. but the code turn out fine if i delcare them in public.
is there anyway i can make them remain in private. i think need to use reference or wad. can anyone help me one this?
#include <iostream>
#include <string>
#include <stdlib.h>
#include <string.h>
using namespace std;
class Account
{
//declaration of friend functions
friend ostream& operator<<(ostream&,const Account&);
friend istream& operator>>(istream&,Account&);
friend Account operator +(const Account&,int);
//friend Account Account::operator+(double r)
//friend Account operator +(int,const Account&);
//declaration of public members
public :
Account();
Account(int a1,int b1);
Account operator +(const Account&)const;
//declaration of private members
private:
int a,b;
};
//constructor for matrix class
Account::Account()
{
a=0;
b=0;
}
Account::Account(int a1,int b1)
{
a=a1;
b=b1;
}
//overloading stream extraction operator
ostream& operator << (ostream &out,const Account &ac)
{
//out << ac.a << "." << ac.b << endl;
out << ac.a << "."<<ac.b << endl;
return out;
}//end of function
//overloading stream insertion operator
istream& operator >>(istream &in,Account &ac)
{
string a,b,s;
fflush(stdin);
getline(in,s);
a = s.substr(0);
b = s.substr(0);
ac.a = atoi(a.c_str());
string::size_type nDecimalPosition = s.find('.');
if (nDecimalPosition != string::npos)
ac.b = atoi(s.c_str() + nDecimalPosition + 1);
return in;
}
Account operator +(const Account &rhs,double i)
{
Account temp;
//temp.a = rhs.a+i;
//temp.b = rhs.b+i;
double temprhs = (double)rhs.a + (double)rhs.b / 100;
double result = 0.0;
cout << "temprhs " << temprhs << endl;
cout << "i " << i << endl;
result=temprhs+i;
temp.a = (int)result;
temp.b = (int)((result - (int)result) * 100);
return temp;
}//end of function
int main()
{
Account A, B, C;
cout << "Enter first account balance, format xx.yy: ";
cin >> A;
cout << "Enter second account balance, format xx.yy: ";
cin >> B;
C = A + 20.70;
cout << "Addition A + 20.70 = " << C << endl;
system("pause");
return 0;
}