简体   繁体   中英

How can I use my own made Array header file in my main program c++?

I have created my own header file, this is how we were asked to do it, but what arguments should I use in my main program to call this header file and create an Array.

My header file looks like this:

#ifndef ARRAY_H
#define ARRAY_H

class Array {
public:

    Array(int size) : _size(0), _arr(0) {
        // Call resize to initialize oneself
        resize(size) ;
    }

    Array(const Array& other) : _size(other._size) {
        _arr = new double[other._size] ;

        // Copy elements
        for (int i=0 ; i<_size ; i++) {
            _arr[i] = other._arr[i] ;
        }
    }

    ~Array() {
        delete[] _arr ;
    }

    Array& operator=(const Array& other)
    {
        if (&other==this) return *this ;
        if (_size != other._size) {
            resize(other._size) ;
        }
        for (int i=0 ; i<_size ; i++) {
            _arr[i] = other._arr[i] ;
        }
    }

    double& operator[](int index) {
        return _arr[index] ;
    }
    const double& operator[](int index) const {
        return _arr[index] ;
    }

    int size() const { return _size ; }

    void resize(int newSize) {
        // Allocate new array
        double* newArr = new double[newSize] ;

        // Copy elements
        for (int i=0 ; i<_size ; i++) {
            newArr[i] = _arr[i] ;
        }

        // Delete old array and install new one
        if (_arr) {
            delete[] _arr ;
        }
        _size = newSize ;
        _arr = newArr ;
    }

private:
    int _size ;
    double* _arr ;
} ;

#endif
  1. Do not write implementation of methods in .h file. There are only a few exceptions when writing code in header file is justified and your case is not one. You should move implementations to a cpp file inside your project. Read carefully: Why have header files and .cpp files in C++?
  2. If you want to use your .h file, simply write #include "your-h-filename.h" . You will then be able to use defined classes, variables and functions defined in your .h file.

You might want to read this: http://www.learncpp.com/cpp-tutorial/19-header-files/

In any .cpp file where you want to use this class, assuming "Array.h" is the relative path to the header file from your .cpp file, put the line:

#include "Array.h"

Near the top of the file, before any function or type declarations. This ensures that the code in your array file is treated as if it were written at that point in that .cpp file.

As a further note, you usually want to split the definition of your class methods into a separate .cpp file (presumably Array.cpp). For example, for resize the definition in your header file should be changed to simply:

void resize(int newSize);

and the full definition should be put in the .cpp file:

void Array::resize(int newSize) {
// Allocate new array
double* newArr = new double[newSize] ;

// Copy elements
for (int i=0 ; i<_size ; i++) {
  newArr[i] = _arr[i] ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM