Introduction to OOP and array class implementation [part 1]

Image retrieved from https://pediaa.com/what-is-the-difference-between-1d-and-2d-array/

Object-oriented programming (OOP) is an important knowledge taught in schools and programming classes. It is a concept that relies on the creation of classes and objects which helps coding more efficient with reusable code blocks of well-defined functions. Today, I will show you how to implement a simple 1D array class. Throughout the post, important OOP terminologies and concepts will be discussed for the sake of better understanding.

Firstly, I will talk about the four basic properties of OOP, namely encapsulation, inheritance, polymorphism, and abstraction. Encapsulation is a property which helps to hide data implementation with three levels of protection in descending order, namely, private, protected, and public. Elements within the public field can be accessed from outside the class. While elements within private field cannot. This prevents data from being altered. For example, a developer accidentally assigns a gilberish value to his pre-defined array. Altering data, however, can be achieved through the accessor methods, which are commonly known as setters and getters.

int view_na();
void set_na(int);
int getElement(int index);
void setElement(int index, int value);

Before continuing, I would like to set up the coding files for the sake of readability, debugging, and enhancement. I divide my class implementation into three files: header (.h extension), class definition, and main. The header file contains only the class prototypes and some helper functions. The class definition file defines all the prototypes declared in the header file. The main file is used for testing.

Next, I will talk about two important OOP concepts, namely, attributes and methods. Attributes are variables declared within a class. Likewise, methods are functions declared within a class. “Within” means belonging to. This means that they must be declared inside a class. Methods, however, can be declared outside a class using a scope resolution operator (::, two colons). This is how the method definitions in the class definition file are connected to their prototypes in the header file.

Now, I will talk about the benefit of dynamic allocation of array. It is said that dynamic allocation of array is better than static allocation of array because it uses the memory more efficiently. By utilizing vacant discontinuous memory spaces and linking them by address than occupying a chuck of continuous spaces, dynamic array will not waste memory if they are not used fully. This is why I have used it as a main class attributes. The other attributes are the maximum number of elements of the array and the number of elements actually persists in the array. A method I always have is deepcopy, which is going to be used for the copy constructor and copy assignment.

Regarding deepcopy, it is a method that creates a separate copy of an array, which is different with shallow copy that only points to the address of the original array. This also prevents data from being mistakenly altered.

I am going to talk about the default constructor. The default constructor is an initialization method of a class. It is automatically called when a class instance is created whether or not a developer defines it. Most of the time, I define it. Because without definition, it won’t work in practice. For example, a dynamic array works only if it is initialized with a maximum number of elements.

void allocate(int *&a, int size)
{
    a = new int[size];
}

Array::Array()
{
    allocate(this->A, 1);
    this->na = 0;
    this->capacity_a = 1;
}

In this example, I have a helper function which initializes a dynamic array with a size.

Bao Ca Ca

HCM 18.03.22 8:59 p.m.

If you want the code, please check out my Github. Thanks!

array.h

array_class.cpp

array_main.cpp

4 bình luận cho “Introduction to OOP and array class implementation [part 1]”

Bình luận về bài viết này

Tạo trang giống vầy với WordPress.com
Tham gia