How to implement an IP address Class in C++
Most people do not understand the knowledge points of this article "how to achieve an IP address class in C++", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve an IP address class in C++" article.
[project-IP address Class]
The IP address used in the Internet occupies 4 bytes and can be expressed as a four-segment method. The range of values for each segment is 0-255. the middle is "." Separate, for example, 202.194.116.97. In fact, you can also look at an unsigned integer value of 3401741409 with 4 bytes.
Now design an IP address class to save the IP address and implement some operations on the IP address. As follows:
Class IP {private: union / / as can be seen from the anonymous consortium, IP addresses occupy a total of 4 bytes {struct / / this is a 4-byte anonymous structure {unsigned char seg0; unsigned char seg1; unsigned char seg2; unsigned char seg3;}; / / 4-byte IP addresses can be regarded as four parts, each part 1 byte unsigned int address / 4-byte IP address can be regarded as a 4-byte whole}; public: IP (int=0,int=0,int=0,int=0); / / constructor void showIP (); / / display IP address bool sameSubnet (const IP & ip, const IP & mark) by four-segment method; / / determine whether it is in the same subnet char whatKind (); / / return to which type of network} / / implement member functions int main () {IP ip1 (202, 194, 116, 97), ip2 (202, 194119102), mark (255, 255, 248, and 0); cout