C ++ implements heavy-load ostream and heavy-load ostream

Source: Internet
Author: User

C ++ implements heavy-load ostream and heavy-load ostream

#include <iostream>using namespace std;class Point{public:Point(int _x = 0, int _y = 0, int _z = 0):x(_x), y(_y), z(_z){}Point(){}~Point(){}friend ostream& operator<<(ostream &os, const Point &pd);private:int x;int y;int z;};ostream& operator<<(ostream &os, const Point &pd){os << pd.x<<" "<<pd.y<<" "<<pd.z;return os;} int main() {Point pd(1,2,3);cout<<pd;return 0;}
The main note is to define the function as a friend function of the class Point outside the class. In addition, pay attention to the return OS to achieve the chain function.
Heavy Load of C ++ into ostream class

Ostream has a char * overload,
Const char * and const void *. Other pointers are output by const void *

In c ++, the operator (\ "<\") reloads ostream

Operator Overloading is an extension of the existing operators used to customize user types, but the specific nature cannot be changed. For example, <indicates that the object on the left of the stream operator cannot be changed as the essence of the stream, + The essence of the two-element operator cannot be changed. For example, for the-> overload operator, the pointer type must be returned and cannot be changed!
That is, even if operators are overloaded, they are limited!
Friend ostream & operator <(ostream & OS, const CPolynomial &)
Operator is a keyword for operator overloading. It is followed by an overloaded operator, with a stream on the left and a stream processing object on the right. In this way, you can process how to input or output the object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.