Why we need a class in OOPS?

Let's quickly jump into the topic.

Procedural Programming 

It is a list of instruction in a single block, and suitable for small program. When the need of customer increases, the software becomes more and more complex. Hence, it is difficult to use procedural programming approach. 

Modular Programming 

We will divide the program into small chunks, and each of these chunks are called program. Each function has a clear purpose. Programmers find difficulty with modular programming for real world problems. Modular approach is also getting older, and it doesn't have facility to relate the things to the outer world.  

In modular programming, data remains alive within module, so we need some data to be global. There are two different types of variables: Local and Global. 


Global variable has its own disadvantage as they remain till the end of the program execution. It can lead out of memory issues.

For example, ATM application has its own functions such as withdrawal(), check_balance(), generate_PIN(), and mini_statement(). Global variables are balance, PIN, and name. 


Each of these functions are related to few of the global variables. In large project, it is difficult to track which function uses which global data/variable as the data and functions are separated. So, the modular programming is difficult to conceptualize and difficult to modify. If we modify, we need to modify all the functions which use the global data. If the variables are used as locally, it can't be accessed outside the module. 

Problem with modular Programming

So, the problem is the separation of both the functions and data. To remove this mess, binding is needed. 

To solve this problem, object oriented programming has the concept as class where variables and functions are bound together. 

Class is a collection of variables and functions, and it is a Blueprint for creating objects. Object is relatable to the real-world object. Moreover, object is created with the help of a class. 


Each customer can have their own object which includes their name, PIN, and balance.With Object Oriented Programming, the mess created between functions and global data, is resolved. 

With this class, member functions are able to access the member data easily in order to perform their tasks.  

There is also no need to pass the variables (PIN, balance) separately as arguments in the function as these are bound with member functions.  This is a sign that we need to combine things into class.

Multiple definitions of a Class

A class is a blueprint for creating objects (a particular data structure), providing initial values for state(member variables or attributes), and implementations of behavior(member functions or methods). 

A class is a template definition of methods and variables in a particular kind of object. 

A class is a combination of member variable and member functions. 

It provides template for creating objects, which can bind code into data. 

Class is a user-defined data type which holds its own data members and member functions. 

Facts about Class 

Class doesn't occupy any space in RAM, and it is the object which occupies the memory in the RAM. 

class army 
{
    int number_suffix;
    string key;
    
    void enrollment(string name, int age)
    {key = name; number_suffix = age;}
    
    void retirement(string name)
    {cout << key;}
};

The process of binding member variable and member functions is called Encapsulation.

References: ByteBoard

Comments