OOPs: The Key to Writing Better Code

·

5 min read

OOPs: The Key to Writing Better Code

Object-Oriented Programming (OOP) is a powerful programming concept that revolutionized the way we design, develop, and maintain software systems. By focusing on the organization of code into objects and classes, OOP provides a modular and reusable approach to software development. It is mainly to increase the readability, manageability and extensibility of the code.

Mainly OOP's concept is based on objects and classes. classes are user-defined data types where as the object is an instance of the class. Some data types which we know are int for integers, double for decimal, and char for symbols.

Object And Class

Declaration of Class in Cpp

Declaration of Class

Object creation in Cpp

Creating an Object of type Hero

Classes define the structure and behavior of the object. Classes serve as templates from which objects are instantiated. If the class is empty 1 byte of memory is allocated to the object created. One more important point to note is that object occupies space whereas a class does not occupy space since it is only a templet.

Access Modifiers

These are the keywords used to specify the accessibility of classes, methods, and variables within a program. They control the visibility and availability of these elements to other parts of the program. There are three commonly used access modifiers: public, private, and protected. By default class is private.

If the access modifier is public, it means that those elements which are defined as public can be accessed anywhere in the program.

declaring variable health as public in class Hero

Elements that are defined as private inside the class can only be accessed inside the class.

If a variable is private and still we want to access it then we can take the help of public functions to access them.

The protected access modifier allows access to the elements within the same class, subclasses (classes that inherit from the parent class), and other classes in the same package. It provides a level of accessibility between private and public.

Constructor and Destructor

Constructor and destructor are special member functions in object-oriented programming languages like C++ that are used during the creation and destruction of objects.

They play a crucial role in initializing and cleaning up the resources associated with an object. They are automatically called during the creation of the object. The default constructor and destructor will be provided by the compiler.

Constructor:

It is automatically called when an object of a class is created. It sets the initial state of the object.

constructed will be called during the creation of object

there is no return type for the constructor

there are three types of constructors they are default, parameterized and copy-constructor.

If we create any constructor, then the constructor which is provided by the compiler will vanish.

Destructor:

is automatically called when an object is destroyed or goes out of scope. It is used to release resources, deallocate memory, or perform any necessary cleanup operations before the object is removed from memory. Destructors have the same name as the class, preceded by a tilde (~), and no return type.

Main Components of OOPs

There are mainly 4 components of OOPs which are also called as 4 pillars of OOPs they are Encapsulation, Inheritance, Polymorphism and Abstraction.

1) Encapsulation:

It is also called Information hiding or data hiding. Because of this security increases. this also leads to code reusability. In the case of Encapsulation, all the variables are initialized in private and get and set methods are used to access them.

the dot operator is used to access the elements inside the class.

2) Inheritance:

It is one of the pillars of OOPs in which the child class inherits the properties of the parent class. It reduces the amount of code that we write.

For example, there is a class named Human and there are two classes named Male and Female, now these two classes can inherit the properties of Human. in this case, the parent class will be Human and the child class will be Male and Female.

There are types of inheritance:

  • Single inheritance

  • Multi-Level inheritance

  • Multiple inheritance

  • Hybrid inheritance

  • Hirarichal inheritance

3) Polymorphism:

polymorphism allows an object to take on different forms or behaviors. It means that even though objects may have different underlying implementations, we can treat them uniformly using a common interface or base class.

There are two main types of polymorphism

  1. Run time Polymorphism:

  2. Compile time Polymorphism:

Polymorphism allows for code flexibility, extensibility, and reusability. It allows us to write generic code that can work with different types of objects, making our programs more modular and adaptable to changes. By treating objects of different classes as objects of a common base class, we can write more flexible and maintainable code.

  • Compile time Polymorphism is also known as method overloading and function overloading. It occurs when multiple functions or methods have the same name but different parameter lists.

  • Run time Polymorphism is also known as method overriding and late binding. It occurs when a function or method in a child class overrides a function or method in its parent class.

4) Abstraction:

It is similar to encapsulation but here implementation hiding takes place, that is hiding unnecessary details and exposing only essential features to the outside world.

Advantages of OOPs:

  • Promotes code reuse, by decreasing redundancy.

  • Code can be easily created, handled and maintained easily.

  • Can hide data that is unnecessary

  • It is a bottom-up approach

  • Polymorphism offers a lot of flexibility.