第6週目プロジェクト3-IPアドレスクラス

2059 ワード

/*
 *Copyright(c) 2016,              
 *All rights reserved.
 *    :my.cpp
 *      :   
 *    :2016 4 13 
 *     :v1.0
 *
 *    :        IP   4  ,        ,       0-255,   “.”  ,  202.194.116.97。  ,        4                     。 
              IP   ,    IP  ,    IP        。    :

 *    :
 *    :
 */

#include <iostream>

using namespace std;
class IP

{
    private:
    union//           ,IP    4   
    {
        struct   //     4           
       {

        unsigned char seg0;
        unsigned char seg1;
        unsigned char seg2;
        unsigned char seg3;
       };
    };//4   IP      4  ,      
      unsigned int address;//4   IP         4     
public:
IP(int s0 =0,int s1 =0,int s2=0,int s3= 0 ):seg0(s0),seg1(s1),seg2(s2),seg3(s3){};//    
void showIP();//      Ip  
bool sameSubnet(const IP &ip,const IP &mark);//          
char whatKind();//         
};
void IP::showIP()
{
    cout<<int (seg0)<<'.'<<int (seg1)<<'.'<<int (seg2)<<'.'<<int (seg3)<<endl;
}
bool IP::sameSubnet(const IP &ip,const IP &mark)
{
    if(ip.address==mark.address)
    return true;
    else
    return false;
}
char IP:: whatKind()
{
     if(seg3<128)
        return 'A';
    else if(seg3<192)
        return 'B';
    else if(seg3<224)
        return 'C';
    else if(seg3<240)
        return 'D';
    else
        return 'E';

}

//      
int main()
{


    IP ip1(202,194,116,97),ip2(202,194,119,102),mark(255,255,248,0);
    cout<<"ip1:";
    ip1.showIP();
    cout<<"ip2:";
    ip2.showIP();
    if(ip1.sameSubnet(ip2,mark))
    cout<<"  IP     "<<endl;
    else
    cout<<"  IP      "<<endl;
    cout<<"ip1  "<<ip1.whatKind()<<"   "<<endl;

}
<img src="http://img.blog.csdn.net/20160413201812133" alt="" />