Oop Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

OBJECT ORIENTED PROGRAMING

C++ What is OOP?

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or functions that perform operations on the
data, while object-oriented programming is about creating objects that contain both data and
functions.

Object-oriented programming has several advantages over procedural programming:

 OOP is faster and easier to execute


 OOP provides a clear structure for the programs
 OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier
to maintain, modify and debug
 OOP makes it possible to create full reusable applications with less code and shorter
development time

Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code. You
should extract out the codes that are common for the application, and place them at a single place
and reuse them instead of repeating it.

C++ What are Classes and Objects?

Classes and objects are the two main aspects of object-oriented programming.

Look at the following illustration to see the difference between class and objects:
So, a class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the variables and functions from the class.

Unraveling Object-Oriented Programming: Evolution of OOP

Object-Oriented Programming (OOP) has revolutionized the way we design and develop software,
promoting modularity, reusability, and maintainability.

Let’s take a journey through the history and evolution of OOP,

Early Beginnings: Simula and Smalltalk

The roots of OOP can be traced back to the 1960s, with the development of the Simula language.
Simula introduced foundational concepts such as classes, objects, and inheritance, laying the
groundwork for OOP. However, the creation of Smalltalk at Xerox PARC in the 1970s truly
advanced the paradigm. Smalltalk emphasized encapsulation and polymorphism and pioneered the
graphical user interface (GUI).

Growing Popularity: C++ and Object Pascal

In the 1980s, two influential languages emerged: C++ and Object Pascal. C++, created by Bjarne
Stroustrup, built upon the concepts of Simula and added new features, making it a powerful and
versatile language. Object Pascal, developed by Apple, brought OOP to the Apple Macintosh
platform, expanding its reach and popularity.

Mainstream Adoption: Java and C#


The 1990s witnessed the rise of two widely adopted languages: Java and C#. Java, created by
James Gosling at Sun Microsystems, gained immense popularity due to its platform independence
and robust features. C#, developed by Anders Hejlsberg at Microsoft, combined elements of C++
and Java, offering a familiar syntax for Windows-based development. Both languages played a
pivotal role in mainstreaming OOP and shaping the software industry.

Modern Advancements: Python, Ruby, and More

In the 2000s, dynamic and interpreted languages gained traction. With its simplicity and versatility,
Python became a favorite among developers, offering an intuitive OOP experience. Ruby, created
by Yukihiro “Matz” Matsumoto, introduced innovative concepts like blocks, mixing, and a concise
syntax. Additionally, languages like PHP, JavaScript, and Swift embraced OOP principles, further
solidifying its dominance in software development.

Present and Beyond Continuous Evolution

Today, OOP continues to evolve and adapt to meet the changing needs of software development.
Modern languages and frameworks build upon the foundations laid by their predecessors, offering
improved syntax, performance, and flexibility. Concepts like functional programming and hybrid
paradigms are influencing the future of OOP, leading to more expressive and efficient code.

Throughout its history, OOP has transformed how we design, develop, and maintain software
systems. It has empowered developers to create modular, reusable, and scalable solutions,
enhancing productivity and facilitating innovation. As we move forward, OOP remains a
cornerstone of software engineering, shaping how we build applications and driving the evolution
of technology.

Benefits of Using OOP in Software Development

 Improved Design and Analysis: OOP facilitates better design and analysis of software
systems. Developers can create more accurate and effective software solutions by modelling
real-world entities.

 Enhanced Collaboration: OOP promotes teamwork and collaboration. With clear modular
structures, multiple developers can work on different parts of the application simultaneously.
 Code Reusability and Reduction of Redundancy: By leveraging inheritance and
polymorphism, OOP allows developers to create reusable code, reducing redundancy and
improving efficiency.

 Easier Troubleshooting and Maintenance: Encapsulation and modular design in OOP make
it easier to troubleshoot and maintain code. Problems can be isolated to specific objects,
simplifying the debugging process.

 Greater Flexibility and Scalability: OOP’s flexibility and scalability make it suitable for both
small projects and large, complex systems. It allows for incremental development and
adaptation to changing requirements.

Classes and Objects: Understanding the Fundamentals

In Object-Oriented Programming (OOP), classes and objects are fundamental concepts that form
the building blocks of our programs. Think of classes as blueprints and objects as instances or
implementations of those blueprints. Let’s explore this concept using real-world analogies and code
examples in Python and Java.

Classes: The Blueprint

Think of a class as a blueprint for creating objects. Just as a blueprint defines the structure and
properties of a building, a class defines the attributes (variables) and methods (functions) that
objects of that class will possess. In other words, a class is a template for creating objects with
similar characteristics and behaviors.

Real-World Analogy: Imagine you’re an architect designing a house. You create a blueprint that
specifies attributes like the number of rooms, windows, and doors, as well as methods or actions
like opening doors, turning on lights, and adjusting the thermostat. This blueprint serves as a
template for constructing multiple houses with similar features.
Objects: Instances of the Blueprint

Objects are instances or implementations of a class. They are the tangible entities that bring the
class blueprint to life. Each object created from a class inherits its attributes and methods but can
have unique values for those attributes.
Real-World Analogy: Going back to the house analogy, think of each house constructed based on
the blueprint as an object. Each house has the same attributes specified in the blueprint, such as
rooms, windows, and doors. However, each house can have unique values for those attributes,
such as the color of the walls, the type of furniture, or the temperature setting on the thermostat.

Examples of OOP Languages

C++, java, ruby, C#,

Object-Oriented Databases (OODBMS)

Object-Oriented Databases (OODBMS) are databases that store data in the form of objects,
similar to how object-oriented programming (OOP) languages like Java, C++, and Python
structure data. Unlike traditional relational databases, which organize data into tables of rows and
columns, OODBMS use the principles of OOP to allow for more complex data structures and
relationships, often used for applications requiring high levels of abstraction, such as CAD
systems, multimedia databases, and AI systems.

Key Features of Object-Oriented Databases

1. Object-Oriented Structure:
o The database stores data as objects, similar to how objects are structured in OOP.
These objects contain both data (attributes) and methods (functions that operate on
the data).
o Data in an OODBMS is represented in the same way as in the application's code,
making it easier for developers to work with complex data.
2. Encapsulation:
o Encapsulation allows objects to hide their internal data and expose only the
necessary parts (via public methods). This makes the system more modular and
secure.
3. Inheritance:
o Classes in an OODBMS can inherit properties and behaviors from parent classes,
similar to OOP principles. This allows for data models that represent hierarchical
relationships.
o For instance, if there is a Person class, a Student class can inherit properties (like
name, age) from the Person class while adding new attributes (like student ID,
grade).
4. Polymorphism:
o Polymorphism in OODBMS allows different classes to respond to the same method
call in different ways. This is beneficial for maintaining flexibility and reusability
in database designs.
5. Complex Data Types:
o Unlike relational databases, which rely on atomic (simple) data types like integers,
strings, and floats, OODBMS can store complex data types such as lists, sets,
arrays, and even other objects.
o This is useful in applications where objects can be nested, such as storing a
Customer object that contains multiple Order objects.
6. Data Persistence:
o Object-oriented databases manage the persistence of objects. Once an object is
created, it remains in the database until explicitly deleted. Persistent objects outlive
the application that created them.
7. Support for Object Identity (OID):
o Every object in an OODBMS has a unique Object Identifier (OID) that
distinguishes it from other objects, regardless of its attributes. This is analogous to
primary keys in relational databases but is managed automatically by the system.
8. Relationships Between Objects:
o Relationships between objects, such as associations or dependencies, can be
directly represented in the database, just like associations between classes in object-
oriented programming.
o The database can manage complex relationships (e.g., one-to-many, many-to-
many) more naturally than relational databases.
9. Inheritance and Class Hierarchies:
o In an OODBMS, classes can be arranged into hierarchies, allowing inheritance of
attributes and methods from a superclass to subclasses. This simplifies the
management of data models with similar structures.
10. Query Language:

 OODBMS typically support Object Query Language (OQL), which is similar to SQL but
