为什么我在定义友元函数后无法访问类的私有变量?

时间:2015-06-26 20:46:44

标签: operator-overloading multiple-inheritance friend-function

我写了一个简单的员工管理项目。当我尝试将值分配给类的私有变量时,虽然我将运算符重载定义为友元函数,但我遇到了问题。

这是我的代码:

#include<iostream>
#include<string>
#include<vector>
#include<fstream>

using namespace std;

class Person
{
    string pName;
    char pSex;
    int pAge;
    string pMob;
    string pAddress;

public:
    Person(){}
    Person(string &pn, char &ps, int &pa, string &pm, string &pad):
        pName(pn),pAge(pa),pMob(pm),pAddress(pad),pSex(ps){}

    string getName(){return pName;}
    int getAge(){return pAge;}
    string getMob(){return pMob;}
    string getAddress(){return pAddress;}
    char getSex(){return pSex;}
};

class Employee:public Person
{
    string eID;
    string eJDate;
    string eRank;
    double eSalary;

public:
    Employee(){}
    Employee(string &pn, char &ps, int &pa, string &pm, string &pad, string &eid, string &ejd, string &er,  double &es):
        Person(pn,ps,pa,pm,pad),eID(eid),eJDate(ejd),eRank(er),eSalary(es){}

    string getID(){return eID;}
    string getJDate(){return eJDate;}
    string getRank(){return eRank;}
    double getSalary(){return eSalary;}

    friend ostream& operator<<(ostream& os, Employee& ob);
    friend istream& operator>>(istream& inf, Employee& ob);
};

ostream& operator<<(ostream& os, Employee& ob)
{
    os<<ob.getName()<<endl;
    os<<ob.getSex()<<endl;
    os<<ob.getAge()<<endl;
    os<<ob.getMob()<<endl;
    os<<ob.getAddress()<<endl;
    os<<ob.getID()<<endl;
    os<<ob.getJDate()<<endl;
    os<<ob.getRank()<<endl;
    os<<ob.getSalary()<<endl;

    return os;
}

istream& operator>>(istream& inf, Employee& ob)
{
    inf>>ob.pName;
    inf>>ob.pSex;
    inf>>ob.pAge;
    inf>>ob.pMob;
    inf>>ob.pAddress;
    inf>>ob.eID;
    inf>>ob.eJDate;
    inf>>ob.eRank;
    inf>>ob.eSalary;

    return inf;
}

int main()
{
    cout<<"\t\t\t\tEnter your choice\n";
    cout<<"\t\t\t\t-----------------\n";

    cout<<"1: Enter an employee data."<<endl;
    cout<<"2: View all the employee data."<<endl;
    cout<<"Enter choice: ";

    int choice;
    cin>>choice;

    if(choice==1)
    {
        string name,mob,address,id,jdate,rank;
        int age;
        char sex;
        double salary;

        ofstream out;
        out.open("DB.txt",ios_base::app);

        cout<<"Enter Name : ";
        cin>>name;
        cout<<"Enter Sex (M/F): ";
        cin>>sex;
        cout<<"Enter Age : ";
        cin>>age;
        cout<<"Enter Mobile No : ";
        cin>>mob;
        cout<<"Enter Address : ";
        getline(cin,address);
        cout<<"Enter ID : ";
        cin>>id;
        cout<<"Enter Join Date (dd-mm-yyyy): ";
        cin>>jdate;
        cout<<"Enter Position : ";
        cin>>rank;
        cout<<"Enter Salary : ";
        cin>>salary;

        Employee ob(name, sex, age, mob, address, id, jdate, rank, salary);

        out<<ob;
    }
    else if(choice==2)
    {
        ifstream inf("DB.txt");
        vector<Employee>ve;
        Employee ob;
        while(inf>>ob)
        {
            ve.push_back(ob);
        }

        for(int i=0; i<ve.size(); i++)
        {
            cout<<"\nEmployee No - "<<i<<endl;
            cout<<"Employee Name: "<<ve[i].getName()<<endl;
            cout<<"Sex: "<<ve[i].getSex()<<endl;
            cout<<"Age: "<<ve[i].getAge()<<endl;
            cout<<"Mobile: "<<ve[i].getMob()<<endl;
            cout<<"Address: "<<ve[i].getAddress()<<endl;
            cout<<"ID: "<<ve[i].getID()<<endl;
            cout<<"Joining Date: "<<ve[i].getJDate()<<endl;
            cout<<"Rank: "<<ve[i].getRank()<<endl;
            cout<<"Salary: "<<ve[i].getSalary()<<endl;
        }
    }
    return 0;
}

它提供以下错误 -

7|error: ‘std::string Person::pName’ is private|
63|error: within this context|
8|error: ‘char Person::pSex’ is private|
64|error: within this context|
9|error: ‘int Person::pAge’ is private|
65|error: within this context|
10|error: ‘std::string Person::pMob’ is private|
66|error: within this context|
11|error: ‘std::string Person::pAddress’ is private|
67|error: within this context|
||=== Build failed: 10 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

在这里,我可以访问并为Employee类的私有变量赋值,但无法访问Person类的私有变量。

朋友功能在这里不起作用?

我如何编码来解决它?如何将文件中的数据加载到Employee类的对象中?

1 个答案:

答案 0 :(得分:1)

当你在一个类中声明一个私有成员时,它对于类本身是私有的,甚至是它的子类。 因此,pName的子类无法访问您示例中的Employee。 如果您声明它受保护,那么子类可以访问它(并修改它)

为了在子类中更改它,同时仍保持成员私有,您需要在父类中提供访问器(get / set方法)。 &#39; setPName&#39;如果要限制对Employee类的修改,则访问者可以是公共的,也可以是受保护的。

相关问题