C ++ stream into the "<<" and extracting ">>" operator overloading

01 stream insertion operator << overloaded

C ++ content in the output, the most common way:

std::cout << 1 <<"hello";

problem:

  • Why was this statement can set up it?
  • coutWhat is? " <<" Operator can be used in couton it?

the reason:

  • In fact, coutin iostreamthe definition header file ostreamobject class.
  • " <<" It can be used in coutpart because, in the ostreamclass of " <<" was overloaded .

For std::cout << 1 <<"hello";this statement, it is possible to overload the following manner ostreammember function:

ostream & ostream::operator<<(int n)
{
    .... // 输出n整型的代码
    return *this;
}

ostream & ostream::operator<<(const char * s)
{
    .... // 输出s字符串的代码
    return *this;
}
  • std::cout << 1;Statement is equivalent tocout.operator<<(1);
  • std::cout << "hello";Statement is equivalent tocout.operator<<("hello");
  • std::cout << 1 <<"hello";Statement is equivalent to( cout.operator<<(1) ).operator<<("hello");

<< Examples 02 stream insertion operator overloading

Suppose we want to put an object in the contents of the printout, then we can override ostreamthe class stream insertion <<operator.

Below CStudentclass as an example:

class CStudent // 学生类
{
public:
    // 构造函数
    CStudent(int id = 0, int age = 0, string name = ""):m_id(id), m_age(age), m_name(name) { }
    
    // 将该函数声明成友元函数
    // 目的是使得函数可以访问CStudent类的私有成员变量
    friend ostream & operator<<(ostream & o, const CStudent & s);
    
private:
    int m_age;      // 年龄
    int m_id;       // ID号
    string m_name;  // 名字
};

// 重载ostream对象的流插入<<运算符函数
// 目的是使得能打印输出CStudent对象的信息
ostream & operator<<(ostream & o, const CStudent & s)
{
    o << s.m_id << "," << s.m_age << "," << s.m_name;
    return o;
}

int main()
{
    CStudent stu(1, 20, "小林coding");
    std::cout << stu ; // 输出std对象的全部信息
    
    return 0;
}

Output:

1,20,小林coding

It should be noted that ostream & operator<<(ostream & o, const CStudent & s)the function is global, so the first parameter of the function must be passed in ostreamthe object and CStudentclass need to declare this function as a friend function , so that the function can access CStudentprivate member variable class.

Examples of the extraction flow 03 >> operator overloading

Or to the CStudentclass as an example, suppose you want the content of a keyboard input, to initialize the object, we can override istreamclass extracting >>operator.

class CStudent // 学生类
{
public:

    // 构造函数
    CStudent(int id = 0, int age = 0, string name = ""):m_id(id), m_age(age), m_name(name) { }
    
    // 将该函数声明成友元函数
    // 目的是使得函数可以访问CStudent类的私有成员变量
    friend ostream & operator<<(ostream & o, const CStudent & s);
    
    // 将该函数声明成友元函数
    // 目的是使得函数可以给CStudent类的私有成员变量进行赋值
    friend istream & operator>>(istream & is,  CStudent & s);
    
private:
    int m_age;      // 年龄
    int m_id;       // ID号
    string m_name;  // 名字
};

// 重载ostream对象的流插入<<运算符函数
// 目的是使得能打印输出CStudent对象的信息
ostream & operator<<(ostream & o, const CStudent & s)
{
    o << s.m_id << "," << s.m_age << "," << s.m_name;
    return o;
}

// 重载istream对象的流提取>>运算符函数
// 目的是使得初始化CStudent对象的内容
istream & operator>>(istream & is,  CStudent & stu)
{
    string inputStr;
    is >> inputStr;
    
    int pos = inputStr.find(",", 0);         // 查找首次出现逗号的位置
    string tmpStr = inputStr.substr(0, pos); // 截取从0到pos位置的字符串
    stu.id = atoi(tmpStr.c_str());           // atoi可以将char*类型的内容转成int类型
    
    int pos2 = inputStr.find(",", pos + 1);            // 查找第二次出现逗号的位置
    tmpStr = inputStr.substr(pos + 1, pos2 - pos -1);  // 取出age的值
    stu.age = atoi(tmpStr.c_str());                    // atoi可以将char*类型的内容转成int类型
    
    tmpStr = inputStr.substr(pos2 + 1, inputStr.length() - pos2 - 1); // 取出name的值
    stu.name = tmpStr;
    
    return is;
}

int main()
{
    CStudent stu;
    
    // 将输入的信息,初始化stu对象
    cin << stu;
    
    // 输出std对象的信息
    cout >> stu;
    
    return 0;
}

Input and output content Content:

// 输入内容:
1,20,小林coding

// 输出内容:
1,20,小林coding

04 Summary

To insert the flow <<operators and extracting >>operators can for custom objects , then we need to reload for the object's ostreamclass <<and operators istreamof the >>operator, and can only be overloaded as a global function , then the CStudentclass there need to be above two overloaded functions declared as a friend function , making two overloaded functions can be accessed and assigned CStudentclass private member functions.

Guess you like

Origin www.cnblogs.com/xiaolincoding/p/11969861.html