Why Classes and Objects?

Today, we see a lot of developers around the world working on classes and objects. Maybe you have done projects using this concept. But why actually do we need classes and objects??Is there any alternative?? If yes, what is the drawback that led to this concept?? Let's see

In the early days, people used to use primitive data types. Now what is a primitive data type?? To put it in simple words, a primitive data type is a basic building block provided by a programming language which cannot be broken down further. These enable the programmers and developers to develop the logic required to finish a task.

Example of primitive data types in C/C++ are int, char, bool, float, double etc.

As the time progressed, people started solving more complicated tasks and this demanded more complicated data types to accomplish a task. Then came the need for user defined data types. This could be solved using structures.

What is a structure? A structure is a user defined data type. It is used to combine several data types into a single data type. These data types can be simple data types(primitive data types or maybe other structures).The keyword used to create structures is struct. Here is a simple example of a movie structure

struct movie
{
int id;
string name;
string cast[20];
float rating;
//other data types as per requirement
};

But structure has a drawback. We cannot declare/define functions with a structure. This means we may create a data type but we cannot add the functionality within a structure. This calls for the need of classes.

Class is a user defined data type which contains its own data and functions.

After we create a data type we should start using it right?? This is where objects come in.

An object is an instance of a class. Here we start using the functionality(data members and member functions) which is initially provided by the class.

This is a brief idea on why we use classes and objects.

Keep learning :)