qfedu-cpp-level/day4/d11.cpp

39 lines
687 B
C++
Raw Normal View History

// 重载 C++ 的 >> 输入流运算符
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Worker
{
friend istream &operator>>(istream &cin, Worker &obj);
friend ostream &operator<<(ostream &cout, Worker &obj);
private:
string name;
int salary;
};
istream &operator>>(istream &cin, Worker &obj)
{
cout << "Name: ";
cin >> obj.name;
cout << "salary: ";
cin >> obj.salary;
return cin;
}
ostream &operator<<(ostream &cout, Worker &obj)
{
cout << "Worker name is " << obj.name << ", salary is " << obj.salary << endl;
return cout;
}
int main()
{
Worker w1;
cin >> w1;
cout << w1;
return 0;
}