ep9_5.cpp
上传用户:wxcui2006
上传日期:2022-07-12
资源大小:1274k
文件大小:2k
- /*9.1 重载学生类的"<<"和">>"运算符。*/
- #include<fstream>
- #include<iostream>
- #include<string>
- #include<iomanip>
- using namespace std;
- class student{
- int id ; //学号
- string name; // 姓名
- char sex; // 性别
- int age; // 年龄
- string address; //家庭地址
- float eng, phy, math, electron; //英语,物理,数学和电子学成绩
- public:
- student(int=0,string="#",char='#',int=0,string="#",float=0,float=0,float=0,float=0);
- friend ostream&operator<<(ostream&dest,student&st); //重载插入运算符
- friend istream&operator>>(istream&sour,student&st); //重载提取运算符
- }; //流类作为形式参数必须是引用
- student::student(int i,string n,char s,int a,string add,float en,float ph,float ma,float ele){
- id=i;
- name=n;
- sex=s;
- age=a;
- address=add;
- eng=en; phy=ph; math=ma; electron=ele;
- }
- ostream&operator<<(ostream&dest,student&st){ //重载插入运算符
- dest<<st.id<<'t'<<st.name<<'t'<<st.sex<<'t'<<st.age<<'t'<<st.address<<'t'
- <<st.eng<<'t'<<st.phy<<'t'<<st.math<<'t'<<st.electron<<endl;
- return dest;
- }
- istream&operator>>(istream&sour,student&st){ //重载提取运算符
- cout<<"请输入学号:"<<endl;
- sour>>st.id;
- cout<<"请输入姓名:"<<endl;
- sour>>st.name;
- cout<<"请输入性别:"<<endl;
- sour>>st.sex;
- cout<<"请输入年龄:"<<endl;
- sour>>st.age;
- cout<<"请输入地址:"<<endl;
- sour>>st.address;
- cout<<"请输入英语、物理、数学、电子各科成绩:"<<endl;
- sour>>st.eng>>st.phy>>st.math>>st.electron;
- return sour;
- }
- int main(){
- student st1,st2(2104105,"陈英",'m',19,"黄山路380号",89,78,90,96);
- cin>>st1;
- cout<<st1<<st2;
- return 0;
- }