zoukankan      html  css  js  c++  java
  • 类和对象(15)——强化练习(2)自定义的数组类

    //MyArray.h
    
    #pragma once
    #include <iostream>
    using namespace std;
    class MyArray
    {
    public:
        MyArray();
        MyArray(int len);
        MyArray(const MyArray &another);
        ~MyArray();
    
        void setData(int index, int data);
        int getData(int index);
        int getLen();
    
        void operator=(const MyArray& another);
    
    private:
        int len;
        int *space;
    };
    //MyArray.cpp
    
    #include "MyArray.h"
    
    MyArray::MyArray()
    {
        cout << "MyArray()..." << endl;
        this->len = 0;
        this->space = NULL;
    }
    
    MyArray::MyArray(int len)
    {
        if (len <= 0)
        {
            this->len = 0;
            return;
        }
        else
        {
            this->len = len;
            this->space = new int[this->len];
            cout << "MyArray::MyArray(int len)..." << endl;
        }
    }
    
    MyArray::MyArray(const MyArray &another)
    {
        if (another.len >= 0)
        {
            this->len = another.len;
            
            //深拷贝
            this->space = new int[this->len];
            for (int i = 0;i < this->len;i++)
            {
                this->space[i] = another.space[i];
            }
            cout << "MyArray::MyArray(const MyArray &)..." << endl;
        }
    }
    
    void MyArray::setData(int index, int data)
    {
        if (this->space != NULL)
        {
            this->space[index] = data;
        }
    }
    
    int MyArray::getData(int index)
    {
        return this->space[index];
    }
    
    int MyArray::getLen()
    {
        return this->len;
    }
    
    
    MyArray::~MyArray()
    {
        if (this->space != NULL)
        {
            delete[]this->space;
            this->space = NULL;
            len = 0;
            cout << "MyArray::~MyArray()..." << endl;
        }
    }
    
    void MyArray::operator=(const MyArray & another)
    {
        if (another.len >= 0)
        {
            this->len = another.len;
    
            //深拷贝
            this->space = new int[this->len];
            for (int i = 0;i < this->len;i++)
            {
                this->space[i] = another.space[i];
            }
            cout << "MyArray::operator=(const MyArray &)..." << endl;
        }
    }
    //main.cpp
    
    #include <iostream>
    #include "MyArray.h"
    using namespace std;
    
    int main(void)
    {
        MyArray array1(10);//开辟10元素的数组
    
        for (int i=0;i < 10;i++) 
        {
            array1.setData(i, i + 10);
        }
        cout << "----------" << endl;
        cout << "array1:" << endl;
    
        for (int i=0;i < 10;i++)
        {
            cout << array1.getData(i)<<" ";
        }
        cout << endl;
    
        MyArray array2 = array1;
        cout << "array2:" << endl;
        for (int i = 0;i < array2.getLen();i++)
        {
            cout << array2.getData(i) << " ";
        }
        cout << endl;
        
        MyArray  array3;
        array3 = array1;
        cout << "array3:" << endl;
        for (int i = 0;i < array3.getLen();i++)
        {
            cout << array3.getData(i) << " ";
        }
        cout << endl;
    
        return 0;
    }
  • 相关阅读:
    ViewGroup全面分析
    onInterceptTouchEvent和onTouchEvent调用时序 .
    基数树与RCU锁
    dwarf格式解析
    算法一(动态规划)
    IO调度器(二) IO的中断返回
    IO调度器
    借个例子说明sed的模式空间,以及针对模式空间的N,P,D用法
    f2fs中node page的lock_page
    python学习之用正则处理log(持续更新,ftace)
  • 原文地址:https://www.cnblogs.com/yuehouse/p/9812098.html
Copyright © 2011-2022 走看看