0% found this document useful (0 votes)
14 views

Introduction To Object

Assignment

Uploaded by

Taskeen Zafar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Introduction To Object

Assignment

Uploaded by

Taskeen Zafar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design


applications and computer programs. Objects can be considered as instances of classes, which
are blueprints for creating objects. OOP is fundamental in modern programming and provides a
structured approach to software development. The primary principles of OOP are encapsulation,
inheritance, polymorphism, and abstraction. These principles help in creating modular, reusable,
and maintainable code.

Encapsulation involves bundling data with the methods that operate on that data, restricting
direct access to some of the object's components. This helps in protecting the integrity of the data
and prevents unintended interference and misuse.

Inheritance allows a new class to inherit properties and behavior (methods) from an existing
class. This promotes code reuse and can lead to an improved hierarchical structure within your
codebase.

Polymorphism enables objects to be treated as instances of their parent class rather than their
actual class. This is particularly useful for designing flexible and scalable systems.

Abstraction simplifies complex systems by modeling classes appropriate to the problem and
working at the most relevant level of inheritance for a particular aspect of the problem.

To understand these principles in a practical context, let’s design and implement a simple library
management system using OOP principles.

Designing a Simple Library Management System

A library management system typically handles tasks like managing books, issuing books to
users, and keeping track of book returns. Let's break down the requirements and identify the
classes needed for this system.

1. Classes and Objects:


o Book: Represents a book in the library.
o LibraryMember: Represents a member of the library.
o Library: Manages books and library members.
2. Attributes and Methods:
o Book:
 Attributes: title, author, isbn, status (e.g., available, issued)
 Methods: __init__(), get_status(), set_status()
o LibraryMember:
 Attributes: member_id, name, borrowed_books (a list of books borrowed
by the member)
 Methods: __init__(), borrow_book(), return_book()
o Library:
 Attributes: books (a list of Book objects), members (a list of
LibraryMember objects)
 Methods: __init__(), add_book(), add_member(), issue_book(),
return_book()

Implementing the Library Management System

Book Class

python
Copy code
class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
self.status = 'available'

def get_status(self):
return self.status

def set_status(self, status):


self.status = status

The Book class encapsulates the data related to a book and provides methods to access and
modify the book's status.

LibraryMember Class

python
Copy code
class LibraryMember:
def __init__(self, member_id, name):
self.member_id = member_id
self.name = name
self.borrowed_books = []

def borrow_book(self, book):


if book.get_status() == 'available':
book.set_status('issued')
self.borrowed_books.append(book)
print(f"Book '{book.title}' borrowed by {self.name}")
else:
print(f"Book '{book.title}' is not available")

def return_book(self, book):


if book in self.borrowed_books:
book.set_status('available')
self.borrowed_books.remove(book)
print(f"Book '{book.title}' returned by {self.name}")
else:
print(f"Book '{book.title}' was not borrowed by {self.name}")
The LibraryMember class represents a member of the library. It allows members to borrow and
return books, updating the book's status accordingly.

Library Class

python
Copy code
class Library:
def __init__(self):
self.books = []
self.members = []

def add_book(self, book):


self.books.append(book)
print(f"Book '{book.title}' added to the library")

def add_member(self, member):


self.members.append(member)
print(f"Member '{member.name}' added to the library")

def issue_book(self, book_isbn, member_id):


book = next((b for b in self.books if b.isbn == book_isbn), None)
member = next((m for m in self.members if m.member_id == member_id),
None)
if book and member:
member.borrow_book(book)
else:
print("Book or Member not found")

def return_book(self, book_isbn, member_id):


book = next((b for b in self.books if b.isbn == book_isbn), None)
member = next((m for m in self.members if m.member_id == member_id),
None)
if book and member:
member.return_book(book)
else:
print("Book or Member not found")

The Library class manages the collection of books and members. It provides methods to add
books and members, issue books to members, and process returns.

Using the Library Management System

Let's create some books, members, and a library to demonstrate how this system works.

python
Copy code
# Create books
book1 = Book('The Great Gatsby', 'F. Scott Fitzgerald', '1234567890')
book2 = Book('1984', 'George Orwell', '1234567891')

# Create members
member1 = LibraryMember(1, 'Alice')
member2 = LibraryMember(2, 'Bob')

# Create a library
library = Library()

# Add books and members to the library


library.add_book(book1)
library.add_book(book2)
library.add_member(member1)
library.add_member(member2)

# Issue and return books


library.issue_book('1234567890', 1)
library.return_book('1234567890', 1)

In this example, we instantiate books and members and add them to the library. We then
demonstrate issuing and returning a book using the Library class's methods.

Conclusion

Object-Oriented Programming (OOP) provides a powerful way to model real-world problems


and design modular, maintainable software. By encapsulating data and functionality within
classes and using principles like inheritance and polymorphism, developers can create systems
that are both flexible and scalable. The library management system example illustrates how OOP
principles can be applied to design a simple yet effective solution to manage books and library
members. This approach can be extended and scaled to handle more complex requirements,
demonstrating the power and versatility of OOP.

You might also like