使用友元函数,让老师输入学生的数据,学生只能查看自己的数据;老师能录入自己的信息!
#include<iostream>
#include<string>
using namespace std;
class preson{
public:
//preson(){};
void input()
{
cout<<"请输入姓名:";
cin>>name;
cout<<"请输入编号:";
cin>>number;
}
void show()
{
cout<<"姓名:"<<name<<endl;
cout<<"编号:"<<number<<endl;
}
protected:
string name;
string number;
};
class teacher;
class student:public preson{
public:
student()
{
};
void show()
{
cout<<"--------------------------"<<endl;
preson::show();
cout<<"班别:"<<Class<<endl;
cout<<"成绩:"<<score<<endl;
}
friend teacher;
private:
string Class;
float score;
};
class teacher:public preson{
public:
teacher()
{};
void input_teacher()
{
preson::input();
cout<<"请输入老师的职称:";
cin>>title;
cout<<"请输入老师的部门:";
cin>>dept;
system("cls");
}
void input_student(student &s)
{
cout<<"--------------------------"<<endl;
cout<<" 请输入学生的信息 "<<endl;
cout<<"请输入学生的姓名:";
cin>>s.name;
cout<<"请输入学生的学号:";
cin>>s.number;
cout<<"请输入学生的班别:";
cin>>s.Class;
cout<<"请输入学生的成绩:";
cin>>s.score;
system("cls");
}
void show()
{
preson::show();
cout<<"职称:"<<title<<endl;
cout<<"部门:"<<dept<<endl;
}
private:
string title;
string dept;
};
int main()
{
teacher tea1;
student stu1;
tea1.input_teacher();
tea1.show();
tea1.input_student(stu1);
tea1.show();
stu1.show();
system("pause");
return 0;
}