Unit 1
Procedure Oriented Programming
Features of procedure-oriented programming are:
● Emphasis is on doing things (Algorithms).
● Large programs are divided into smaller programs called as functions.
● Most of the functions share global data.
● Data move openly around the system from function to function.
● Functions transform data from one form to another
● Follows Top down approach
Object Oriented Programming
● Emphasis is on data rather than procedure.
● Data structures are designed such that they characterize the objects.
● Programs are divided into what are called objects.
● Functions that operate on the data of an object are tied together in the data structure.
● Data is hidden and cannot be accessed be external functions.
● Objects may communicate with each other through functions.
● New data and functions can be easily added whenever necessary.
● Follows bottom-up approach in program design.
Comparison Between Procedure Oriented vs Object Oriented Programming
Paradigm
Introduction
● Procedural Programming: Based on procedures or routines (functions) that perform
operations on data.
● Object-Oriented Programming: Based on objects, which encapsulate data and
methods to operate on that data.
Programming Style
● Procedural: Linear and follows a top-down approach.
● Object-Oriented: Modular and follows a bottom-up approach.
Procedural Oriented: Functions , Global/local variables
Object-Oriented:
● Classes and objects
● Inheritance, Polymorphism, Encapsulation, and Abstraction
Procedural:
● Focuses on functions and the sequence of operations.
Object-Oriented:
● Focuses on data and objects.
Data Handling
● Procedural: Data and functions are separate; data is passed between functions.
● Object-Oriented: Data and methods are bundled together in objects.
Code Reusability
● Procedural: Reusability is limited; functions can be reused.
● Object-Oriented: High reusability through inheritance and polymorphism.
Security
● Procedural: Less secure; global variables are accessible throughout the program.
● Object-Oriented: More secure; encapsulation restricts data access.
Scalability
● Procedural: Less suitable for complex and large programs.
● Object-Oriented: Better suited for large-scale applications due to modularity.
Ease of Debugging
● Procedural: Debugging becomes difficult as program complexity increases.
● Object-Oriented: Easier to debug and maintain due to modular structure.
Example
Procedural: C, Pascal, Fortran
Object-Oriented: C++, Java, Python
Object-Oriented Programming (OOP)
◆ Object-oriented programming aims to implement real-world entities in
programming.
◆ Main aim of OOP is to bind together the data and the functions that operate on
them so that no other part of the code can access this data except that function.
Characteristics of OOP
Class :
● Building block of C++ that leads to Object-Oriented programming is a Class.
● Class is a user-defined data type that has data members and member functions.
● Data members are the data variables and member functions are the functions used to
manipulate these variables
● Data members and member functions define the properties and behavior of the objects in a
Class.
● Class is a blueprint representing a group of objects which shares some common properties and
behaviors.
● Example :
Class of Cars , Class of Students , Class of Employees
Object
● Object is identifiable entity with some characteristics and
behavior.
● Object is an instance/ variable of a Class.
● When a class is defined, no memory is allocated but when it
is instantiated (i.e. an object is created) memory is allocated.
● When a program is executed the objects interact by sending
messages to one another.
● Each object contains data and code to manipulate the data.
● Objects can interact without having to know details of each
other’s data or code
● It is sufficient to know the type of message accepted and the
type of response returned by the objects
● Example :
Class Student Object Amit , Sumit
Class Car Object Maruti Brezza , Amaze, Alto
Encapsulation :
● Encapsulation is defined as wrapping up data and information under a single unit.
● Encapsulation is defined as binding together the data and the functions that manipulate them.
● Encapsulation also leads to data abstraction or data hiding.
● Using encapsulation also hides the data.
● Example: the data of any of the departments like sales, finance, or accounts are hidden from
any other departments
Abstraction using Classes:
● Abstraction in C++ can be implemented using classes.
● Class helps to group data members and member functions using available access specifiers (
private , public , protected )
● Class can decide which data member will be visible to the outside world and which is not.
Abstraction in Header files:
● Abstraction in C++ can be with header files.
● Consider the pow( ) method present in math.h header file.
● Call the function pow() present in the math.h header file and pass the numbers as arguments
without knowing the underlying algorithm according to which the function is actually calculating
the power of numbers.
Polymorphism
● Polymorphism means having many forms
● Polymorphism means the ability of a message to be
displayed in more than one form.
● Person at the same time can have different characteristics.
A man at the same time is a father, a husband, and an
employee
● Same person possesses different behavior in different
situations.
● Operation may exhibit different behaviors in different
instances.
● Behavior depends upon the types of data used in the
operation.
● C++ supports operator overloading and function
overloading to implement Polymorphism
Types of Polymorphism
● Compile-time (Method overloading)
● Runtime (Method overriding)
● Operator Overloading: Process of making an operator exhibit different behaviors in different
instances is known as operator overloading
4+5=9
Amit + Singh = Amit Singh
● Function Overloading: Function overloading is using a single function name to perform different
types of tasks.
Inheritance
● Capability of a class to derive properties and characteristics from another class is called
Inheritance.
● Sub Class / Derived Class : Class that inherits properties from another class
● Super Class / Base Class : Class whose properties are inherited by a sub-class
● Reusability: Inheritance supports the concept of “reusability” . Existing code / functionality can
be derived in another class .
Dynamic Binding
● Dynamic Binding (or Late Binding) refers to the process where the method to be
invoked is determined at runtime rather than compile-time.
● Code to be executed in response to the function call is decided at runtime.
● C++ has virtual functions to support this.
● Dynamic Binding enables runtime polymorphism by enabling the selection of the
appropriate method implementation based on the actual type of the object at
runtime.
● Works with pointers or references to base classes pointing to derived class objects.
Message Passing:
• Message Passing is a way for objects to communicate and interact with each other in Object-Oriented
Programming (OOP).
• Objects exchange information by calling each other's methods (functions) and passing data (arguments).
• Message passing facilitates interaction between objects.
• Objects interact by invoking methods on each other, leading to a more dynamic and flexible system
• Similar to sending a message or request to someone (object) to perform a task and receiving a
response.
.
Interface and Implementation of Class and Object
● Interface and implementation are two essential aspects that determine how a class and
its objects are designed, interact, and function.
Interface of a Class and Object
● Interface defines how the outside world interacts with a class or object.
● Specifies the methods and attributes that can be accessed or invoked by users of the class
without revealing how they are implemented internally.
Characteristics of Interface:
● Public Methods: These are the methods declared in the public section of the class and are
accessible to external code.
● Abstract View: Focuses on what a class or object does rather than how it does it.
● Encapsulation: Hides the internal details (implementation) and exposes only necessary
functionality.
Implementation of a Class
● Implementation is the actual code that defines how the methods and attributes of a class work.
● It provides the logic and data behind the interface.
Characteristics of Implementation:
● Private Data: Attributes and helper methods are usually declared as private or protected to
restrict direct access.
● Code Logic: The implementation details are provided in the function definitions
● Encapsulation: Ensures that changes to internal logic do not affect the interface.
Interface and Implementation in Object
● Object uses the interface to interact with external code and performs its tasks
using the implementation.
Advantages of Separating Interface and Implementation
● Encapsulation: Protects the internal details of a class.
● Code Reusability: Interface can remain consistent while reusing or improving the
implementation.
● Ease of Maintenance: Changes to the implementation don’t affect the user interface
● Modularity: Facilitates modular code design and testing.
Operations on Objects
● Object Creation : Objects are created as instances of a class. This involves
allocating memory for the object and initializing its attributes.
● Accessing and Modifying Attributes : Objects store data in their attributes (data
members). Operations can include reading or updating these values.
● Invoking Methods (Message Passing) : Objects perform tasks using their methods
(member functions). Calling a method is an operation where the object processes
data or performs a computation.
● Passing Objects as Arguments : Objects can be passed as arguments to functions
or methods. This allows interaction between objects or with external functions.
Operations on Objects
● Returning Objects : Objects can also be returned by functions, allowing further
operations or chaining of calls.
● Copying Objects : Objects can be copied using the assignment operator or a copy
constructor.
● Comparing Objects : Objects can be compared based on their attributes or by
overriding comparison operators.
● Object Destruction : When an object is no longer needed, its destructor is called to
free resources
Relationship Among Objects
● In OOP, objects do not exist in isolation
● Objects interact and form relationships to work together and represent complex
systems.
● Object relationships model real-world interactions and are essential for designing
robust software systems.
Types of Relationships Among Objects
● Association
● Aggregation
● Composition
● Dependency
● Inheritance
Association :
● Association represents a relationship between two or more objects where they are
connected through a link.
● It can be a one-to-one, one-to-many, or many-to-many relationship. For example, a
"Student" object may be associated with a "Course" object through an enrollment
relationship
● Association represents a "uses-a" or "has-a" relationship between two objects.
● It indicates that one object is related to another, but neither depends on the other's
life cycle.
● Objects are independent of each other.
● Example : Student and Course
● Example : Doctor and Patient
Aggregation :
● Aggregation is a "has-a" relationship where one object contains another, but the
contained object can exist independently.
● Aggregation is a special form of association where a part-whole relationship exists
between objects, and the part (child) can exist independently of the whole (parent).
● Example: Student Class and Address Class
Each student has a address
● Example: Library "has" books
Library Class aggregates Book Objects
● Example : Department has teachers
Department Class aggregates Teacher Objects
Composition
● Composition is a stronger form of aggregation where one object contains another,
and the contained object's lifecycle is tied to the container.
● If the container is destroyed, the contained object is also destroyed.
● Composition is a stronger form of aggregation where the part (child) cannot exist
without the whole (parent).
● Example: a "House" object may be composed of "Room" objects, and if the house
is destroyed, the rooms are also destroyed.
● Represents a "part-of" relationship.
● Example: Car "has" an engine, and an engine cannot exist independently of the car
● Example : House and Room
Dependency :
● Dependency is a weaker relationship where one object depends on another object
temporarily to perform some operations.
● It is often a "uses-a" relationship.
● Objects are not permanently linked.
● Example: A printer uses a document to print.
Inheritance :
● Inheritance is a "is-a" relationship where one class (derived) inherits the attributes
and behavior of another class (base)
● Inheritance is a mechanism where one class (subclass or child class) inherits
properties and behaviors from another class (superclass or parent class).
● It allows for code reuse and supports the "is-a" relationship.
● Example: a "Dog" class can inherit from an "Animal" class.
● Enables code reuse and hierarchy