zoukankan      html  css  js  c++  java
  • *** 自写代码:demo一个类的创建

    #include <iostream>
    #include <string.h>
    using namespace std;
    class demo
    {
    public:
        static void showObjCount(void);
    private:
        static int count;
        static const int j = 1;
    public:
        void funcObj(void);
        demo(int x, int y);
        ~demo();
    private:
        int k;
        const int m;
    };
    int demo::count = 0;
    demo::demo(int x, int y):k(x), m(y)
    {
        count++;
        cout << "Constructed: " << count << endl;
    }
    demo::~demo()
    {
        count--;
        cout << "destructed: " << count << endl;
    }
    void demo::showObjCount(void)
    {
        cout << "showObjCount: " << count << endl;
    }
    void demo::funcObj(void)
    {
        cout << "k=" << k << " m=" << m << " count=" <<  count << " j=" << j <<endl;
    }
    int main()
    {
        demo::showObjCount(); // 在不存在任何对象时即可直接调用类的方法
        demo * pObj = new demo(3, 4); // 在堆上创建对象的方式
        pObj->funcObj();      // 对象指针调用对象方法
        pObj->showObjCount(); // note:对象指针也可以调用类的方法
        demo obj(5,6);        // 在栈上创建对象的方式,作用域结束时自动执行析构函数
        obj.showObjCount();   //对象实例调用类方法
        delete pObj;
        return 0;
    }
  • 相关阅读:
    Microsoft office 2013安装图解
    6.2单一继承
    #include <QLabel>
    #include <QDebug>
    9.1运算符重载
    简单QT界面信号图形化输入输出
    类指针引用
    NULL和nullptr的区别
    网易云课堂_C语言程序设计进阶_第8周:图形交互程序
    5.3友元函数
  • 原文地址:https://www.cnblogs.com/superrunner/p/10165150.html
Copyright © 2011-2022 走看看