📜  重载 IO 流 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:52.152000             🧑  作者: Mango

代码示例1
//Overloading C++ Stream , Extraction Operator| C++ Programming
#include 
using namespace std;
class Person
{
    string name;
    int age;

public:
    Person()
    {
        this->name = "No nmae";
        this->age = 0;
    }

    friend ostream &operator<<(ostream &output, Person &p);
    friend istream &operator>>(istream &input, Person &p);
};

ostream &operator<<(ostream &output, Person &p)
{
    output << "What the devil " << endl;
    output << "My name is " << p.name << " and My age is " << p.age << endl;
    return output;
}
istream &operator>>(istream &input, Person &p)
{
    input >> p.name >> p.age;
    return input;
}

int main()
{
    cout << "Enter the name and age " << endl;
    Person ak;
    cin >> ak;
    cout << ak;
    return 0;
}