Chapter 1 - Principal of Object Oriented Programming
Chapter 1 - Principal of Object Oriented Programming
Chapter 1 - Principal of Object Oriented Programming
Prepared by
Md. Masum Billah
Lecturer, Dept. of CSE, CBST
Mobile: 01793200796
Q. What is Programming?
Programming is the process of designing, writing, testing, and maintaining computer programs to solve
problems.
Programs can be used to automate tasks, process data, control hardware, build websites and mobile
applications, create video games, and perform many other functions.
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around
objects rather than functions and logic. An object is an instance of a class, which can contain both data
(attributes or properties) and methods (functions or behaviors).
1. Modularity: Breaks down large programs into manageable objects, improving organization and code
clarity.
2. Reusability: Encourages code reuse through inheritance and libraries, reducing redundancy and
development time.
3. Encapsulation: Protects data by restricting access, enhancing security and data integrity.
4. Productivity: Increases development efficiency through reusable objects and modular design.
5. Real-World Mapping: Models real-world entities, making design intuitive and easier to understand.
7. Scalability: Eases modification and extension of code, making software adaptable and maintainable
Q 3. Write down the difference between Procedure Oriented Programming(POP)
and Object Oriented Programming(OOP).
Or, How does Object oriented approach differ from Procedure oriented approach?
Difference between Procedure Oriented Programming and Object Oriented Programming
Procedure-Oriented Programming
Feature Object-Oriented Programming (OOP)
(POP)
Data and Function Functions operate on data passed as Data and methods are encapsulated within
Relation arguments objects
Problem Solving Tasks are broken down into Models real-world entities as objects that
Approach sequences of functions interact
Structured Programming (SP) and Object-Oriented Programming (OOP) are two distinct programming
paradigms, each with its own approach to organizing and managing code. Here's a comparison of the two:
Program Organized around functions and Organized around objects and classes that
Organization procedures that operate on data. encapsulate data and methods.
Less scalable; adding new features often More scalable; new features can be added
Scalability requires significant changes to existing with minimal changes by extending classes
code. or adding new objects.
Best for small to medium-sized tasks with Better suited for complex, large-scale
Flexibility
clear, linear logic. systems where objects interact.
Uses objects, but doesn’t support Fully supports all features of object-oriented
Definition
all OOP features. programming (OOP).
No inheritance (classes can't pass Supports inheritance (classes can pass traits to
Inheritance
traits to others). others).
An Abstract Data Type (ADT)is like a blueprint for a data structure that defines what we can do with the
data but hides how it's done. It specifies the operations we can perform (like adding, removing, or
checking data), without showing how the data is stored or managed internally.
Example:
Think of a stack:
- we can push items onto it, pop items off, and peek at the top item.
- we don’t need to know how the stack is stored inside (it could be an array or a list); you just use it based
on the available operations.
An ADT focuses on ‘what’ operations are available, not ‘how’ they work internally.
Hiding the internal details and showing Bundling data and methods that operate on the
Definition
only the essential features. data into a single unit and restricting access.
Focus What an object does (functionality). How data is protected and managed.
Achieved Abstract classes, interfaces, and function Classes with access specifiers (private,
Through declarations. protected, public).
Access to Not concerned with restricting data Controls access to data by restricting direct
Data access directly. manipulation.
To highlight relevant data and hide To safeguard the internal state and ensure data
Goal
unnecessary details. integrity.
These techniques allow developers to write modular, adaptable, and maintainable code.
Q 13. Define System Design. Explain briefly steps involved in Object Oriented Design
Approach.
System Design refers to the process of defining the architecture, components, interfaces, and data for a
system to satisfy specified requirements. It involves designing how different parts of the system interact
with each other, ensuring the system is scalable, efficient, and meets the needs of the users.
Identify the Objects: Determine the key objects (or classes) that are relevant to the problem domain.
Objects represent real-world entities or concepts within the system.
Define the Attributes and Methods: For each object, specify its attributes (data) and methods (functions)
that define its behavior. This step involves detailing what the object knows and what it can do.
Establish Relationships: Identify how the objects are related to each other. This includes defining
associations (how objects interact), inheritance (how objects share common features), and aggregation
(how objects are composed of other objects).
Design Class Hierarchies: Organize the objects into a hierarchy based on common traits. Use
inheritance to create a structured classification where child classes inherit features from parent classes.
Create Interaction Diagrams: Develop diagrams (like sequence or collaboration diagrams) to visualize
how objects interact with each other to perform specific tasks or processes. This helps in understanding
the flow of data and control within the system.
Object Class
Object is an instance of a class. Class is a blueprint or template from which
objects are created.
Object is a real world entity such as pen, laptop, Class is a group of similar objects.
mobile, bed, keyboard, mouse, chair etc.
Object is created through new keyword mainly e.g. Class is declared using class keyword e.g.
Student s1=new Student(); class Student{}
Object allocates memory when it is created. Class doesn't allocated memory when it is
created.
There are many ways to create object in java such There is only one way to define class in java using
as new keyword, newInstance() method, clone() class keyword.
method, factory method and deserialization.
Inheritance Polymorphism
Inheritance is one in which a new class is created
Whereas polymorphism is that which can be
(derived class) that inherits the features from the
defined in multiple forms.
already existing class(Base class).
Whereas it is basically applied to functions or
It is basically applied to classes.
methods.
Inheritance supports the concept of reusability and Polymorphism allows the object to decide which
reduces code length in object-oriented form of the function to implement at compile-time
programming. (overloading) as well as run-time (overriding).
Whereas it can be compiled-time polymorphism
Inheritance can be single, hybrid, multiple,
(overload) as well as run-time polymorphism
hierarchical and multilevel inheritance.
(overriding).
It is used in pattern designing. While it is also used in pattern designing.
Example : Example :
The class bike can be inherit from the class of The class bike can have method name set_color(),
two-wheel vehicles, which is turn could be a which changes the bike’s color based on the name
subclass of vehicles. of color you have entered.
2. Extensibility: Dynamic binding allows new derived classes to be added with minimal changes to
existing code. The base class functions can be overridden in derived classes to provide specific
functionality, and dynamic binding ensures that the correct function is called based on the object's type.
3. Code Reusability: It allows for writing more generic and reusable code. we can write code that works
with pointers or references to the base class, and it will automatically work with any derived class that
overrides the base class's virtual functions.
4. Maintainability: By reducing the need for conditional statements (like if-else or switch), dynamic
binding makes code cleaner and easier to maintain.
These are the fundamental types provided by the C/C++ language. They include:
Function: Represents a block of code that performs a specific task and can return a value.
Array: A collection of elements of the same type stored in contiguous memory locations (e.g., int
arr[5];).
Pointer: Stores the memory address of another variable (e.g., int *ptr;).
Reference: An alias for another variable (e.g., int &ref = var;).
These allow the programmer to define their own data types. They include:
Class: A blueprint for creating objects that encapsulate data and functions.
Structure (struct): A group of variables of different data types under a single name.
Union: Similar to a structure, but all members share the same memory location, so only one
member can hold a value at a time.
Enum: A user-defined type consisting of integral constants (e.g., enum Color {RED, GREEN,
BLUE};).
Typedef: Used to give a new name to an existing data type (e.g., typedef unsigned long int uli;).
Q 18. Write a C++ program that inputs a String from the keyboard and Determines
the length of String.
Solution:
#include <iostream>
int main() {
char userInput[100];
int length = 0;
cout << "The length of the string is: " << length << endl;
return 0;
}
Q 19. Write a simple C++ program to manipulate a stack where push and pop functions are used to
help this manipulation.
Solution:
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> myStack;
cout << "Elements pushed onto the stack: 10, 20, 30" << endl;
while (!myStack.empty()) {
cout << myStack.top() << endl;
myStack.pop();
}
return 0;
}
Solution:
#include <iostream>
#include <cmath> // For pow() function
int main() {
return 0;
}
1. Hides Details: It keeps the internal workings of an object hidden from the outside world. This means
you only need to know how to use an object, not how it works internally.
2. Organizes Code: It groups related data and functions into a single unit (a class). This makes the code
easier to manage and understand.
3. Easier Changes: If you need to change how something works, you only need to update the code inside
the class. The rest of your program remains unaffected.
4. Prevents Errors: By controlling access to the data (usually through methods), it helps prevent
accidental changes to the object's state from outside the class.
therefore, encapsulation simplifies program by hiding complexity, organizing code better, making
updates easier, and reducing errors.
Object: In OOP object is the representation of real life entity. Objects are instances of classes. Each
object contains data and functions to manipulate the data. Following statements declare two objects of
class Box −
Both of the objects Box1 and Box2 will have their own copy of data members.
Class: Class is the blue print for creating object. Objects are variables of the type class. Once a class has
been defined, we can create any number of objects belonging to that class. A class definition starts with
the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A
class definition must be followed either by a semicolon or a list of declarations. For example, we defined
the Box data type using the keyword class as follows –
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
Inheritance: It is the process by which object of one class acquire the properties or features of objects of
another class. The concept of inheritance provides the idea of re-usability. Means we can add additional
features to an existing class without modifying it.
Syntax:
class subclass_name : access_mode base_class_name
{
//body of subclass
};
Data abstraction and Encapsulation: Data abstraction refers to, providing only essential information to
the outside world and hiding their background details, i.e., to represent the needed information in
program without presenting the details. Encapsulation is an Object Oriented Programming concept that
binds together the data and functions in single unit and that keeps both safe from outside interference and
misuse.
Polymorphism: The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of an object to take on many forms.
Real life example of polymorphism, a person at the same time can have different characteristic. Like a
man at the same time is a father, a husband, an employee. So the same person possess different behavior
in different situations. This is called polymorphism. Polymorphism is considered as one of the important
features of Object Oriented Programming.
Dynamic binding: Dynamic binding also called dynamic dispatch is the process of linking
procedure call to a specific sequence of code (method) at run-time. It means that the code
to be executed for a specific procedure call is not known until run-time. Dynamic binding
is also known as late binding or run-time binding.
Message Passing: In Object-Oriented Programming (OOP), message passing is a way for objects to
communicate with each other by sending messages. A message is a request sent by one object to another
object to perform a certain action or to return some information.
When an object receives a message, it decides how to respond based on its own internal state and the
content of the message. This is known as message handling or message processing.
It shows, what elements are necessary to build a It hides the complexity of a system.
system.
Its application during 'design level'. Its application during 'Implementation level'.
This is Achieved through encapsulation. This is Achieved through making the members of a
class as 'private'.
The GUI of a mobile phone, it has some icons to After the icon is clicked, the end user has no idea
click on, which on click perform the specific regarding its implementation details
function.