zoukankan      html  css  js  c++  java
  • 对象与变量

    【1】对象与变量

    变量是变量,对象归对象。二者不可混为一谈(许多资料书把两者没有明确区分)。

    个人认为,由类直接创建的称为对象。由内部数据类型定义的称为变量。

    另外,一个对象包含许多成员变量和成员函数。

    下面就对象与变量的对比实现示例代码如下:

      1 #include<iostream>
      2 using namespace std;
      3 
      4 /*
      5  *自定义整型类
      6  */
      7 class Int
      8 {
      9 private:
     10     int num;
     11 
     12 public:
     13     Int(int x = 0):num(x)  //默认复合构造函数
     14     {
     15         cout<<"Create Int: "<<this<<endl;
     16     }
     17     Int(const Int &it):num(it.num)   //拷贝构造函数
     18     {
     19         cout<<"Copy Int object: "<<this<<endl;
     20     }
     21     Int & operator=(const Int &it)  //赋值函数
     22     {
     23         cout<<"operator =  "<<this<<endl;
     24         if(this != &it)
     25         {
     26             num = it.num;
     27         }
     28         cout<<" = "<<endl;
     29         return *this;
     30     }
     31     ~Int()  //析构函数
     32     {
     33         cout<<"Free Int: "<<num<<"  "<<this<<endl;
     34     }
     35 
     36 public:
     37     Int operator++()
     38     //(Int * const this)(隐藏this指针)
     39     {
     40         ++num;
     41         return *this;
     42     }
     43     /*
     44      *方法1实现后置++
     45      */
     46     /*
     47     Int operator++(int)
     48     {
     49          Int tmp = *this;   // Int tmp(*this);
     50          ++num;
     51          return tmp;
     52     }
     53     */
     54     /*
     55      *方法2实现后置++
     56      */
     57     Int operator++(int)
     58     {
     59         return Int(num++);
     60     }
     61     /*
     62      *方法3实现后置++
     63      */
     64     /*
     65     Int operator++(int)
     66     {
     67         Int tmp = *this;
     68         ++*this;//(*this).operator++();this->operator++();
     69         return tmp;
     70     }
     71     */
     72     /*
     73      *方法4实现后置++
     74      */
     75     /*
     76     Int operator++(int)
     77     {
     78        Int tmp=*this;
     79        ++num;
     80        return tmp;
     81     }
     82     */
     83     Int operator+(const Int &it)  //重载运算符+(Int)
     84     //Int operator+(Int * const this, const Int &it)  参数
     85     {
     86         return Int((*this).num + it.num);
     87     }
     88     
     89     Int operator+(const int x)   //重载运算符+(int)
     90     {
     91         return Int(num + x);
     92     }
     93    int operator=(const int x)  //重载赋值运算符=
     94     {
     95         num = x;
     96         return x;
     97     }
     98     bool operator<(int x)  // 重载比较运算符<
     99     {
    100        return num < x;
    101     }
    102     void print() const     //打印
    103     {
    104         cout<<num<<endl;
    105     }
    106 };
    107 //////////////////////////////////////
    108 
    109 void main()
    110 {
    111     //隐式类型转换
    112      Int a = 9, b; 
    113      //构建a对象,隐式类型转换
    114      //构建b对象,复合默认构造函数。所以,对象b中成员变量num=0;
    115      
    116      //对象+变量
    117      int x = 10;
    118      b = a + x;  // == a.operator+(x) == operator(&a, x);
    119      b.print();  //19
    120 
    121      //对象 + 对象
    122      Int c;
    123      c = a + b;   //对象 + 对象
    124      //c = a.operator+(b);
    125      //c = operator(&a,b);
    126      //c.operator = (a.operator+(b));
    127      c.print();   //28
    128 
    129      //前置++
    130      Int i = 100;
    131      ++i;     
    132      //i.operator++();  
    133      //operator++(&i);
    134      Int j = 0;
    135      j = i++;
    136      i.print();  //102
    137      j.print();  //101
    138 
    139 
    140      //对象与变量对比后置++
    141      Int d = 1, e = 0;
    142      e = d++;      
    143      // j=i.operator++(0);
    144      // j.operator=(i.operator++(0));
    145      // j=operator++(&i,0);
    146      d.print(); //2
    147      e.print(); //1
    148      //m,n可以看做变量
    149      int m = 0, n = 0;
    150      m = n++;
    151      cout<<m<<endl; //0
    152 
    153      Int  f= 1,g=1;   //对象
    154      f + g = 12;      //可以通过OK!!!!!!//???????????
    155 
    156     int k = 1, l = 2;
    157 //    k + l = 10;   //error!!不可以通过
    158 
    159     //变量
    160     int h = 10, p = 10;   //变量
    161     ++i = 10;
    162     //i++=10;     //error!!!!!!!!!
    163 
    164     //对象
    165     Int s = 0;  
    166     ++s = 10;
    167     //i.operator++();
    168     //operator++(&i);
    169     i++ = 100;
    170 
    171     //对象操作
    172     Int ai = 0, aj = 10, ak;      
    173     ak = ai + aj;
    174     ak.print();  //10
    175     ai = ai++ +1;
    176     //产生三个对象:
    177     //ai.operator=(ai.operator++(0).operator+(1));
    178     //ai.operator=(operator++(&ai,0).operator+(1));
    179     //operator=(&ai, (operator++(&ai,0).operator+(1)))
    180     // ai ,@1 @2
    181     ai.print();  //1
    182 }

    分析内容见代码具体备注。

    【2】关于变量 i++

    示例代码如下:

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void  main()
     5 {
     6     int a = 4;
     7     a += (a++);
     8     cout<<"a :"<<a<<endl;   //9 = 5 + 4
     9 
    10     int b = 4;
    11     b += (++b);
    12     cout<<"b :"<<b<<endl;   //10 = 5 + 5
    13 
    14     int c = 4;
    15     (++c) += c;
    16     cout<<"c :"<<c<<endl;   //10 = 5 + 5
    17 
    18     int d = 4;  
    19     (++d) += (d++);     
    20     cout<<"d :"<<d<<endl;   //11 = 5 + 6
    21 
    22     int f; 
    23     cout<<"f :"<<(f=d)<<endl;  //11
    24     int m = 5;
    25 
    26     m *= m*m;
    27     cout<<"m :"<<m<<endl;    //125 = 5 * 5 * 5  
    28 
    29     int n = 5;
    30     n *= n*(n++);
    31     cout<<"n :"<<n<<endl;    //126 = 5 * 5 * 5 + 1
    32 
    33     int  h = 5;
    34     h *= (h++)*h;
    35     cout<<"h :"<<h<<endl;    //126 = 5 * 5 * 5 + 1
    36 
    37     int k = 0; 
    38     ++k = h;
    39     cout<<"k :"<<k<<endl;    //126
    40 
    41     int g = 10;
    42     (++g) *= g*g;  
    43     cout<<"g :"<<g<<endl;  //1331 = 11 * 11 * 11
    44 
    45     int s = 10;
    46     s = (++s)*s*s;
    47     cout<<"s :"<<s<<endl;  //1331 = 11 * 11 * 11
    48 }
    49 
    50 /*
    51 a :9
    52 b :10
    53 c :10
    54 d :11
    55 f :11
    56 m :125
    57 n :126
    58 h :126
    59 k :126
    60 g :1331
    61 s :1331
    62 */

    具体结论自己总结。

    Good Good Study, Day Day Up.

    顺序  选择  循环  坚持  总结

    作者:kaizen
    声明:本文版权归作者和博客园共有,欢迎转载。但未经作者同意必须保留此声明,且在文章明显位置给出本文链接,否则保留追究法律责任的权利。
    签名:顺序 选择 循环
  • 相关阅读:
    北京西格玛大厦微软社区精英 Visual Studio 2010 技术交流会记录
    2010522 Windows Phone 开发者日
    SharePoint Server 2010 RTM 安装过程(图)
    Windows HPC Server 2008 R2 简体中文版 下载
    转:Community Clips 使用指南
    关于CS0016: Could not write to output file ‘c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files… ‘Access is denied.’ 的解决办法
    岗位职责
    PowerPoint 2010 的广播幻灯片功能尝试了一下,可以用。
    Silverlight 4 五
    Windows Server AppFabric 简体中文 下载地址
  • 原文地址:https://www.cnblogs.com/Braveliu/p/2850652.html
Copyright © 2011-2022 走看看