Link to home
Start Free TrialLog in
Avatar of namen
namen

asked on

friend functions to make use of cin and cout with objects, need some help

i need to make 2 friend functions to make use of cin and cout with objects.  When I compile this programi get errors saying i dont have access to private members..   I thought friend functions could access them.



class voter
{

friend ifstream& operator>> (ifstream&, voter v1);
friend ostream& operator << (ostream&, const voter v1);
      
private:      
      int  idnumber;
      char lastname [25];
      char firstname[25];
      char party         [2];
      int  birthyear;
      int  currentage;

public:
      voter();
      voter(int,char[], char[], char[], int, int);
      voter(const voter&);
      void Add();
      void Edit();
      void operator = (const voter&);
      void print(int);
      void disksave(ofstream& outfile);
      ~voter();

};

voter::voter()
{
      idnumber       =   0;
      lastname [0]   = '\0';
      firstname[0]   = '\0';
      party    [0]   = '\0';
      birthyear      =   0;
      currentage     =   0;
}

voter::voter(int id,char ln[], char fn[], char pid[], int year, int by)
{
      idnumber   = id;
      strcpy(lastname, ln);
      strcpy(firstname, fn);
      strcpy(party, pid);
      birthyear  = year;
      currentage = 2003 - birthyear;
}

voter::voter(const voter& v1)
{
      idnumber   = v1.idnumber;
      strcpy(lastname, v1.lastname);
      strcpy(firstname, v1.firstname);
      strcpy(party, v1.party);
      birthyear  = v1.birthyear;
      currentage = 2003 - v1.birthyear;
}

void voter::operator = (const voter& v1)
{
      idnumber   = v1.idnumber;
      strcpy(lastname, v1.lastname);
      strcpy(firstname, v1.firstname);
      strcpy(party, v1.party);
      birthyear  = v1.birthyear;
      currentage = 2003 - v1.birthyear;
}

void voter::Add()
{

      cls();
      int age =0;

      char trash[2];
      
      cout<<"Enter your voter id number: ";
      cin >> idnumber;

      while ((idnumber < 1000) || // Loop that validates voter
              ( idnumber > 10000))  // id number.

      {      
            cls();
            cout << "Valid ID number is between 1000 and 10000"
                   << endl << endl;

            cout << "Please enter a valid voter id number > ";
            cin  >>  idnumber;
            cout << endl;
      }

      cout<<"Enter your last name: ";
      cin.getline(trash,2);
      cin.getline(lastname, 25);

      cout<<"Enter your first name: ";
      cin.getline(firstname, 25);

      cout<<"Enter your party id letter: ";
      cin>>party;

      cout<<"Enter your birth year: ";
      cin>>birthyear;

      while((birthyear < 1878) || (birthyear > 1985))
      {
      
            cls();
            age = 2003 - birthyear;
      
      
            cout << " You must be atleast 18 yrs. old to vote!!"
                   << endl;
            cout << " Your current age: " << age << endl;



            cout << "\n Please enter a valid birth year: ";
            cin  >> birthyear;
      }

      currentage = 2003 - birthyear;      
}

void voter::Edit()
{
      cout<<"What is the voters new Party: ";
      cin>>party;

}

void voter::print(int label)
{
      cout << setw(3)
             << label            << setw(10)
           << idnumber      << setw(13)
             << lastname      << setw(8)  
             << firstname      << setw(9)
             << party            << setw(10)
             << birthyear      << setw(8)
             << currentage  << setw(8)
             << endl;
}

void voter::disksave(ofstream& outfile)
{
outfile  << setw(6)  << idnumber
             << setw(14) << lastname  
             << setw(12) << firstname
             << setw(12) << party
             << setw(12) << birthyear
             << setw(12) << currentage  
             << setw(12)
         << endl;
}


voter::~voter()
{



}



ifstream& operator >> (ifstream& vin, voter& v1)
{
      cls();
      int age =0;

      
      cout<<"Enter your voter id number: ";
      cin >> idnumber;


      cout<<"Enter your last name: ";
      cin.getline(trash,2);
      cin.getline(lastname, 25);

      cout<<"Enter your first name: ";
      cin.getline(firstname, 25);

      cout<<"Enter your party id letter: ";
      cin>>party;

      cout<<"Enter your birth year: ";
      cin>>birthyear;


      currentage = 2003 - birthyear;      

      return vin;


}

ostream& operator << (ostream& vout, const voter& v1)
{

vout    << setw(6)  << v1.idnumber
             << setw(14) << v1.lastname  
             << setw(12) << v1.firstname
             << setw(12) << v1.party
             << setw(12) << v1.birthyear
             << setw(12) << v1.currentage  
             << setw(12)
         << endl;

return vout;

}




ASKER CERTIFIED SOLUTION
Avatar of blahpers
blahpers

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mayank S
Also, in your ifstream .... >> () definition:

>>  cin >> idnumber;

should be: cin >> v1.idnumber;

You cannot access data-members directly without using the objects because this is not a member function but a friend function. Use v1.lastname instead of lastname, and so on.
Is friend functions realy able to access private. Isn't it only protected data that is available???

I remember it as private is alwas only available to local class. But protected is available to friend and inheritaged.
No, friends can access private data. That rule applies to inheritance. While inheriting from a class, you can only access the protected members of the base class. However, the private members are also inherited but not directly accessible. They can be accessed by get () and set () methods in the base-class.

(Its been years since I worked on C++.... I'm working on J2EE now, but I hope that I remember it right.)
Avatar of sin_
sin_

Did you find your answer? Guys have given you the answers. If you haven't happend to look at the above, please go through a few suggestions:

1. Always pass by reference
friend ifstream& operator>> (ifstream&, voter v1);
friend ostream& operator << (ostream&, const voter v1);

You are passing by value here. It means that a copy ctor will be called when you pass it. Had you used the reference, you would have avoided this costly operation.

2. It's very much true that friend functions or classes can access the private data members of the other classes.
But you need to access the data members through the object names. Otherwise, they are simply undeclared variables..rite?
ifstream& operator >> (ifstream& vin, voter& v1)
{
     cls();
     int age =0;

     
     cout<<"Enter your voter id number: ";
     cin >> idnumber;


it should be, cin >> v1.idnumber


hope it helps
-sin
Avatar of namen

ASKER

Thanks for all the help i have it working now.
With all due respect, I think that you should've split between blahpers and mayankeagle.
I agree.  I only gave the program a once-over; I wasn't expecting my answer to solve everything, and mayankeagle showed that it didn't.
Anyways, I don't mind. I guess namen is also very new to EE and doesn't know about splitting points.