UnicMinds

Arrays vs Classes

Arrays vs. Classes

Why do we need Classes, why not just use Arrays and work things out?

A lot of kids ask this question: why do we need classes, why not just use arrays and put all data together in one array or in a couple of arrays and access? It is understandable why kids are asking this question. They’ve used arrays for various requirements before in programming and they see that it can store a set of data and hence they think everything can be done using this structure.

In the real-world of programming, it is often required to group together dissimilar types of data. For example, to store the data records of a book, you’ll need Author Name (string data or character array), Title (string data or character array), Number of Pages (integer data), ISBN number (13 digit number – long int datatype), and Price (float data).

Arrays are used to group together similar types of data. So, we cannot use arrays for the above use-case. For example, you can have an array of integers or an array of characters. But, you cannot have an array where the first few elements are integers and the next few elements are characters, and the next few elements are float data-type. You may say, I will have one array of integers for Number of Pages with a delimiter and another array of characters for Title with a delimiter and another array of floats with price data again with a delimiter. Firstly, it is very cumbersome because you have to store all data in the same index order. Imagine, if you have to retrieve the price data of the book of a particular author (say third author), it is very cumbersome as you have to remember the index of the author or count it each time whether it is the third or fourth or fifth in order in the arrays and then traverse the array and count delimiters and retrieve it. Now, imagine if you have to change the Author Name and Price to something else. You get the point it becomes almost impossible and inefficient to work like this as you have to keep checking the index and then doing the same across other arrays. This is not only cumbersome but also very error-prone.

Hence, classes are required. Classes are the best way to put different types of data-types together. And classes also have member functions that can operate on the data-types enclosed together in a class. The variables of a class are called Objects and they become the center of Object-Oriented Programming languages such as C++, Python and Java. In defining classes, the programmer or coder doesn’t need to bind the author name with the price and pages data. The class itself, as a behavior, does that for you. You are not worried anymore to map this author name to this particular data. So, there is no confusion that can arise.

Private vs. Public

The data present within a class can be qualified as either private or public (to keep it simple). When you say public, it means that data is accessible to the outside world directly. When we say the data is private, the data can be accessible only through its own member functions.

Sample class StudentRecords

For example, below is a class StudentRecords with a private variable age and a public variable rollNumber. We make an object s1 of this class StudentRecords. Object s1 can access its rollNumber directly by saying “s1.rollNumber” but its age cannot be accessed directly in the main function (outside the class). Please refer to the below simple example for the same.

#include <iostream>

class StudentRecords {

public:

    int rollNumber;

    int setAge(int num);

private:

    int age;

};

int StudentRecords::setAge(int num) {

    age = num;

    return age;

}

int main () { 

//When you make an object of the above class as below

StudentRecords s1;

s1.rollNumber = 1; // you can access this s1.rollNumber because rollNumber is public

//Whereas to set age value for this roll number, you have to use the function setAge as Age variable is //private and can only be accessed through member functions.

//You cannot set age for s1 student by doing the below:

//s1.age = 12; Because ‘age’ variable is private and only class’s own functions can access

int ageofthechild = s1.setAge(12);

std::cout << “Age of rollNumber”<<s1.rollNumber<<” is:” << ageofthechild;

return 0;

}

Hope this useful, we’ll learn more details about Object-oriented programming, Classes, Objects, Data-types and Scope in some of the future blog posts.

Join our courses in OOPs programming languages like Python, C++, C#, and Java to learn some exciting fun stuff and publish your own apps and games.

Learn more about what is coding for kids, and is coding relevant for non-STEM subjects.

BOOK A FREE TRIAL