zoukankan      html  css  js  c++  java

    一组具有相同属性和行为的对象的抽象

    每个对象都是唯一的:

      对象的唯一来源于真实世界中事物的唯一

      程序中理解为内存地址的唯一

    访问修饰符:protected , public , private

    • 构造函数:

    与类同名

    无返回值!

    未定义构造函数调用默认的无参构造函数 , 一旦定义就不会调用默认的无参构造函数

    定义对象时自动调用构造函数

    构造函数和虚构函数必须是public

    • 析构函数:

    删除类内指针成员 指向的堆区的空间

    一个类中只有一个析构函数,没有参数,没有返回值

    类中会有默认的析构函数

    对象生命周期结束,对应的内存空间被系统回收或被程序用delete删除时,析构函数自动被调用

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 
     5 
     6 class CPerson
     7 {
     8 public:
     9     CPerson()
    10     {
    11         cout << "CPerson" << endl;
    12     }
    13     ~CPerson()  
    14     {
    15         cout << "~CPerson" << endl;
    16     }
    17 };
    18 
    19 //CPerson ps;    //  全局对象   生命周期  程序结束(主函数结束) 大括号时候
    20 
    21 int main()
    22 {
    23 
    24     //------------在  栈区对象---生命周期---{}作用域----------------------------
    25     {
    26         CPerson ps;    //如果不加括号,则在主函数return 0;时候
    27     }
    28     //括号结束调用析构函数
    29     //---------------------------------------------------------------------------
    30 
    31     //  --------------在 堆区的对象 生命周期-遇到delete------------------------------
    32     {
    33         CPerson* ps = new CPerson;
    34         delete ps;//若不调用delete则主函数结束也不会释放该空间
    35         ps = 0;
    36     }
    37     //  -----------------------------------------------------------------------------
    38 
    39     //--------------------临时对象-----生命周期当前这一行-------------------
    40     cout << "================================" << endl;
    41     CPerson();
    42     cout << "================================" << endl;
    43     //---------------------------------------------------
    44 
    45     // malloc free    new delete 区别 new delete可以触发构造析构
    46     CPerson* pp = (CPerson*)malloc(sizeof(CPerson));
    47     free(pp);
    48 
    49 
    50     system("pause");
    51     return 0;
    52 }

    可以逐个注释掉不同区域的对象来实验,观察其生命周期,什么时候调用析构函数

    •  this
    类中普通的成员函数  有一个 隐藏的指针,  当前这个类的  叫 this
    调用的类成员  默认的 都 this调用的    //  完成封装
    空类的大小  是1个字节   占位的   
    类中的普通成员变量  是在定义对象的时候  分配空间   
    类中的成员函数      编译的时候放到代码区
    静态成员函数没有this指针
    因为类只有一个静态成员函数实例,所以使用this指针没有什么意义。在静态成员函数中使用this指针会引起编译错误
    • const
     1 #include <iostream>
     2 using namespace std;
     3 
     4 class CPerson 
     5 {
     6 public:
     7     const int m_nSex;    //  const 成员 一定要在初始化列表中初始化
     8     int m_nAge;    
     9 public:
    10     CPerson():m_nSex(123) 
    11     {
    12     }
    13     void Show(/*   CPerson* this =  CPerson*    */)
    14     {
    15         cout << this->m_nSex << endl;
    16     }
    17     void ShowShow(/*   const CPerson* this = const CPerson*  */) const   //  常函数  不能修改类中的 成员属性   因为 this  已经变量成  const
    18     {
    19         //this->m_nAge = 300;
    20         cout << this->m_nAge << endl;
    21     }
    22 };
    23 
    24 
    25 int main()
    26 {
    27     const CPerson ps; 
    28     //ps.Show(/* const CPerson*  */); //  常量对象  不能用  普通函数
    29     ps.ShowShow(/* const CPerson*  */);
    30 
    31 
    32     system("pause");
    33     return 0;
    34 }
     
  • 相关阅读:
    Firemonkey 控件设定字型属性及颜色
    ListView 使用 LiveBindings 显示超过 200 条记录
    Firemonkey ListView 获取项目右方「>」(Accessory) 事件
    XE7 Update 1 选 iOS 8.1 SDK 发布 iPhone 3GS 实机测试
    Firemonkey Bitmap 设定像素颜色 Pixel
    Firemonkey 移动平台 Form 显示使用 ShowModal 范例
    XE7 提交 App(iOS 8)提示「does not contain the correct beta entitlement」问题修复
    XE7 Android 中使用 MessageDlg 范例
    导出 XE6 预设 Android Style (*.style) 档案
    修正 Memo 設定為 ReadOnly 後, 無法有複製的功能
  • 原文地址:https://www.cnblogs.com/Lune-Qiu/p/7890098.html
Copyright © 2011-2022 走看看