为什么定义友元函数后无法访问class的私有变量?
Why can't I access the private variable of a class after defining a friend function?
我写了一个简单的员工管理项目。尽管我将运算符重载定义为友元函数,但尝试将值分配给 class 的私有变量时,我遇到了问题。
这是我的代码:
#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)) ===|
这里可以访问Employeeclass的私有变量并赋值,但是不能访问Personclass的私有变量。
好友功能在这里不起作用?
我如何编写代码来解决它?如何将文件中的数据加载到 Employee class 的对象中?
当您在 class 中声明一个成员私有时,它对 class 本身是私有的,甚至对其 children 也是私有的。
因此,您示例中的 pName
无法从 Employee
的 child class 访问。
如果您声明它受保护,那么 child class 可以访问它(并修改它)
为了在 child class 中更改它,同时仍然保持成员私有,您需要在 parent [中提供访问器(get/set 方法)class。 'setPName' 访问器可以是 public,或者如果您想限制对 Employee
class.
的修改,则可以对其进行保护
我写了一个简单的员工管理项目。尽管我将运算符重载定义为友元函数,但尝试将值分配给 class 的私有变量时,我遇到了问题。
这是我的代码:
#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)) ===|
这里可以访问Employeeclass的私有变量并赋值,但是不能访问Personclass的私有变量。
好友功能在这里不起作用?
我如何编写代码来解决它?如何将文件中的数据加载到 Employee class 的对象中?
当您在 class 中声明一个成员私有时,它对 class 本身是私有的,甚至对其 children 也是私有的。
因此,您示例中的 pName
无法从 Employee
的 child class 访问。
如果您声明它受保护,那么 child class 可以访问它(并修改它)
为了在 child class 中更改它,同时仍然保持成员私有,您需要在 parent [中提供访问器(get/set 方法)class。 'setPName' 访问器可以是 public,或者如果您想限制对 Employee
class.