tailored for working with objects rather than tables.
 OQL can query object attributes and methods, supporting both simple and complex queries.

Advantages of Object-Oriented Databases

1. Seamless Integration with Object-Oriented Programming Languages:


o Since OODBMS store data in objects, it provides a direct mapping between
application objects and database objects, eliminating the need for Object-Relational
Mapping (ORM) tools.
o This makes OODBMS ideal for applications developed in OOP languages.
2. Efficient Handling of Complex Data:
o Object-oriented databases are well-suited for applications that involve complex
data models, such as CAD, multimedia systems, and simulations, where objects can
be deeply nested or have complex relationships.
3. Improved Productivity:
o Developers can use the same object model in both the application and the database,
which leads to more intuitive design and better code reuse.
o In relational databases, developers often have to transform the objects into a table
structure, leading to the complexity of maintaining two separate models.
4. Better Support for Versioning and Long Transactions:
o OODBMS provide better support for versioning (tracking changes over time) and
long transactions, which are crucial in environments like CAD, engineering, or
software development, where multiple versions of objects need to be tracked.
5. Inheritance and Reusability:
o Through inheritance, common properties can be shared across objects, reducing
redundancy and enabling better organization of the data.
6. Extensibility:
o Since objects can be extended via inheritance, OODBMS systems are highly
extensible, allowing new data types or features to be added without major
redesigns.

Disadvantages of Object-Oriented Databases

1. Complexity:
o OODBMS can be more complex to design, especially when dealing with highly
nested objects or objects with many interrelationships.
o The learning curve for OODBMS is higher compared to traditional relational
databases.
2. Lack of Standardization:
o While SQL is a standardized query language for relational databases, object-
oriented databases lack a universally adopted standard. Different OODBMS
systems may have their own query languages or APIs, which can limit portability
and flexibility.
3. Performance:
o For simple, flat data models, OODBMS can be less performant than relational
databases due to the overhead associated with managing objects and relationships.
4. Limited Tooling and Ecosystem:
o Compared to relational databases, OODBMS have fewer third-party tools for
reporting, analytics, and administration, making it harder to integrate them into
certain enterprise workflows.
5. Smaller Market Adoption:
o Relational databases dominate the market, and OODBMS are typically used in
niche applications. This can make finding developers, consultants, or
administrators familiar with OODBMS more difficult.

When to Use Object-Oriented Databases

1. Complex Data Relationships:


o OODBMS is a good choice for applications that have complex, interconnected data.
For instance, multimedia systems, simulation systems, and CAD/CAM applications
can benefit from the natural representation of objects and their relationships.
2. Real-Time Systems:
o Systems that require real-time data processing, such as telecommunications and
avionics systems, often benefit from the object-oriented approach due to its
flexibility and direct modeling of real-world entities.
3. Multimedia and Hypertext Databases:
o Applications that handle multimedia content (images, video, audio) or hypertext
databases (e.g., web content management systems) can benefit from OODBMS, as
these types of data are better represented as objects than in relational models.
4. Scientific Applications:
o Scientific databases, simulations, and artificial intelligence (AI) systems often
require highly complex data structures that are difficult to represent in a relational
model but fit well in an OODBMS.

Examples of Object-Oriented Databases

1. db4o:
o A popular open-source object database for Java and .NET developers. It provides
seamless integration with the object model of the language, eliminating the need
for ORM tools.
2. ObjectDB:
o An object-oriented database designed for Java developers using JPA and JVM-
based languages. It’s optimized for high performance and large-scale applications.
3. GemStone/S:
o An advanced object-oriented database designed for Smalltalk. It supports object
persistence, transactional integrity, and scaling for distributed systems.
4. Versant Object Database:
o A highly scalable and high-performance OODBMS that is often used in
telecommunications, engineering, and other domains where complex data
relationships are needed.
5. ZODB:
o A Python-based object-oriented database that is often used in web applications built
with the Zope application server. It stores Python objects natively, making it a good
fit for applications with complex data structures.

You might also like