Update 输入输出运算符重载.cpp

This commit is contained in:
Francis 2022-04-10 17:39:19 +08:00 committed by GitHub
parent 70fb25143f
commit 23228108df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,39 +1,41 @@
/* /*
Salesnameidage Salesnameidage
/Sales类数据成员的输入和输出 /Sales类数据成员的输入和输出
*/ */
#include<iostream.h> #include<iostream>
#include<string.h> #include<cstring>
using namespace std;
class Sales{ class Sales{
private: private:
char name[10]; char name[10];
char id[18]; char id[18];
int age; int age;
public: public:
Sales(char *Name,char *ID,int Age); Sales(char *Name,char *ID,int Age);
friend Sales &operator<<(ostream &os,Sales &s); //重载输出运算符 friend Sales &operator<<(ostream &os,Sales &s); //重载输出运算符
friend Sales &operator>>(istream &is,Sales &s); //重载输入运算符 friend Sales &operator>>(istream &is,Sales &s); //重载输入运算符
}; };
Sales::Sales(char *Name,char *ID,int Age) { Sales::Sales(char *Name,char *ID,int Age) {
strcpy(name,Name); strcpy(name,Name);
strcpy(id,ID); strcpy(id,ID);
age=Age; age=Age;
} }
Sales& operator<<(ostream &os,Sales &s) { Sales& operator<<(ostream &os,Sales &s) {
os<<s.name<<"\t"; //输出姓名 os<<s.name<<"\t"; //输出姓名
os<<s.id<<"\t"; //输出身份证号 os<<s.id<<"\t"; //输出身份证号
os<<s.age<<endl; //输出年龄 os<<s.age<<endl; //输出年龄
return s; return s;
} }
Sales &operator>>(istream &is,Sales &s) { Sales &operator>>(istream &is,Sales &s) {
cout<<"输入雇员的姓名,身份证号,年龄"<<endl; is>>s.name>>s.id>>s.age; cout<<"输入雇员的姓名,身份证号,年龄"<<endl;
return s; is>>s.name>>s.id>>s.age;
return s;
} }
void main(){ int main(){
Sales s1("杜康","214198012111711",40); //L1 Sales s1("杜康","214198012111711",40); //L1
cout<<s1; //L2 cout<<s1; //L2
cout<<endl; //L3 cout<<endl; //L3
cin>>s1; //L4 cin>>s1; //L4
cout<<s1; //L5 cout<<s1; //L5
} }