Project with multiple files

In this section we'll learn about C++ app development comprising of multiple files. We'll develop a class, called Vector, which implements a dynamic array. This class is similar to the std::vector class offered by Standard Template Library (STL) and has a very limited set of features compared to STL class.

Create a new project and name it App2. Navigate to File | New | File… menu option and then choose C/C++ header option and follow the wizard to add a new file to App2 project. Add the following code in a new file under App2 and name it vector.h file:

#ifndef __VECTOR_H__
#define __VECTOR_H__

#ifndef DATA_TYPE
#define DATA_TYPE double
#endif

class Vector {
public:
    Vector(size_t size = 2);
    virtual ~Vector();

    size_t GetCount() const;

    bool Set(size_t id, DATA_TYPE data);
    DATA_TYPE operator[] (size_t id);

private:
    DATA_TYPE* m_data;
    size_t     m_size;
};

#endif //__VECTOR_H__

Header file vector.h declares the Vector class structure. We have a pre-processor macro DATA_TYPE that defines the data type that this class holds. We have a constructor (with a default parameter) and a destructor function. These functions will allocate and de-allocate a pointer m_data that holds array of elements. A member variable m_size will be used to hold size of array that will assist us in bound-checking.

There are several member functions that operate on the member variables. The GetCount() function returns number array size, Set() function assigns a value to an element in array. An operator [] has been overloaded to access array data.

The Vector class has been implemented in the vector.cpp file. Create and add this new file to App2 project and then copy the following code to it:

#include <cstring>
#include "vector.h"

Vector::Vector(size_t size)
    : m_size(size)
{
    m_data = new DATA_TYPE[m_size];
    ::memset(m_data, 0, m_size * sizeof(DATA_TYPE));
}

Vector::~Vector() {
    if (m_data) {
        delete [] m_data;
        m_data = 0;
    }
}

size_t Vector::GetCount() const {
    return m_size;
}

bool Vector::Set(size_t id, DATA_TYPE data) {
    if (id < m_size) {
        m_data[id] = data;
        return true;
    }
    return false;
}

DATA_TYPE Vector::operator[](size_t id) {
    if (id < m_size) {
        return *(m_data + id);
    }

    return 0;
}

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

The line m_size(size) defines an initializer list. where member variables are initialized as per the order they have been declared. We have used new operator to allocate an array of size given by user. The memset() function initializes that array with zeroes. In destructor internal array is checked for null pointer and then de-allocated with delete [] keyword and assigned a null pointer.

Note

Null pointers have a value (typically 0) that is reserved to indicate that it doesn't point to any valid object. Any operation on null pointers will lead to a segmentation fault or access violation. In such a case, app will die instantly. C++ 11 defines a separate nullptr constant to define a null pointer.

There are two member functions, Set() and GetCount() that operate on the internal array.

Finally, replace code inside the main.cpp file with the following code. It creates an object of Vector class and subsequently uses it:

#include <iostream>
#include "vector.h"

int main() {
    Vector vec(4);
    vec.Set(0, 10); // Set first item = 10
    vec.Set(2, 55); // Set first item = 55
    std::cout << "Number of elements = " << vec.GetCount() << std::endl;
    std::cout << "vec[1] = " << vec[1] << std::endl;
    std::cout << "vec[2] = " << vec[2] << std::endl;
    return 0;
}

Now, the Management window will look similar to the following screenshot:

Project with multiple files

We'll define a pre-processor define to ensure that the Vector class is compiled as an array of integers. Navigate to Project | Build options… menu option and the Project build options window will be presented:

Project with multiple files

As we intend to apply the settings throughout the project click on the root of the project tree in that window. Now, click on the Compiler settings | #defines tab and add the line as per the preceding screenshot. Further, click on the OK button to close that dialog box. Now compile and run this project. This will produce result as per the following screenshot:

Project with multiple files

In our code we have a pre-processor macro DATA_TYPE that defines the data type that this class holds. If we intend to use it as an array of double we have to recompile this app.

Do note that pre-processor macros work by simple text substitution and no type checking is performed on them during substitution. This can introduce other bugs in the program if it is used incorrectly.

In this section we learned about app development with multiple files, tweaking of compiler options.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.167.183