Unti 1
Unti 1
Unti 1
Class: A blueprint for creating objects. It defines a type and the properties and behaviors that
the objects of this type will have.
public:
int year;
void startEngine() {
};
int main() {
myCar.brand = "Toyota";
myCar.year = 2022;
myCar.startEngine();
return 0;
}
Object-Oriented Thinking
Object-Oriented Thinking (OOT) is a conceptual framework that guides software design and
programming, emphasizing the use of objects to represent real-world entities and their interactions.
This approach is foundational to Object-Oriented Programming (OOP) and promotes a way of thinking
about software development that aligns closely with how we understand and interact with the world
around us.
1. Identify Objects:
o Start by analyzing the problem domain to identify key objects and their
relationships. Think about the entities you need to represent.
2. Define Attributes and Methods:
o For each identified object, define its attributes (data) and methods (functions).
Consider what information the object needs to hold and what actions it can
perform.
3. Establish Relationships:
o Determine how objects interact with one another. Identify associations (e.g., a
Member can borrow a Book), inheritances (e.g., Car is a type of Vehicle), and
compositions (e.g., a Library has many Books).
4. Create a Class Diagram:
o Use Unified Modeling Language (UML) or similar tools to create a visual
representation of the classes, their attributes, methods, and relationships. This
diagram serves as a blueprint for implementation.
5. Implement and Refine:
o Begin coding the classes based on your design. As you implement, be prepared to
refine your objects and their interactions based on testing and new insights.
Differences between Procedural and Object
Oriented Programming
Merits of Object-Oriented Methodology
1. Modularity:
o OO methodology promotes modular design through the use of classes and objects.
Each class can be developed, tested, and maintained independently, which
enhances code organization.
2. Reusability:
o Classes can be reused across different projects. Inheritance allows developers to
create new classes based on existing ones, which reduces duplication of code and
fosters consistent behavior.
3. Encapsulation:
o Data and methods are encapsulated within objects, protecting the internal state of
an object from outside interference and misuse. This leads to better data integrity
and reduces the chances of unintended side effects.
4. Abstraction:
o OO methodology allows for the representation of complex real-world systems
through abstraction, focusing on essential features while hiding unnecessary
details. This simplification helps in understanding and managing complexity.
5. Polymorphism:
o Polymorphism enables objects of different classes to be treated as objects of a
common base class. This flexibility allows for the implementation of generic
algorithms and enhances code extensibility.
6. Easier Maintenance and Debugging:
o The modular nature of OO design makes it easier to locate and fix bugs. Changes
can be made to a single class without affecting the entire system, leading to more
manageable code maintenance.
7. Better Modeling of Real-World Problems:
o OO methodology aligns closely with how we perceive the real world, making it
intuitive to model entities, their attributes, and their interactions.
object model
An object model is a conceptual framework used to describe the structure and behavior of a
system in terms of its objects. It is a fundamental component of object-oriented programming
(Oaching of complexity by organizing systems into discrete, reusable components.
1. Objects:
o Objects are instances of classes that encapsulate data and behavior. Each object
represents a real-world entity or concept and contains attributes (data) and
methods (functions or procedures).
2. Classes:
o A class is a blueprint for creating objects. It defines the attributes and methods
common to all objects of that type. Classes establish the structure of the object
model.
3. Attributes:
o Attributes are the data properties of an object. They represent the state or
characteristics of the object. For example, a Car class might have attributes like
color, make, and model.
4. Methods:
o Methods are functions defined within a class that operate on the attributes of the
object. They define the behavior of the object. For example, a Car class might
have methods like start(), stop(), and accelerate().
5. Relationships:
o Object models define how objects relate to one another. Common types of
relationships include:
Association: A relationship where one object uses or interacts with
another. For example, a Driver object may have an association with a Car
object.
Inheritance: A mechanism by which one class can inherit attributes and
methods from another class, establishing a parent-child relationship. For
example, a SportsCar class might inherit from a Car class.
Composition: A strong form of association where an object is composed
of one or more other objects. For example, a Library may be composed
of multiple Book objects.
6. Interfaces:
o An interface defines a contract that classes must adhere to. It specifies a set of
methods without implementing them. This allows for polymorphism, where
different classes can be treated as instances of the same interface.
7. Events:
o Objects can also respond to events, which are occurrences that an object can listen
for and react to. Events enhance interactivity and dynamic behavior within the
object model.
Object models can be represented visually using diagrams, such as Unified Modeling Language
(UML) diagrams. UML provides various types of diagrams to depict the structure and behavior
of systems:
1. Class Diagrams:
o Class diagrams represent the static structure of the system, showing classes, their
attributes, methods, and relationships.
2. Object Diagrams:
o Object diagrams represent instances of classes and the relationships between them
at a specific moment in time.
3. Sequence Diagrams:
o Sequence diagrams show how objects interact with each other over time,
depicting the order of method calls.
4. State Diagrams:
o State diagrams illustrate the states an object can be in and how it transitions from
one state to another based on events.
Encapsulation: Objects encapsulate data and behavior, promoting data hiding and
reducing dependencies between components.
Reusability: Classes can be reused across different parts of a system or in different
systems, enhancing productivity.
Modularity: The object model promotes a modular approach, making it easier to
manage, understand, and maintain code.
Flexibility: Changes to the object model can often be made with minimal impact on other
parts of the system, allowing for easier updates and extensions.
Elements of OOPS
Object-Oriented Programming (OOP) is based on several key elements or principles that define
how objects interact within a system. These elements help in structuring software in a way that
promotes reusability, maintainability, and scalability. Here are the core elements of OOP:
1. Classes
Definition: A class is a blueprint or template for creating objects. It defines the attributes
(data) and methods (functions) that the objects created from the class will have.
Example:
cpp
class Car {
public:
string brand;
int year;
void startEngine() {
cout << "Engine started" << endl;
}
};
2. Objects
cpp
3. Encapsulation
Definition: Encapsulation is the principle of bundling the data (attributes) and methods
(functions) that operate on the data within a single unit (class). It restricts direct access to
some of the object's components, which can prevent unauthorized access and
modification.
Example:
cpp
class BankAccount {
private:
double balance;
public:
void deposit(double amount) {
balance += amount;
}
double getBalance() {
return balance;
}
};
4. Abstraction
Example:
cpp
class IDriveable {
public:
virtual void drive() = 0; // Pure virtual function
};
5. Inheritance
Definition: Inheritance allows a new class (derived class) to inherit attributes and
methods from an existing class (base class). This promotes code reuse and establishes a
hierarchical relationship between classes.
Example:
cpp
class Vehicle {
public:
int wheels;
void move() {
cout << "Moving" << endl;
}
};
6. Polymorphism
cpp
class Animal {
public:
virtual void sound() {
cout << "Some sound" << endl;
}
};
7. Composition
cpp
class Engine {
public:
void start() {
cout << "Engine started" << endl;
}
};
class Car {
private:
Engine engine; // Car "has-a" Engine
public:
void start() {
engine.start(); // Delegation
}
};
8. Interfaces
Definition: An interface is a contract that a class must adhere to. It defines a set of
methods without implementing them, allowing different classes to implement the same
interface in various ways.
Example:
java
interface Animal {
System.out.println("Bark");
System.out.println("Dog is eating");
}}
Input/Output (I/O) processing in C++:
Input/Output (I/O) processing in C++ is a fundamental aspect of programming, allowing the
interaction between a program and its environment. C++ provides several mechanisms for
performing I/O operations, primarily through its Standard Library. Here, we will cover the basics
of I/O processing, focusing on the <iostream> library, which includes the cin, cout, cerr, and
clog objects for input and output.
cpp
Copy code
#include <iostream>
cpp
Copy code
std::cout << "Hello, World!" << std::endl; // Using std::
// or
using namespace std;
cout << "Hello, World!" << endl; // Using namespace
oThe << operator is called the insertion operator, and it can be used to send various
types of data to the output stream.
3. Input with cin:
o The cin object is used for inputting data from the standard input (usually the
keyboard). It also uses the >> operator, known as the extraction operator.
cpp
Copy code
int number;
std::cout << "Enter a number: ";
std::cin >> number; // Read an integer from the user
Here’s a simple program that demonstrates the use of cin and cout:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int age;
string name;
cout << "Hello, " << name << "! You are " << age << " years old." << endl;
return 0;
}