Solved

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

Posted on 2003-12-04
9
760 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:mayankeagle
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:mayankeagle
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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:mayankeagle
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:mayankeagle
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

706 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

14 Experts available now in Live!

Get 1:1 Help Now