zoukankan      html  css  js  c++  java
  • 动态建立并释放对象

    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    class Box
    {
        int height;
        int width;
        int length;
    public:
        Box();
        Box(int h, int w, int l) :height(h), width(w), length(l) {}
        int volume();
        ~Box();
    };


    Box::Box()
    {
        height = 10;
        length = 10;
        width = 10;
    }

    int Box::volume()
    {
        return (height*width*length);
    }

    Box::~Box()
    {
        cout << "Destructor is called" << endl;
    }

    int main()
    {
        Box *pbox1 = new Box; //定义指向Box堆对象的指针pbox1
        cout << "The volume of box1 is:" << pbox1->volume() << endl;
        delete  pbox1;   //释放pbox1指向的对象空间
        Box *pbox2 = new Box(12, 24, 36);
        cout << "The voiume of box2 is:" << pbox2->volume() << endl;
        delete pbox2;
        system("pause");
        return 0;
    }

    图像 1

  • 相关阅读:
    Spark介绍与环境搭建
    Kafka基本操作
    Hadoop的HDFS概述
    hadoop环境搭建
    常用小工具
    mac机
    Eclipse使用
    微信公众号开发
    PM2
    JS 零散知识点
  • 原文地址:https://www.cnblogs.com/summercloud/p/5531307.html
Copyright © 2011-2022 走看看