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;
}

Recommended Answers

All 6 Replies

maybe use a helper method to get the values

> Account operator +(const Account &rhs,double i)
Where is this, in your class, as a public function?

hmm that the part i dunno where went wrong.

can someone enlighten me?

>>can someone enlighten me?
Lol, I donot know about enlightening, but do know where the error is.


You have defined a function

Account operator +(const Account &rhs,double i)

What Salem means to say is.

In the declaration of Class Account.
You have not added the "PROTOTYPE" or declaration of a function .
Class declaration means

class Account{
//Declarations go here.
};

Apart from that make sure you declare it as Public :)

>Class declaration means...
Between you and me: this constuct called a class definition in the C++ Standard...
;)

>Class declaration means...
Between you and me: this constuct called a class definition in the C++ Standard...
;)

Lol true, As i have written Declaration twice in the above para. I thought i would just re interpete it in that way of my sense. Sorry though .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.