What is Procedural, Event Driven and Object Orientated Programming

Procedural Programming –

Procedural programming is a programming paradigm that follows a step-by-step approach to solving problems, using procedures or routines (also called functions or subroutines). It emphasizes the concept of procedure calls, where a program is divided into smaller sections, each performing a specific task. Procedural programming is characterized by the use of variables, loops, conditionals, and function calls to structure the program.

Here are some key features of procedural programming:

  1. Procedures/Functions: Reusable blocks of code that perform a specific task.
  2. Sequential Execution: Code is executed line-by-line in a sequential manner.
  3. Local and Global Variables: Variables can have a scope limited to the function or be accessible throughout the program.
  4. Control Structures: Use of loops (for, while), conditionals (if, switch) to control the flow of the program.
  5. Modularity: Breaking down a program into smaller, manageable procedures.

Characteristics of Procedural Programming:

  1. Readable and Maintainable: By breaking down a program into functions, the code becomes more organized and easier to understand and maintain.
  2. Reusability: Functions can be reused in different parts of the program, reducing redundancy.
  3. Modularity: Different functions can be developed and tested independently.

Procedural programming is well-suited for applications where tasks can be clearly defined and broken down into smaller, reusable procedures. It forms the foundation for many programming languages and is often used for teaching basic programming concepts.

using System;

class Program
{
    // Function declaration
    static void Greet()
    {
        Console.WriteLine("Hello, World!");
    }

    static int Add(int a, int b)
    {
        return a + b;
    }

    static void Main(string[] args)
    {
        // Function call
        Greet();

        int sum = Add(5, 3);
        Console.WriteLine("Sum: " + sum);
    }
}

In this C# example:

  1. Greet: This is a function that prints “Hello, World!” to the console.
  2. Add: This is a function that takes two integers as parameters, adds them, and returns the result.
  3. Main: This is the entry point of the program where the Greet and Add functions are called.

Explanation:

  1. Function Declaration and Definition: Functions Greet and Add are declared and defined outside the Main function but within the same class Program.
    1. Greet prints a message to the console.
    2. Add takes two integer parameters and returns their sum.
  2. Function Call: In the Main function, Greet is called to print the greeting message. Then, Add is called with the arguments 5 and 3, and the result is stored in the variable sum, which is then printed to the console.

Event Driven Programming –

Event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or message passing from other programs or threads. In event-driven programming, the program listens for events and responds accordingly, often through the use of callback functions or event handlers.

Key Concepts in Event-Driven Programming

  1. Event Source: The object or system that generates the event (e.g., a button click).
  2. Event Listener: The object or method that waits for and reacts to an event.
  3. Event Handler: The function or method that executes in response to an event.

Let’s consider a simple example in C# using a Windows Forms application. In this example, we’ll create a form with a button. When the button is clicked, a message box will be displayed.

Open Visual Studio. Create a new project and select “Windows Forms App (.NET Framework)”.  Name your project and click “Create”.

Design the Form:

In the form designer, drag a Button control onto the form.
Set the button’s properties, such as its name (e.g., button1) and text (e.g., “Click Me”).
Write the Event Handler Code:

Double-click the button in the designer. This will automatically create an event handler method in the code-behind file (e.g., Form1.cs).
Here is the complete code for Form1.cs:

using System;
using System.Windows.Forms;

namespace EventDrivenExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // Event handler for button click event
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button was clicked!");
        }
    }
}

Run the Application:

  1. Press F5 to run the application.
  2. Click the button, and you will see a message box displaying “Button was clicked!”.

Explanation

  1. Event Source: The button (button1) acts as the event source.
  2. Event Listener: The button1.Click event is the listener that waits for the button click event.
  3. Event Handler: The button1_Click method is the event handler that gets called when the button is clicked.

Detailed Breakdown

  1. InitializeComponent: This method is called in the constructor of the form to initialize all components and controls on the form.
  2. button1_Click: This method is automatically created when you double-click the button in the designer. It is registered as the event handler for the button’s Click event.
  3. MessageBox.Show: This method displays a message box with the specified text. It is called inside the button1_Click event handler, so it executes when the button is clicked.Key Points

Event-driven architecture: The program flow is controlled by events (e.g., button click).

  1. Separation of concerns: The user interface logic is separated from the event handling logic, making the code more modular and maintainable.
  2. User interaction: Event-driven programming is commonly used in graphical user interfaces (GUIs) where user interactions trigger specific actions.
  3. This example demonstrates how event-driven programming works in C#, with a focus on handling a button click event in a Windows Forms application.

Object Orientated Programming – OOP

Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data in the form of fields (often known as attributes or properties), and code in the form of procedures (often known as methods). OOP aims to implement real-world entities like inheritance, polymorphism, encapsulation, and abstraction in programming. The main principles of OOP are:

  1. Encapsulation: Bundling the data and the methods that operate on the data into a single unit or class, and restricting access to some of the object’s components.
  2. Inheritance: A mechanism by which one class (child or derived class) can inherit the properties and methods of another class (parent or base class).
  3. Polymorphism: The ability of different classes to be treated as instances of the same class through inheritance. It allows methods to do different things based on the object it is acting upon.
  4. Abstraction: The concept of hiding the complex implementation details and showing only the necessary features of the object.

Step-by-Step Example

  1. Define the Base Class (Book):
    • This class will encapsulate the properties and methods common to all books.
using System;

namespace OOPExample
{
    public class Book
    {
        // Encapsulation: private fields with public properties
        private string title;
        private string author;
        private int year;

        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        public string Author
        {
            get { return author; }
            set { author = value; }
        }

        public int Year
        {
            get { return year; }
            set { year = value; }
        }

        // Constructor
        public Book(string title, string author, int year)
        {
            this.title = title;
            this.author = author;
            this.year = year;
        }

        // Method to display book details
        public virtual void DisplayInfo()
        {
            Console.WriteLine($"Title: {title}, Author: {author}, Year: {year}");
        }
    }
}
  1. Define a Derived Class (EBook):
    • This class will inherit from Book and add additional properties and methods specific to e-books.
using System;

namespace OOPExample
{
    public class EBook : Book
    {
        // Additional property for EBook
        public string FileFormat { get; set; }

        // Constructor
        public EBook(string title, string author, int year, string fileFormat)
            : base(title, author, year)
        {
            this.FileFormat = fileFormat;
        }

        // Overriding the DisplayInfo method
        public override void DisplayInfo()
        {
            base.DisplayInfo();
            Console.WriteLine($"File Format: {FileFormat}");
        }
    }
}
  1. Main Program to Use the Classes:
using System;

namespace OOPExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creating an instance of Book
            Book book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925);
            book1.DisplayInfo();

            // Creating an instance of EBook
            EBook ebook1 = new EBook("1984", "George Orwell", 1949, "PDF");
            ebook1.DisplayInfo();

            Console.ReadLine();
        }
    }
}

Explanation

  • Encapsulation:
    • The Book class has private fields (title, author, year) and public properties to access and modify these fields.
    • The methods DisplayInfo encapsulate the logic for displaying the details of a book.
  • Inheritance:
    • The EBook class inherits from the Book class using the : base syntax in its constructor.
    • EBook adds an additional property FileFormat.
  • Polymorphism:
    • The DisplayInfo method in the EBook class overrides the DisplayInfo method in the Book class using the override keyword. This demonstrates polymorphism, allowing the DisplayInfo method to behave differently based on the object type.
  • Abstraction:
    • The details of how the DisplayInfo method works are abstracted away. Users of the Book and EBook classes just call DisplayInfo without needing to know its implementation details.



Comments are closed.