Solved

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

Posted on 2003-12-04
9
763 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
9 Comments
 

Accepted Solution

by:
blahpers earned 125 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

896 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now