Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement an IP address Class in C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report