Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-12-04
9
Medium Priority
?
778 Views
Last Modified: 2008-02-01
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;

}




0
Comment
Question by:namen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 

Accepted Solution

by:
blahpers earned 500 total points
ID: 9880195
Your declarations for operator<< and operator>> look like:

friend ifstream& operator>> (ifstream&, voter v1);
friend ostream& operator << (ostream&, const voter v1);

But your definitions look like:

ifstream& operator >> (ifstream& vin, voter& v1)
ostream& operator << (ostream& vout, const voter& v1)

The declarations take voter as arguments; the definitions take reference-to-voter.  Just stick the missing ampersands in your declarations so they match the definitions.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9880235
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.
0
 
LVL 2

Expert Comment

by:JensUniweb
ID: 9882137
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 30

Expert Comment

by:Mayank S
ID: 9882174
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.)
0
 
LVL 2

Expert Comment

by:sin_
ID: 9882793
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
0
 

Author Comment

by:namen
ID: 9888164
Thanks for all the help i have it working now.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9891804
With all due respect, I think that you should've split between blahpers and mayankeagle.
0
 

Expert Comment

by:blahpers
ID: 9916111
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9917847
Anyways, I don't mind. I guess namen is also very new to EE and doesn't know about splitting points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question