Answers for "Overloading IO Stream"

C++
0

Overloading IO Stream

//Overloading C++ Stream , Extraction Operator| C++ Programming
#include <bits/stdc++.h>
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;
}
Posted by: Guest on September-11-2021

Browse Popular Code Answers by Language