PadhaikoAll subjects

Recent Trends in Programming

Computer Programming (ENCT 101)

Open in the Padhaiko app

10.1 Introduction to object oriented programming (OOP)

As software systems grew to millions of lines of code, Procedure-Oriented Programming (POP) (such as classic C) exposed structural limitations: global data could be modified inadvertently by any function, and data structures were separated from the functions operating on them.

Object-Oriented Programming (OOP) is a programming paradigm built around modeling real-world entities as self-contained objects that bundle data (attributes) and behavior (methods) together into cohesive units.

Four Core Pillars of OOP:

  1. Encapsulation: Binding data and the methods that manipulate it inside a single unit (class) while restricting direct external access via access specifiers (private, protected, public).
  2. Abstraction: Exposing only essential external interfaces while hiding complex internal implementation details from the user.
  3. Inheritance: Allowing a new (derived / child) class to reuse and extend attributes and behaviors from an existing (base / parent) class, forming hierarchical "is-a" relationships.
  4. Polymorphism: Enabling a single interface, function name, or operator to take multiple forms depending on the object or parameter types (compile-time overloading and runtime method overriding).

10.2 Definitions of class, method and object in OOP

Definition

Class, Object, and Method

Class, Object, and Method Illustration (C++)C++
#include <iostream>
using namespace std;

class Rectangle {               // Class (blueprint)
private:
    double length, width;       // Encapsulated attributes
public:
    void setDimensions(double l, double w) {   // Method
        length = l;
        width = w;
    }
    double area() {             // Method
        return length * width;
    }
};

int main() {
    Rectangle r1;               // Object (instance of Rectangle)
    r1.setDimensions(5.0, 3.0);
    cout << "Area = " << r1.area() << endl;
    return 0;
}

10.3 Difference between procedure oriented and OOP

AspectProcedure-Oriented Programming (POP)Object-Oriented Programming (OOP)
Primary FocusFocuses on functions/procedures (algorithms and step-by-step tasks)Focuses on objects and data modeling real-world entities
Design ApproachTop-down decomposition (main problem split into smaller functions)Bottom-up design (build independent classes/objects, then compose them)
Data SecurityData moves freely across functions; global data lacks access protectionStrong data hiding (private/protected); data accessed safely through public methods
Code Reuse & ExtensibilityReuse mainly via calling functions or copying codeReuse and extension via inheritance, composition, and polymorphism
Communication ModelFunctions call other functions passing dataObjects communicate by invoking methods (message passing) on each other
Representative LanguagesC, Pascal, FORTRAN, COBOLC++, Java, C#, Python, Kotlin

10.4 Overview of other high level programming languages

While C remains unmatched for low-level systems and embedded development, modern software engineering spans web backends, mobile apps, data science, and cloud services—domains where higher-level languages trade raw hardware control for developer productivity, memory safety, and rich ecosystems.

Overview of Major Modern High-Level Programming Languages
LanguageParadigm(s)Execution ModelKey StrengthsPrimary Application Domains
C++Multi-paradigm (OOP, procedural, generic)Compiled to native machine codeZero-overhead abstractions, STL, backward compatible with CGame engines, browsers, high-frequency trading, systems software
JavaObject-oriented, strongly typedCompiled to JVM bytecode (JIT execution)"Write Once, Run Anywhere" portability, automatic garbage collectionEnterprise backends, banking systems, Android apps (legacy/core)
PythonMulti-paradigm (OOP, procedural, functional)Interpreted / bytecode VMConcise readable syntax, vast scientific/AI ecosystemData science, machine learning, scripting, web APIs, automation
JavaScript / TypeScriptEvent-driven, functional, prototype OOPJIT-compiled in browser / Node.jsNative language of the web browser; asynchronous I/OFrontend web UIs, full-stack web apps, cross-platform apps
C#Object-oriented, component-oriented.NET CLR bytecode (JIT / AOT)Strong tooling, LINQ, async/awaitWindows desktop, enterprise web services, Unity game development
Go / RustSystems / concurrent (Go: GC; Rust: ownership)Compiled to native binariesGo: simple concurrency (goroutines); Rust: compile-time memory safety without GCCloud infrastructure, microservices, OS kernels, CLI tools

Worked Examples

Example 1

1 (6 marks)

Suppose you are tasked with building a Bank Account Management System with balances, deposits, and withdrawals. Explain how a Procedure-Oriented (C) design differs from an Object-Oriented design, showing how encapsulation prevents invalid negative balances.

Procedure-Oriented (C) Design: In C, you define a struct BankAccount { int accNo; double balance; }; and standalone functions deposit(struct BankAccount *a, double amt) and withdraw(struct BankAccount *a, double amt). Because struct members are public everywhere, any unrelated function in the program can accidentally write acc.balance = -5000; directly, bypassing validation.

Object-Oriented (OOP) Design: In OOP, balance is declared private inside class BankAccount, while deposit() and withdraw() are public methods:

  • External code cannot assign acc.balance = -5000; (the compiler rejects direct access).
  • Every modification must pass through withdraw(amt), which checks if (amt > 0 && amt <= balance) before updating balance.
Answer:

In POP, data (struct) and functions are separate with no built-in access restriction, risking accidental corruption of balance. In OOP, encapsulation bundles balance and its validation methods inside a BankAccount class with private data protection, guaranteeing valid state transitions.

Example 2

2 (5 marks)

Identify the class, attributes, methods, and objects in a rectangle modeling problem (length = 5, width = 3), and show how to calculate area and perimeter in OOP style compared with procedural C functions.

