C ++ input/output overload (converting IP to binary)

Source: Internet
Author: User

# Include <iostream. h> // The code is very simple. You only need to use this header file in VC 6.0. Otherwise, it cannot be compiled.

// # Include <iostream>

// Using namespace std;

Class IP
{
Public:
IP (int I = 0, int j = 0, int k = 0, int m = 0)
{
I =;
J = B;
K = c;
M = d;
}
Friend istream & operator> (istream & input, IP & h );
Friend ostream & operator <(ostream & output, IP & h );
Void print ()
{
Printt ();
Cout <".";
Printt (B );
Cout <".";
Printt (c );
Cout <".";
Printt (d );
Cout <'/N ';
}
Void printt (int)
{
Int B = 128;
For (int I = 1; I <= 8; I ++) // convert it to an eight-bit binary number.
{
Cout <a/B;
A = a % B;
B = B/2;
}
}
Private:
Int;
Int B;
Int c;
Int d;
};

Istream & operator> (istream & input, IP & h)
{
Input> h.a;
Input. ignore (); // read "."
Input> h. B;
Input. ignore (); // read "."
Input> h.c;
Input. ignore (); // read "."
Input> h. d;

Return input;
}
Ostream & operator <(ostream & output, IP & h)
{
H. print (); // call the print () function. You can also directly call the member output in the class.
Return output;
}


Int main ()
{
IP address;
Cin> ip address;
Cout <ip;
Return 0;
}

 

Running result:

 

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.