UNIT II
Object based Programming
Procedural Programming
◦ sequence of things
◦ Emphasis is on doing things i.e. procedures.
◦ share global data
◦ top-down approach
Modular Programming
◦ Emphasizes separating the functionality
◦ modules perform logically discrete functions.
◦ no interaction between modules.
◦ Each module works independently
Generic Programming
◦ Generic programming is about generalizing software components.
◦ Algorithms are written in terms of to-be-specified-later.
◦ In C++ class and function templates are particularly effective
mechanisms.
Object-Oriented Programming
◦ decomposition of problem into a number of entities called objects
◦ Emphasis is on data rather than procedure.
◦ Objects have attributes and can take actions
◦ pass messages to objects, so that they take action
◦ The same message works differently for various objects
◦ A method can work appropriately with different types of data
◦ Objects can inherit traits of previously created objects
◦ Information hiding is more complete than in procedural programs
◦ New data and functions can be added easily whenever necessary.
◦ Follows bottom-up approach
Limitations of procedural programming
◦ Difficult to relate with real world objects
◦ Abstraction and modularization is difficult
◦ Difficult to understand
◦ Difficult to maintain
Need of object-oriented programming
◦ Possible to map objects in the problem domain
◦ Data Hiding : Security
◦ Inheritance : Eliminate redundancy , reusability
◦ Its flexibility
◦ It makes the coding more organized
◦ Simple maintenance, an advanced analysis of complicated programs
Structured Vs OOP
Structured OOP
Focuses on Focuses on
Process Object
Follows Top Down Follows Bottom
Approach Up Approach
Fundamentals of object-oriented programming
◦ objects
◦ classes
◦ data members
◦ methods
◦ messages
◦ data encapsulation
◦ data abstraction and information hiding
◦ inheritance
◦ polymorphism
Object
◦ Objects are basic run time entities.
◦ e.g. person, a place, a bank account, or any item that the program has to
handle.
◦ Each object take up space in the memory to store data and code.
◦ OOP encapsulates data (attributes) and functions (behavior) into
packages called objects. Objects have the property of information
hiding.
◦ Representation of object
◦ Objects Interact with each other by sending messages to each other.
• Class
◦ A class is a blueprint or prototype that defines the data members and the
methods (functions) common to all objects of a certain kind.
◦ Objects (data and functions) are members of user-defined types called
classes.
◦ A class definition is an extension of a C structure definition , made up of
declarations of variables and of function
◦ The variables are typically declared to be private or protected and the
functions are typically declared to be public.
◦ structures members are public by default and class members are private
by default.
Example : Objects and Classes
object class
class Student
char name
int rollNo
setName()
Rani Rima Rita Bina setRollNo()
R001 R003 R004
R002 calcMarks()
Program is composed of a collection of individual units or
objects, as opposed to a traditional view in which a program is
a list of instructions to the computer.
Object B
Data variable
Object A Object C
Member
Function
Data variable Data variable
Member Member
Function Function
• Data members
◦ Attributes of object.
◦ The variables are typically declared to be private or protected
• Methods or member functions or messages
◦ Functions operate on class data.
◦ functions are typically declared to be public.
◦ Defined outside or inside class.
◦ Objects communicate with each other.
Encapsulation
◦ The process of binding code and data together in the form of a capsule
◦ Data is not accessible to the outside world, only those function which are wrapped in
the class can access it.
◦ Since the classes use the concept of data abstraction, they are known as Abstract Data
Type(ADT).
◦ EXAMPLE-tv
Data Abstraction or Data Hiding
◦ The process of extracting the essential information and hiding the irrelevant details
Inheritance
◦ The feature by which one class acquires the properties and
functionalities of another class
◦ Classes are extensible
◦ You can create new classes that extend or are descendents of existing
classes
◦ The descendent classes can inherit all the attributes of the parent class
◦ Hierarchical classification
◦ Reusability
Person
Name
Gender
Age
Student Teaching Staff
Reg. No. Qualification
Course Designation
Marks Specialization
“Person” is a generalization of “Student”.
“Student” is a specialization of “Person”.