4 Major Fundamental concepts of programming:
In programming, Data Structures are simply a particular way of storing and organising data so that it can be used effectively in the code. A real life example of applying data structures would be organising your bookshelf. There are a bunch of books on your shelf, and that shelf of books keeps growing every week. If you were to try and represent all those books in a computer program, there is an inefficient way and efficient way. Using the bookshelf example, let’s say we need to organise 10 books.
This example above is a poor way to trying to store 10 different book variables because of two major reasons.
- The amount of extra code you will need to write in the program. We might only have 10 books right now, but if we have 100 books, forget about declaring 100 book variables.
- It’s not flexible because if we need to add in another book, we have to manually edit the code and enter it in.
So the most efficient way is to use a data structure.
In this scenario, we have a list of books to organise and with C++, there is a data structure called a class. So how would that look like? Here’s an easy sample:
Alright so what’s so great about using a class? We can organise the variables to be either public, protected or private when they are declared. You can easily add and remove variables from a class without any major headaches. With the books example, it’s very easy to create a new book object in the code. Just like this:
There we go, we’ve just created book objects with the help of the Book Class and set the title and author. The inefficient way would have been that you had to create 10 unique string variables (string book1, book2, book3) but with the effective approach, we have created a book class with two public members. Because we only had to create one class with two members, we can now easily add in the title and author when we add in a new book.
That is the key with applying data structures. We want it to be able to handle a variety of situations without having to enter in more code. To be simple, a data structure is just a way to get around having to create several variables.
Data structures are applied daily in object orientated programming, it enables programmers to efficiently organise code without having to declare several variables and allow the program to handle a variety of situations.