Afzal Badshah, PhD

Introduction to Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP), is a way of writing programs by focusing on real-life objects. In the real world, everything we deal with is an object, such as a car, a book, or a student. Each of these objects has certain features and can perform specific actions. In OOP, we try to represent these features as attributes and the actions as functions.

This programming style helps us organize our code in a way that is closer to how we understand things in real life. It makes large programs easier to design, manage, and extend.

Aspect comparison

AspectProcedural ProgrammingObject-Oriented Programming (OOP)
ApproachTop-downBottom-up
StructureProgram is divided into functionsProgram is divided into objects and classes
Data & FunctionsData and functions are separateData and functions are bundled together in objects
ReusabilityLimitedHigh (through inheritance and modularity)
SecurityLess secure (data is exposed)More secure (data hidden using encapsulation)
Real-world ModelingDifficult to model real-world scenariosEasier to model real-world entities and interactions
ExamplesC, PascalJava, C++, Python (with OOP), C#
Function ReuseUses functions repeatedlyUses objects and methods repeatedly
Code MaintenanceHarder to manage and scale in large programsEasier to manage due to modular structure
Main FocusFocuses on functions (actions)Focuses on objects (entities)

What is OOP?

Object-oriented programming is a programming model that allows developers to organize and build programs around real-world concepts. In this model, we define objects that contain both data (attributes) and actions (functions or methods).

The main concepts of OOP are:

Each of these will be explained step-by-step below.

Why Use Object-Oriented Programming (OOP)?

Reusability: Through inheritance, new classes can be created based on existing ones, inheriting their properties and methods. This means you can reuse code from previously developed classes, saving time and effort.

Modularity: OOP breaks down complex problems into smaller, manageable objects. Each object encapsulates its data and behavior, making it easier to understand, modify, and test independently.

Maintainability: Encapsulation helps isolate changes, reducing unintended side effects and making updates safer over time.

Flexibility: Polymorphism allows objects of different classes to be treated uniformly if they share the required interface, increasing adaptability.

Key Concepts in Object-Oriented Programming (OOP)

Class

Class is the template, design or blueprint for creating real-world objects. It defines what attributes and functions an object will have, but it doesn’t store real data itself.

Representation of class in OOP

For example, a Student class can define:

You can then create many student objects from this one class.

Object

An object is a real instance of a class. It is created using the blueprint defined by the class and stores actual data.

Class and its objects in OOP

For example, once you define a Student class, you can create:

// C++ example
#include <string>

class Student {
public:
    std::string name;
    void giveExam() {
        // ...
    }
};

int main() {
    Student s1;
    s1.name = "Ali";
    s1.giveExam();
    return 0;
}

Here, s1 is an object of class Student. Each object can have different values for the same attributes, like different names or roll numbers.

Attributes

Attributes are the properties or characteristics of an object (also called data members). They describe what an object has.

Object attributes in OOP

Attributes are written inside a class and accessed through objects.

For example:

// C++ example
#include <string>

class Book {
public:
    std::string title;
    std::string author;
    int pages;
};

Here, title, author, and pages are attributes. Each book object will have its own values for these.

Functions

Functions (methods) define the actions an object can perform. They are written inside the class and called using the object.

Function of the object in OOP

For example:

// C++ example
#include <iostream>

class Book {
public:
    void read() {
        std::cout << "Reading the book..." << '\n';
    }

    void bookmarkPage() {
        std::cout << "Page bookmarked." << '\n';
    }
};

When we create a Book object, it can call these functions to perform tasks.

Encapsulation

Encapsulation means protecting the internal data of an object by keeping attributes private and accessing them only through special functions (getters and setters).

Encapsulation in OOP

This improves security and keeps the data safe from direct changes.

Example:

// C++ example
class Student {
private:
    int marks;

public:
    int getMarks() const {
        return marks;
    }

    void setMarks(int m) {
        marks = m;
    }
};

Here, marks is private, so it cannot be accessed directly.

Inheritance

Inheritance means creating a new class based on an existing class. The new class (child) can use everything from the old class (parent) and also add its own features.

Inheritance in OOP

Example:

// C++ example
#include <iostream>

class Vehicle {
public:
    void start() {
        std::cout << "Starting..." << '\n';
    }
};

class Car : public Vehicle {
public:
    void playMusic() {
        std::cout << "Playing music..." << '\n';
    }
};

Car is using start() from Vehicle and also has its own function playMusic().

Polymorphism

Polymorphism means one function behaves differently for different classes. It allows the same function name to do different jobs based on the object.

Polymorphism in OOP

Example:

// C++ example
#include <iostream>

class Animal {
public:
    virtual void speak() {
        std::cout << "Animal speaks" << '\n';
    }
    virtual ~Animal() = default;
};

class Dog : public Animal {
public:
    void speak() override {
        std::cout << "Dog barks" << '\n';
    }
};

When we call speak(), the result depends on whether the object is an Animal or Dog.

Task

Take a Book as an example. Now apply OOP concepts:

Exit mobile version