Mapping OOP Elements:

  • Class: Rectangle (the blueprint).
  • Attributes (Data Members): length, width (kept private).
  • Methods: area() (returns length * width = 5 * 3 = 15) and perimeter() (returns 2 * (length + width) = 2 * 8 = 16).
  • Objects: rect1 (with length = 5, width = 3), rect2 (a separate instance with its own dimensions).

Comparison of Invocation:

  • Procedural C: area(rect1) passes the data structure into a standalone function.
  • OOP: rect1.area() sends a message to object rect1 to compute its own area using its internal state.
Answer:

Rectangle is the class, length and width are attributes, area()/perimeter() are methods, and rect1 is an object yielding area = 15 and perimeter = 16.

Example 3

3 (5 marks)

A past exam question asks: "Recommend an appropriate high-level programming language (from C, C++, Python, Java, JavaScript) for each of three projects—(a) an embedded microcontroller sensor firmware, (b) a machine learning data-analysis pipeline, and (c) an interactive browser web application—and justify each choice."

Matching Problem Requirements to Language Strengths:

  1. (a) Embedded Microcontroller Firmware -> C: Minimal memory footprint, deterministic execution without garbage-collection pauses, and direct register/bit-level hardware access.
  2. (b) Machine Learning Pipeline -> Python: Rapid prototyping with expressive syntax and mature numerical/ML libraries (NumPy, pandas, scikit-learn, PyTorch).
  3. (c) Interactive Browser Web App -> JavaScript (or TypeScript): Runs natively inside every web browser's engine, manipulates the HTML DOM directly, and handles asynchronous user/network events cleanly.
Answer:

Choose C for embedded firmware (hardware control, minimal RAM/CPU overhead), Python for ML/data pipelines (readability and scientific libraries), and JavaScript/TypeScript for browser web apps (native browser execution and DOM/event support).


Exercises

Exercises — Recent Trends in Programming

Question 1

What is Object-Oriented Programming (OOP)? Briefly explain its four fundamental principles: encapsulation, abstraction, inheritance, and polymorphism.

OOP Overview and Four Pillars: OOP models software around objects combining data and behavior.

  1. Encapsulation: Wrapping data and methods together in a class and hiding internal data (private) from direct external tampering.
  2. Abstraction: Showing only relevant operations while hiding underlying implementation complexity.
  3. Inheritance: Deriving a new subclass from an existing base class to reuse and extend attributes/methods.
  4. Polymorphism: Allowing one interface or method name to exhibit different behaviors depending on the object or argument types.
Answer:

OOP organizes programs around objects bundling data and behavior, built on encapsulation (data hiding + bundling), abstraction (hiding complexity), inheritance (code reuse via class hierarchies), and polymorphism (one interface, multiple behaviors).

Question 2

Define class, object, and method in OOP, and explain how a class differs from a struct in C.

Definitions:

  • Class: Abstract blueprint specifying attributes and methods; allocates no instance memory on its own.
  • Object: Concrete runtime instance of a class holding actual attribute values in memory.
  • Method: Function defined inside a class representing an object's behavior.

Class vs. C struct: A C struct contains only data fields (all publicly accessible by default) and cannot contain member functions directly. An OOP class encapsulates both data and methods together and enforces access control (private, protected, public).

Answer:

A class is a blueprint, an object is a concrete memory instance of a class, and a method is a class-bound function. Unlike a C struct (data-only, public by default), an OOP class bundles both data and methods with access protection.

Question 3

List five major differences between Procedure-Oriented Programming (POP) and Object-Oriented Programming (OOP).

Five Key Differences:

  1. Focus: POP centers on procedures/functions; OOP centers on objects/data.
  2. Design Direction: POP follows top-down design; OOP follows bottom-up design.
  3. Data Security: POP exposes global data without access modifiers; OOP protects data using encapsulation (private/protected).
  4. Extensibility/Reuse: POP reuses functions; OOP adds inheritance and polymorphism.
  5. Examples: POP -> C, FORTRAN; OOP -> C++, Java, Python.
Answer:

POP focuses on functions with top-down design and open data (e.g., C), whereas OOP focuses on objects with bottom-up design, private data hiding, inheritance, and polymorphism (e.g., C++, Java).

Question 4

Give an overview of C++, Java, and Python, comparing their execution models (compiled vs. bytecode/VM vs. interpreted), memory management, and primary use cases.

Comparing C++, Java, and Python:

  • C++: Compiled directly to native machine code; manual/RAII memory management (new/delete, smart pointers); used for game engines, OS components, and high-performance systems.
  • Java: Compiled to platform-independent JVM bytecode (executed by JIT); automatic garbage collection; used in enterprise web backends, financial systems, and Android.
  • Python: Bytecode-interpreted dynamically typed language; automatic garbage collection; used in data science, AI/ML, automation, and rapid web development.
Answer:

C++ compiles to native binaries with manual/RAII memory control for high-performance systems; Java compiles to JVM bytecode with garbage collection for portable enterprise apps; Python is interpreted with garbage collection for rapid development, scripting, and AI/data science.

Question 5

Why is C still taught as the first programming course in engineering despite the popularity of Python and Java? State three reasons.

Three Engineering Reasons for Teaching C First:

  1. Hardware and Memory Transparency: Teaching pointers, stack vs. heap, arrays, and data sizes reveals how computers actually store and manipulate data in RAM.
  2. Foundation for Systems Courses: Later courses in Computer Architecture, Operating Systems, Microprocessors, Embedded Systems, and Compilers rely directly on C concepts.
  3. Syntactic Ancestor: Mastering C's syntax, control flow, and type discipline makes learning C++, Java, C#, and JavaScript straightforward.
Answer:

C teaches explicit memory and pointer mechanics, directly prepares engineers for Operating Systems and Embedded Systems, and provides the core syntax and mental model shared by C++, Java, and modern systems languages.

← File Management