Introduction to OOPs

Vineet RajVineet Raj
5 min read

Introduction

Object-Oriented Programming (OOP) is a programming paradigm (a style of writing code) based on the concept of objects which can contain data and code. The data is represented as fields (often called attributes or properties), and the code is represented as procedures (often called methods). Objects are instances of classes, which act as blueprints for creating objects.

It majorly consists of two things:

  • Class: A class is a blueprint or template that defines the properties (attributes) and behaviors (methods) common to all objects of its type.

  • Object: An object is an instance of a class, representing a specific entity with its own unique state (attribute values) and behavior.

Difference between procedural and Object oriented Programming language

LabelProceduralObject Oriented
ApproachFollows a step by step sequenceIt does not have a particular flow control as here we define various methods attributes in an object representing real life entities
Data handlingif data declared once it’s globally available so may cause confusionhere Data is encapsulated within a object and it’s access is mostly restricted through methods
Code ReusabilityLimited. while we can reuse functions no concepts of inheritance or polymorphism is therevery high as inheritance is introduced and polymorphism
ScalabilityHard to scale as adding new functionality means changing old codeVery high we can just inherit old class or just add one new method in old class
ModularityVery low. we can modularize functions but functions logic is not properly structuredVery high as we can create seprate classes for seperate features so it is properly structured
Real-World ModelingNot aligned with real world systemsvery aligned with real world systems

Use-Cases of Object Oriented Programming

Modularity : The process of breaking down a complex problem into smaller, manageable, and reusable components (such as classes), enhancing code organization and maintainability.

Code Reusability: Refers to the ability to extend and reuse existing functionality, reducing the need to duplicate code and promoting maintainability.
Example:
Vehicle class extended by Car and Bike*.*

Scalability: Refers to the ability to effortlessly add new features or functionality without modifying existing code, ensuring the system can grow and adapt without disruption.

Security: Using OOP, users can protect sensitive data by encapsulating it within objects and exposing only the necessary functionality through controlled access methods, ensuring data integrity and security.
Example: private balance in a
Bank Account class.

» Due to above mentioned use-cases OOPs is better for large scale application

Classes

» class is the blueprint or template for creating an object.

It defines the set of attributes(data) and methods(function) that an object will have after being created from certain class

class Employee { 
private:
    int salary; // to store the salary of employee

public:
    string employeeName; // to store the name of employee

    // Method to set the employee name
    void setName(string s) { 
        employeeName = s;
    }

    // Method to set the salary
    void setSalary(int val) { // method
        salary = val;
    }

    // Method to get the salary
    int getSalary() {
        return salary;
    }
};

Objects

» It is the instance of certain class that holds data for attributes and method provided by that class

for above Employee class , object may look like

int main() {
    // Creating an object of Employee class
    Employee obj1;

    // Setting different attributes of object 1 using available methods
    obj1.setName("Raj"); // Set name to "Raj"
    obj1.setSalary(10000); // Set salary to 10,000

    // Creating another object of Employee class
    Employee obj2;

    // Setting different attributes of object 2 in a similar way
    obj2.setName("Rahul"); // Set name to "Rahul"
    obj2.setSalary(15000); // Set salary to 15,000

    // Accessing the attributes of different objects
    cout << "Salary of " << obj1.employeeName << " is " << obj1.getSalary() << endl;
    cout << "Salary of " << obj2.employeeName << " is " << obj2.getSalary() << endl;

    return 0;
}

/*
Output : 
Salary of Raj is 10000
Salary of Rahul is 15000
*/

Note :

  • Class does not hold memory

  • Each object created by a class holds separate allocated memory

Attributes

» Inside an object attributes are something that holds data or you can say characteristics of that particular object

Behaviours:

» also called methods or function

It exists only to manipulate or change data for attributes of an object

Constructor

» A special method in a class to initialize an object when it is created

Constructor name must math the name of class

» If there is no constructor written for the given class, the language by-default triggers the default constructor*.*

Purpose of Constructor

  • Object Initialization

  • Code Reusability

  • Ensures Default value

Types of constructor

Non-parameterized Constructor : When a constructor does not take any arguments as the input, it is called a Non-parameterized Constructor.

Parameterized Constructor : It is a type of constructor that accepts arguments to initialize attributes with specific values

Copy Constructor : It enables the programmer to create a new object by copying the attributes of an existing object. Java doesn't have an explicit copy constructor like C++ does. However, a copy constructor can be implemented manually by creating a constructor that takes an object of the same class as a parameter and copies its attributes using Constructor Chaining. Here's an example:

class Employee {
public:
    string employeeName; // To store the name of the employee
    int salary;          // To store the salary of the employee

    // Parameterized constructor
    Employee(string name, int salary) {
        this->employeeName = name;
        this->salary = salary;
    }

    // Copy Constructor
    Employee(const Employee &employee) {
        // Calling another constructor
        this->employeeName = employee.employeeName;
        this->salary = employee.salary;
    }
};

// Main Class
int main() {
    /* Creating an object of Employee class and passing 
    values for the parameterized constructor */
    Employee obj("Raj", 10000);

    // Creating a copy of obj using Copy constructor
    Employee objCopy(obj);

    // Printing the attibutes of copied object
    cout << "Name of the copied employee: " << objCopy.employeeName << endl;
    cout << "Salary of the copied employee: " << objCopy.salary << endl;

    return 0;
}

» In java a constructor can also call other constructor with constructor chaining using this() keyword

» There also exist a concept called constructor overloading, that means creating multiple constructor with different number of arguments

0
Subscribe to my newsletter

Read articles from Vineet Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vineet Raj
Vineet Raj